bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群

  网络流,对于每一个行星,将行星所在行到行星连一条流量为1的边,将行星到其所在列连一条流量为1的边,从源点到所有行连一条流量为1的边,将所有列到汇点都连一条流量为1的边,最大流即为答案。

  代码

 #include<cstdio>
#include<algorithm>
#define N 1000050
#define M 1000050
const int inf =0x37373737;
using namespace std;
int n,m,i,a,b;
struct Dinic {
int s, t, n, pre[N], cur[N], h[N], level[N], sign, q[N];
int cap[M], to[M], ne[M], flow, e;
void liu(int u, int v, int w) {
to[e] = v, ne[e] = h[u], cap[e] = w;
h[u] = e++;
}
void link(int u, int v, int w) {
liu(u, v, w);
liu(v, u, );
}
void init(int n) {
for (int i = ; i <= n; ++i)
h[i] = -;
e = ;
}
bool bfs() {
int L = , R = ;
fill(level, level + n, -);
sign = q[R++] = t;
level[t] = ;
while (L < R && level[s] == -) {
int c = q[L++];
for (int k = h[c]; ~k; k = ne[k]) {
if (cap[k ^ ] > && level[to[k]] == -)
level[to[k]] = level[c] + , q[R++] = to[k];
}
}
return ~level[s];
}
void push() {
int pl = inf, p, k;
for (p = t; p != s; p = to[k ^ ]) {
k = pre[p];
pl = min(pl, cap[k]);
}
for (p = t; p != s; p = to[k ^ ]) {
k = pre[p];
cap[k] -= pl;
cap[k ^ ] += pl;
if (cap[k] == )
sign = to[k ^ ];
}
flow += pl;
}
void dfs(int c) {
if (c == t)
push();
else {
for (int &k = cur[c]; ~k; k = ne[k])
if (cap[k] > && level[to[k]] + == level[c]) {
pre[to[k]] = k;
dfs(to[k]);
if (level[sign] > level[c])
return;
sign = t;
}
level[c] = -;
}
}
int run(int _s, int _t, int _n) {
s = _s, t = _t, n = _n;
flow = ;
while (bfs()) {
for (int i = ; i < n; ++i)
cur[i] = h[i];
dfs(s);
}
return flow;
}
} mf;
int main()
{
scanf("%d%d",&n,&m);
mf.init(*n+m+);
for (i=;i<=n;i++)
mf.link(,i,),mf.link(n+i,*n+m+,);
for (i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
mf.link(a,*n+i,);
mf.link(*n+i,n+b,);
}
printf("%d\n",mf.run(,*n+m+,*n+m+));
}
上一篇:Java程序生成exe可执行文件详细教程(图文说明)


下一篇:UOJ #7 【NOI2014】 购票