标签:ever mem instead nes out may nbsp miss panel
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25940 Accepted Submission(s): 12214
1 #include <bits/stdc++.h> 2 const int INF=1e9; 3 const int maxn=300; 4 using namespace std; 5 6 int n,m,graph[maxn][maxn],pre[maxn];///graph[][]不仅记录图,还是残留网络 7 8 int bfs(int s,int t){ 9 int flow[maxn]; 10 memset(pre,-1,sizeof pre); 11 flow[s]=INF; pre[s]=0;///初始化起点 12 queue<int> Q; Q.push(s);///起点入栈,开始BFS 13 while(!Q.empty()){ 14 int u=Q.front(); Q.pop(); 15 if(u==t)break;///搜到一个路径,这次BFS结束 16 for(int i=1;i<=m;i++){///BFS所有的点 17 if(i!=s&&graph[u][i]>0&&pre[i]==-1){ 18 pre[i]=u;///记录路径 19 Q.push(i); 20 flow[i]=min(flow[u],graph[u][i]);///更新结点流量 21 } 22 } 23 } 24 if(pre[t]==-1)return -1; 25 return flow[t]; 26 } 27 28 int maxflow(int s,int t){ 29 int Maxflow=0; 30 while(1){ 31 int flow=bfs(s,t);///执行一次BFS,找到一条路径,返回路径的流量 32 if(flow==-1)break;///没有找到新的增光路,结束 33 int cur=t;///更新路径上的残留网络 34 while(cur!=s){///一直沿路径回溯到起点 35 int father=pre[cur];///pre[]记录路径上的前一个点 36 graph[father][cur]-=flow;///更新残留网络:正向减 37 graph[cur][father]+=flow;///更新残留网络:反向加 38 cur=father; 39 } 40 Maxflow+=flow; 41 } 42 return Maxflow; 43 } 44 45 int main(){ 46 while(~scanf("%d%d",&n,&m)){ 47 memset(graph,0,sizeof graph); 48 for(int i=0;i<n;i++){ 49 int u,v,w; 50 scanf("%d%d%d",&u,&v,&w); 51 graph[u][v]+=w;///可能有重边 52 } 53 printf("%d\n",maxflow(1,m)); 54 } 55 }
无注释版本
1 #include <bits/stdc++.h> 2 const int INF=1e9; 3 const int maxn=300; 4 using namespace std; 5 6 int n,m,graph[maxn][maxn],pre[maxn]; 7 8 int bfs(int s,int t){ 9 int flow[maxn]; 10 memset(pre,-1,sizeof pre); 11 flow[s]=INF; pre[s]=0; 12 queue<int> Q; Q.push(s); 13 while(!Q.empty()){ 14 int u=Q.front(); Q.pop(); 15 if(u==t)break; 16 for(int i=1;i<=m;i++){ 17 if(i!=s&&graph[u][i]>0&&pre[i]==-1){ 18 pre[i]=u; 19 Q.push(i); 20 flow[i]=min(flow[u],graph[u][i]); 21 } 22 } 23 } 24 if(pre[t]==-1)return -1; 25 return flow[t]; 26 } 27 28 int maxflow(int s,int t){ 29 int Maxflow=0; 30 while(1){ 31 int flow=bfs(s,t); 32 if(flow==-1)break; 33 int cur=t; 34 while(cur!=s){ 35 int father=pre[cur]; 36 graph[father][cur]-=flow; 37 graph[cur][father]+=flow; 38 cur=father; 39 } 40 Maxflow+=flow; 41 } 42 return Maxflow; 43 } 44 45 int main(){ 46 while(~scanf("%d%d",&n,&m)){ 47 memset(graph,0,sizeof graph); 48 for(int i=0;i<n;i++){ 49 int u,v,w; 50 scanf("%d%d%d",&u,&v,&w); 51 graph[u][v]+=w; 52 } 53 printf("%d\n",maxflow(1,m)); 54 } 55 }
hdu1532 Drainage Ditches(最大流+EK)
标签:ever mem instead nes out may nbsp miss panel
原文地址:https://www.cnblogs.com/ChangeG1824/p/11722060.html