如题:http://poj.org/problem?id=1273
EK算法求最大流,注意重边的处理。
#include<iostream>
using namespace std;
#include<queue>
#define inf 0x7fffffff
int map[202][202];
queue<int>myque;
int pre[202];
int N,M;
int MIN(int x,int y){return x<y?x:y;}
bool bfs(int src,int des)
{
memset(pre,-1,sizeof(pre));
while(!myque.empty())
myque.pop();
pre[src]=1;
int index;
myque.push(src);
int i;
while(!myque.empty())
{
index=myque.front();
myque.pop();
for(i=1;i<=N;i++)
{
if(pre[i]==-1&&map[index][i]>0)
{
pre[i]=index;
if(i==des) return true;
myque.push(i);
}
}
}
return false;
}
int MaxFlow(int src,int des)
{
int maxflow=0;
int i;
while(bfs(src,des))
{
int minflow=inf;
for(i=des;i!=src;i=pre[i])
minflow=MIN(minflow,map[pre[i]][i]);
for(i=des;i!=src;i=pre[i])
{
map[pre[i]][i]-=minflow;
map[i][pre[i]]+=minflow;
}
maxflow+=minflow;
}
return maxflow;
}
int main()
{
int i,u,v,w;
//freopen("C:\\1.txt","r",stdin);
while(~scanf("%d %d",&M,&N))
{
memset(map,0,sizeof(map));
for(i=1;i<=M;i++)
{
scanf("%d%d%d",&u,&v,&w);
map[u][v]+=w;
}
printf("%d\n",MaxFlow(1,N));
}
}