pku1273 Drainage Ditches

http://poj.org/problem?id=1273

网络流,Dinic

 #include <stdio.h>
#include <string.h>
#include <queue> using namespace std; const int inf = <<;
const int maxn = , maxm = ; struct Edge
{
int v, f, nxt;
}; int n, src, sink;
int g[maxn + ];
int nume;
Edge e[maxm* + ]; void addedge(int u, int v, int c)
{
e[++nume].v = v;
e[nume].f = c;
e[nume].nxt = g[u];
g[u] = nume;
e[++nume].v = u;
e[nume].nxt = g[v];
g[v] = nume;
} queue<int> que;
bool vis[maxn + ];
int dist[maxn + ]; void bfs()
{
memset(dist, , sizeof(dist));
while(!que.empty())
{
que.pop();
}
vis[src] = true;
que.push(src);
while(!que.empty())
{
int u = que.front();
que.pop();
for(int i=g[u]; i; i=e[i].nxt)
{
if(e[i].f && !vis[e[i].v])
{
que.push(e[i].v);
dist[e[i].v] = dist[u] + ;
vis[e[i].v] = true;
}
}
}
} int dfs(int u, int delta)
{
if(u == sink)
{
return delta;
}
else
{
int ret = ;
for(int i=g[u]; delta && i; i=e[i].nxt)
{
if(e[i].f && dist[e[i].v] == dist[u] + )
{
int dd = dfs(e[i].v, min(e[i].f, delta));
e[i].f -= dd;
e[i ^ ].f += dd;
delta -= dd;
ret += dd;
}
}
return ret;
}
} int maxflow()
{
int ret = ;
while(true)
{
memset(vis, , sizeof(vis));
bfs();
if(!vis[sink])
{
return ret;
}
ret += dfs(src, inf);
}
} int main()
{
int n, m;
int i, x, y, z;
while(~scanf("%d%d", &m, &n))
{
memset(g, , sizeof(g));
nume = ;
src = ;
sink = n;
for(i=; i<=m; i++)
{
scanf("%d%d%d", &x, &y, &z);
addedge(x, y, z);
}
x = maxflow();
printf("%d\n", x);
}
return ;
}
上一篇:C语言地址对齐(转)--网络编程之结构体大小的计算


下一篇:2016青岛网络赛 Barricade