链接:click here
题意:Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
翻译:网络流量是一个众所周知的难题ACMers。给定一个图,你的任务是找出加权有向图的最大流。
输出格式:
Case 1: 1 Case 2: 2思路:跟hdu1532 解法一样。
代码:
#include <ctype.h> //最大流 入门 #include <stdio.h> #include <vector> #include <stdlib.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1101; const int inf=0x3f3f3f3f; struct edge { int to,cap,rev; }; vector<edge > G[maxn]; //图的邻接表表示 bool used[maxn]; //访问标记 void add_edge(int from,int to,int cap) { G[from].push_back((edge) { to,cap,G[to].size() }); G[to].push_back((edge) { from,0,G[from].size()-1 }); } int dfs(int v,int t,int f) { if(v==t) return f; used[v]=true; for(int i=0; i<G[v].size(); i++) { edge &e=G[v][i]; if(!used[e.to]&&e.cap>0) { int d=dfs(e.to,t,min(f,e.cap)); if(d>0) { e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0; } int max_flow(int s,int t) { int flow=0; for(;;) { memset(used,0,sizeof(used)); int f=dfs(s,t,inf); if(f==0) return flow; flow+=f; } } int main() { int n,m,too,capp,revv; int tt,j=1; scanf("%d",&tt); while(tt--) { scanf("%d%d",&m,&n); memset(G,0,sizeof(G)); for(int i=0; i<n; i++) { scanf("%d%d%d",&too,&capp,&revv); add_edge(too,capp,revv); } printf("Case %d: %d\n",j++,max_flow(1,m)); } return 0; }
2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1
Case 1: 1 Case 2: 2
原文地址:http://blog.csdn.net/u013050857/article/details/43452067