| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
第一次用王建德的数组模拟邻接表。#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int limit[201][201];
int flow[201][201];
int queue[400];
int rear,front;
int pre[201];
int nowmax[201];
struct node
{
int to;
int next;
}mymalloc[410];
int my;
int city[201];
int s,t,n,m;
int maxflow()
{
int p,u,v,w,maxflow=0;
memset(flow,0,sizeof(flow));
for(;;)
{
front=rear=-1;
queue[++front]=s;
memset(nowmax,0,sizeof(nowmax));
nowmax[s]=0xfffffff;
while(front>rear)
{
u=queue[++rear];
p=city[u];
while(p!=-1)
{
v=mymalloc[p].to;
if(!nowmax[v]&&limit[u][v]>flow[u][v])
{
queue[++front]=v;
pre[v]=u;
nowmax[v]=nowmax[u]<(limit[u][v]-flow[u][v])?nowmax[u]:(limit[u][v]-flow[u][v]);
}
p=mymalloc[p].next;
}
if(nowmax[t]) break;
}
if(!nowmax[t]) break;
for(u=t;u!=s;u=pre[u])
{
flow[pre[u]][u]+=nowmax[t];
flow[u][pre[u]]-=nowmax[t];
}
maxflow+=nowmax[t];
}
return maxflow;
}
void init()
{
int i,from,to,cost;
memset(city,-1,sizeof(city));
s=1;t=n;
my=0;
memset(limit,0,sizeof(limit));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&from,&to,&cost);
limit[from][to]+=cost;
mymalloc[my].to=to;
mymalloc[my].next=city[from];
city[from]=my++;
mymalloc[my].to=from;
mymalloc[my].next=city[to];
city[to]=my++;
}
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
init();
cout<<maxflow()<<endl;
}
return 1;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator