题意:网络流模板题,求1到n的最大流,直接写模板。
代码:
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#define N 30
#define INF 1000000
using namespace std;
int Map[N][N],path[N],t,n,m;
bool bfs(int s,int t){
memset(path,-1,sizeof(path));
queue<int> q;
int p;
path[s]=s;
q.push(s);
while(!q.empty()){
p=q.front();
q.pop();
for(int i=1;i<=n;i++){
if(Map[p][i]>0&&path[i]==-1){
path[i]=p;
if(i==t) return true;
q.push(i);
}
}
}
return false;
}
int Ek(int s,int t){
int flow=0,d;
while(bfs(s,t)){
d=INF;
for(int i=t;i!=s;i=path[i])
d=min(d,Map[path[i]][i]);
for(int i=t;i!=s;i=path[i]){
Map[path[i]][i]-=d;
Map[i][path[i]]+=d;
}
flow+=d;
}
return flow;
}
int main(){
scanf("%d",&t);
int tot=1;
while(t--){
memset(Map,0,sizeof(Map));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
int from,to,flow;
scanf("%d%d%d",&from,&to,&flow);
Map[from][to]+=flow;
}
printf("Case %d: %d\n",tot,Ek(1,n));
tot++;
}
return 0;
}
来源:https://www.cnblogs.com/LMissher/p/7398875.html