题目链接:传送门
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 16525 Accepted Submission(s): 7792
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
FF算法模板
#include <cstdio>
#include <algorithm>
using namespace std;
#define size1 20
#define size2 1009
int N,M;
//边的结构
struct edge_t{
int node;
int c;//c为容量
edge_t* next;
edge_t* redge;//指向反向边
}Edge[size2*2];
int ECnt;
//图的邻接表
edge_t* Ver[size1];
void init(){
ECnt = 0;
fill(Ver,Ver+size1,(edge_t*)0);
}
//生成双向边
void mkEdge(int a,int b,int c){
int t1 = ECnt++;
int t2 = ECnt++;
Edge[t1].node = b;
Edge[t1].c = c;
Edge[t1].next = Ver[a];
Edge[t1].redge = Edge + t2;
Ver[a] = Edge + t1;
Edge[t2].node = a;
Edge[t2].c = 0;
Edge[t2].next = Ver[b];
Edge[t2].redge = Edge + t1;
Ver[b] = Edge + t2;
}
bool F[size1];
//u当前节点,f为当前流
//寻找增广路径
int dfs(int u,int f){
if ( u == N ) return f;
F[u] = true;
for(edge_t*p=Ver[u];p;p=p->next){
int v = p->node;
if( F[v] ) continue;
int c = p->c;
if ( c > 0 ){
int t = dfs(v,min(c,f));
if ( 0 == t ) continue;
p->c -= t;
p->redge->c += t;
return t;
}
}
return 0;
}
int solve(){
int ret = 0;
while(1){
fill(F,F+size1,false);
int t = dfs(1,INT_MAX);
if ( 0 == t ) return ret;
ret += t;
}
}
void read(){
scanf("%d%d",&N,&M);
init();
for(int i=0;i<M;++i){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
mkEdge(a,b,c);
}
}
int main(){
int Count = 0;
int T;
scanf("%d",&T);
while( T-- )
{
read();
printf("Case %d: %d\n",++Count,solve());
}
return 0;
}