(网络流) Sabotage -- UVA -- 10480

链接:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82835#problem/J

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; #define N 100005
#define INF 0x3fffffff struct node {int v, flow, next;} a[N<<]; int Layer[N], Head[N], cnt, n, m; void Init()
{
cnt = ;
memset(Head, -, sizeof(Head));
} void Add(int u, int v, int flow)
{
a[cnt].v = v;
a[cnt].flow = flow;
a[cnt].next = Head[u];
Head[u] = cnt++;
} bool BFS(int Start, int End)
{
queue<int>Q;
Q.push(Start); memset(Layer, , sizeof(Layer));
Layer[Start] = ; while(Q.size())
{
int u = Q.front(); Q.pop(); if(u==End) return true; for(int i=Head[u]; i!=-; i=a[i].next)
{
int v = a[i].v; if(!Layer[v] && a[i].flow)
{
Layer[v] = Layer[u] + ;
Q.push(v);
}
}
}
return false;
}
int DFS(int u, int MaxFlow, int End)
{
if(u==End) return MaxFlow; int uflow=; for(int i=Head[u]; i!=-; i=a[i].next)
{
int v = a[i].v;
if(Layer[v]==Layer[u]+ && a[i].flow)
{
int flow = min(a[i].flow, MaxFlow-uflow);
flow = DFS(v, flow, End); a[i].flow -= flow;
a[i^].flow += flow;
uflow += flow; if(uflow==MaxFlow) break;
}
} if(uflow==) Layer[u] = ; return uflow;
}
void Dinic(int Start, int End)
{
int MaxFlow = ; while(BFS(Start, End)==true)
MaxFlow += DFS(Start, INF, End);
} int main()
{
while(scanf("%d%d", &n, &m), n+m)
{
int i, u, v, flow, x[N], y[N]; Init();
for(i=; i<=m; i++)
{
scanf("%d%d%d", &u, &v, &flow);
x[i] = u, y[i] = v;
Add(u, v, flow);
Add(v, u, flow);
} Dinic(, ); for(i=; i<=m; i++)
{
if((!Layer[x[i]] && Layer[y[i]]) || (Layer[x[i]] && !Layer[y[i]]))
printf("%d %d\n", x[i], y[i]);
}
printf("\n");
}
return ;
}
上一篇:NAT、端口映射、内网穿透、公网IP都是啥


下一篇:转!!Java 基础面试题的剖析: short s1=1;s1 = s1 +1 报错? s1+=1 呢