Flow Problem
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 <algorithm>
#include <vector>
#include <queue>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 15;
struct Edge
{
int from, to, flow, cap;
};
vector<Edge> edges;
vector<int> g[maxn + 10];
void init(int n)
{
edges.clear();
for (int i = 1; i <= n; ++i)
g[i].clear();
}
void add_edge(int u, int v, int cap, int flow = 0)
{
edges.push_back(Edge{ u, v, flow, cap, });
edges.push_back(Edge{ v, u, 0, 0, });
int siz = edges.size();
g[u].push_back(siz - 2);
g[v].push_back(siz - 1);
}
int a[maxn + 10];//当前节点允许通过的流量
int pre[maxn + 10];//保存增广路径上的点的前驱顶点
int max_flow(int s, int t)
{
int total_flow = 0;
for( ; ;)
{
memset(a, 0, sizeof(a));
a[s] = 0x7fffffff;
queue<int> que;
que.push(s);
while (!que.empty())
{
int u = que.front();
que.pop();
for (int i = 0; i < g[u].size(); ++i)
{
Edge &e = edges[ g[u][i] ];
if (a[e.to] == 0 && e.cap > e.flow)//如果可以流
{
a[e.to] = min(a[u], e.cap - e.flow);
pre[e.to] = g[u][i];
que.push(e.to);
}
}
if (a[t] != 0)
break;
}
if (a[t] == 0)
break;
int cur_flow = a[t];
total_flow += cur_flow;
for (int u = t; u != s;u = edges[pre[u]].from)
{
edges[pre[u]].flow += cur_flow;//正向边流量增加
edges[pre[u] ^ 1].flow -= cur_flow;//反向边减少
}
}
return total_flow;
}
int main()
{
int T;
int _case = 1;
scanf("%d", &T);
while (T--)
{
int n, m;
scanf("%d%d", &n, &m);
init(n);
for (int i = 0; i < m; ++i)
{
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
}
printf("Case %d: %d\n", _case++, max_flow(1, n));
}
return 0;
}
dinic模板
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 20;
struct edge
{
int from, to, cap, flow;
};
vector <edge> Edge;//存所有的边
vector<int> G[MAXN];//存每条边能到达的顶点
bool vis[MAXN];//在bfs中作为访问数组
int dist[MAXN];//分层结构的深度数组
int n, m;
void init()//初始化
{
for(int i=0; i<=n; ++i)
G[i].clear();
Edge.clear();
}
void add_edge(int u, int v, int cap)
{
Edge.push_back((edge)
{
u, v, cap, 0
});
Edge.push_back((edge)
{
v, u, 0, 0
});//双向边
int siz=Edge.size();
G[u].push_back(siz-2);//正向边为偶数
G[v].push_back(siz-1);//正向边位置^1
}
bool bfs(int s, int t)//从s到t分层,直到t被分层后结束
{
memset(vis, false, sizeof(vis));
queue<int>q;
q.push(s);
vis[s]=true;
dist[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=0; i<G[u].size(); ++i)
{
edge &e=Edge[G[u][i]];
if(!vis[e.to]&&e.cap>e.flow)//如果是正向边,就分层
{
vis[e.to]=true;
dist[e.to]=dist[u]+1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a)//找增广路, a表示当前可以流过的最大流量
{
if(x==n||a==0)//n指终点, 超级汇点
return a;
int flow=0, f;
for(int i=0; i<G[x].size(); ++i)
{
edge &e=Edge[G[x][i]];//&直接修改正向边
if(dist[e.to]==dist[x]+1&&(f=dfs(e.to, min(a, e.cap-e.flow)))>0)
{
e.flow+=f;//正向边加
Edge[G[x][i]^1].flow-=f;//反向边减
flow+=f;//当前流量+增广路流量
a-=f;
if(!a)//如果没有流量可以通过,否则继续找增广路
break;
}
}
return flow;
}
int dinic(int s, int t)
{
int flow=0;
while(bfs(s, t))
flow+=dfs(s, 0x3f3f3f3f);
return flow;
}
int main()
{
int t;
int Case=1;
scanf("%d", &t);
while(t--)
{
init();
scanf("%d %d", &n, &m);
int u, v, c;
for(int i=1; i<=m; ++i)
{
scanf("%d %d %d", &u, &v, &c);
add_edge(u, v, c);
}
printf("Case %d: %d\n", Case++, dinic(1, n));
}
return 0;
}