Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 10948 Accepted Submission(s): 5184
Problem Description
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.
Input
The first line of input contains an integer T, denoting the number of test cases.
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)
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)
Output
For each test cases, you should output the maximum flow from source 1 to sink N.
Sample Input
2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1
Sample Output
Case 1: 1 Case 2: 2
Author
HyperHexagon
Source
Recommend
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson num<<1,le,mid
#define rson num<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk make_pair
#define _f first
#define _s second
using namespace std;
const int INF =0x3f3f3f3f;
const int maxn= 15+5 ;
const int maxm=1000+20 ;
//const int INF= ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
//by yskysker123
int n,m;
int pre[maxn],a[maxn];
struct Edge
{
int from,to,cap,flow;
Edge(){}
Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
};
vector<Edge> edges;
vector<int > G[maxn];
inline void add_edge(int from ,int to, int cap)
{
edges.push_back(Edge( from,to,cap,0 ));
edges.push_back(Edge(to,from, 0, 0) );
int m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
void init()
{
FOR1(i,n) G[i].clear();
edges.clear();
}
int Edmondskarp(int st,int ed)
{
int tot=0;
while(1)
{
a[st]=INF;
memset(pre,-1,sizeof pre);
pre[st]=st;
queue<int >q;
q.push(st);
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=0;i<G[x].size();i++)
{
int id=G[x][i];
Edge &e=edges[id];
int y=e.to;
if(pre[y]!=-1||e.cap-e.flow<=0) continue;
pre[y]=id;
a[y]=min(a[x],e.cap-e.flow);
q.push(y);
}
if(pre[ed]!=-1) break;
}
if(pre[ed]==-1) break;
tot+=a[ed];
for(int i=ed;i!=st; )
{
int id=pre[i];
Edge & e=edges[id];
edges[id].flow+=a[ed];
edges[1^id].flow-=a[ed];
i=e.from;
}
}
return tot;
}
int main()
{
int x,y,ca,kase=0,T;scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
FOR1(i,m)
{
scanf("%d%d%d",&x,&y,&ca);
add_edge(x,y,ca);
}
printf("Case %d: %d\n",++kase,Edmondskarp(1,n) );
}
return 0;
}