Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 7647 Accepted Submission(s): 3560
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include<stack>
using namespace std;
typedef long long LL;
const int N=22;
const int INF=100000000;
struct edge
{
int to,cap,res;
};
vector<edge> g[N];
int used[N];
void add_edge(int from,int to,int cap)
{
edge tmp;
tmp.to=to;
tmp.cap=cap;
tmp.res=g[to].size();
g[from].push_back(tmp);
tmp.to=from;
tmp.cap=0;
tmp.res=g[from].size()-1;
g[to].push_back(tmp);
}
int dfs(int s,int t,int f) // 找一条增广路
{
if(s==t) return f;
used[s]=1;
for(int i=0;i<g[s].size();i++)
{
edge &e=g[s][i];
int d;
if(!used[e.to]&&e.cap>0) // e.cap>0 要满足,不然搜下去会为负值
{
d= dfs(e.to,t,min(f,e.cap));
if(d>0)
{
e.cap-=d;
g[e.to][e.res].cap+=d;
return d;
}
}
}
return 0;
}
void init()
{
for(int i=0;i<=N;i++)
g[i].clear();
}
LL max_flow(int s,int t)
{
LL ans=0;
for(;;)
{
memset(used,0,sizeof(used));
LL d=dfs(s,t,INF);
if(d==0)
return ans;
ans+=d;
}
}
int main()
{
int ca,v_sum,e_sum;
scanf("%d",&ca);
for(int kase=1;kase<=ca;kase++)
{
scanf("%d%d",&v_sum,&e_sum);
init();
for(int i=0;i<e_sum;i++)
{
int aa,bb,cc;
scanf("%d%d%d",&aa,&bb,&cc);
add_edge(aa,bb,cc);
}
printf("Case %d: ",kase);
printf("%lld\n",max_flow(1,v_sum));
}
return 0;
}
1694



被折叠的 条评论
为什么被折叠?



