Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 15036 Accepted Submission(s): 7092
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)
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
题意: 给你n个点 m条边 u v c 表示 u 到 v 的流量 求顶点1到n的最大流量
EK邻接表模板:
#include<cstdio>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
#include<queue>
using namespace std;
typedef long long ll;
const int N=20;
const int M=1005;
struct node
{
int u,v,cal,flow;
}q;
vector<node>edge;//存边
vector<int>head[N];//存边的下标(下面的代码看一下就懂了)
int a[N],pre[N];//pre记录增广路径
int m,n;
void init(int n)
{
edge.clear();
for(int i=1;i<=n;i++)
head[i].clear();
}
void add(int u,int v,int w)
{
edge.push_back(node{u,v,w,0});
edge.push_back(node{v,u,0,0});
int e=edge.size();
head[u].push_back(e-2);
head[v].push_back(e-1);
}
int solve()
{
int ans=0;
queue<int>que;
while(true)
{
que.push(1);
memset(a,0,sizeof(a));
a[1]=inf;
while(!que.empty())
{
int u=que.front();
que.pop();
for(int i=0;i<head[u].size();i++)
{
q=edge[head[u][i]];
if(!a[q.v]&&q.cal>q.flow)
{
pre[q.v]=head[u][i];
que.push(q.v);
a[q.v]=min(a[u],q.cal-q.flow);
}
}
}
if(!a[n]) break;
for(int v=n;v!=1;v=edge[pre[v]].u)
{
edge[pre[v]].flow+=a[n];
edge[pre[v]^1].flow-=a[n];
}
ans+=a[n];
}
return ans;
}
int main()
{
int T,cas=0;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
init(n);
int u,v,w;
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
}
printf("Case %d: %d\n",++cas,solve());
}
return 0;
}