題目:http://acm.hdu.edu.cn/showproblem.php?pid=1532
題意:給定兩個數n m,分别代表圖的邊數和頂點數,然後n行形式如a b c,代表a到b之間有一條容量為c的有向邊。求1到m的最大流
思路:就是一模版題啊。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 210;
const int INF = 0x3f3f3f3f;
struct edge
{
int to, cap, next;
}g[N*100];
int iter[N], level[N], head[N];
int n, m, cnt;
void add_edge(int v, int u, int cap)
{
g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;
}
bool bfs(int s, int t)
{
memset(level, -1, sizeof level);
queue<int> que;
level[s] = 0;
que.push(s);
while(! que.empty())
{
int v = que.front(); que.pop();
for(int i = head[v]; i != -1; i =g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[u] < 0)
{
level[u] = level[v] + 1;
que.push(u);
}
}
}
return level[t] == -1;
}
int dfs(int v, int t, int f)
{
if(v == t) return f;
for(int &i = iter[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[v] < level[u])
{
int d = dfs(u, t, min(f, g[i].cap));
if(d > 0)
{
g[i].cap -= d, g[i^1].cap += d;
return d;
}
}
}
return 0;
}
int dinic(int s, int t)
{
int flow = 0, f;
while(true)
{
if(bfs(s, t)) return flow;
memcpy(iter, head, sizeof head);
while(f = dfs(s, t, INF), f > 0)
flow += f;
}
}
int main()
{
int a, b, c;
while(~ scanf("%d%d", &n, &m))
{
cnt = 0;
memset(head, -1, sizeof head);
for(int i = 0; i < n; i++)
{
scanf("%d%d%d", &a, &b, &c);
add_edge(a, b, c);
}
printf("%d\n", dinic(1, m));
}
return 0;
}