您的位置:首页 > 其它

Codeforces 547 D. Mike and Fish

2015-08-02 14:53 465 查看
http://codeforces.com/contest/547/problem/D

还是太弱了,怎么都想不出来,最后看了标签贴着 graphs,感觉有了点方向,想到了网络流。。。最近做好多题都需要标签指路。

源点S和横坐标连线,S->x , cnt 是横坐标为x的点数,则这条边的下界是 cnt / 2,上界是 (cnt + 1) / 2 ,纵坐标与汇点T相连。对于点(x,y) , 连一条x -> y 的边,下界0,上界1。

然后就是求有源汇的有上下界的可行流。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
#define rep(i,n) for(int i=0; i mpx,mpy;

void edge(int a,int b,int c)
{
v[tol] = b;
f[tol] = c;
nxt[tol] = fst[a];
fst[a] = tol++;

v[tol] = a;
f[tol] = 0;
nxt[tol] = fst[b];
fst[b] = tol++;
}

int cur[N<<2],d[N<<2];
bool vis[N<<2];

bool BFS()
{
mst(vis,0);
queue Q;
Q.push(SS);
d[SS] = 0;
vis[SS] = 1;
while(!Q.empty())
{
int u = Q.front();Q.pop();
for(int e=fst[u]; ~e; e=nxt[e]) if(!vis[v[e]] && f[e] > 0)
{
vis[v[e]] = 1;
d[v[e]] = d[u] + 1;
Q.push(v[e]);
}
}
return vis[TT];
}
int DFS(int u,int a)
{
if(u == TT || a == 0) return a;
int flow = 0,tmpf;
for(int& e=cur[u]; ~e; e=nxt[e]) if(d[v[e]] == d[u] + 1)
{
if((tmpf = DFS(v[e],min(a,f[e]))) > 0)
{
f[e] -= tmpf;
f[e^1] += tmpf;
flow += tmpf;
a -= tmpf;
if(a == 0) break;
}
}
return flow;
}
int Maxflow()
{
int flow = 0;
while(BFS())
{
memcpy(cur,fst,sizeof(fst));
flow += DFS(SS,INF);
}
return flow;
}
int main()
{
scanf("%d",&n);
for(int i=0; i> 1;
int high = cnt_x[X[i]] - low;
edge(S,i+1,high-low);
edge(S,TT,low);
edge(SS,i+1,low);
}
for(int i=0; i<_Y; i++)
{
int low = cnt_y[Y[i]] >> 1;
int high = cnt_y[Y[i]] - low;
edge(i+_X+1,T,high-low);
edge(i+_X+1,TT,low);
edge(SS,T,low);
}
edge(T,S,INF);
Maxflow();
for(int i=0; i
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息