题目:http://www.lightoj.com/volume_showproblem.php?problem=1153
题意:给定一张图,给定源点s和汇点t,求s到t之间的最大流。输入格式:首先输入一个T,代表样例组数,然后是s t c,代表源点,汇点和边的条数,然后c行输入,每行a b c,代表从a到b有一条边,容量为c。
思路:dinic模板一套,最大流跑一下结果就出来了
总结:最大流水题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 120;
const int INF = 0x3f3f3f3f;
struct edge
{
int to, cap, next;
}g[N*100];
int head[N], iter[N], level[N];
int n, cnt, _case = 0;
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 = cap, g[cnt].next = head[u], head[u] = cnt++;
}
bool bfs(int s, int t)
{
memset(level, -1, sizeof level);
level[s] = 0;
queue<int> que;
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(g[i].cap, f));
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 T, s, t, m, a, b, c;
scanf("%d", &T);
while(T--)
{
cnt = 0;
memset(head, -1, sizeof head);
scanf("%d", &n);
scanf("%d%d%d", &s, &t, &m);
for(int i = 0; i < m; i++)
{
scanf("%d%d%d", &a, &b, &c);
add_edge(a, b, c);
}
printf("Case %d: %d\n", ++_case, dinic(s, t));
}
return 0;
}