您的位置:首页 > 其它

hdu 3987 Harry Potter and the Forbidden Forest 求将s和t隔开的最少费用下的最小边数 最小割

2011-09-20 13:06 232 查看
[align=left]Problem Description[/align]
Harry Potter notices some Death Eaters try to slip into Castle. The Death Eaters hide in the most depths of Forbidden Forest. Harry need stop them as soon as.



The Forbidden Forest is mysterious. It consists of N nodes numbered from 0 to N-1. All of Death Eaters stay in the node numbered 0. The position of Castle is node n-1. The nodes connected by some roads. Harry need block some roads by magic and he want to minimize
the cost. But it’s not enough, Harry want to know how many roads are blocked at least.

[align=left]Input[/align]
Input consists of several test cases.

The first line is number of test case.

Each test case, the first line contains two integers n, m, which means the number of nodes and edges of the graph. Each node is numbered 0 to n-1.

Following m lines contains information about edges. Each line has four integers u, v, c, d. The first two integers mean two endpoints of the edges. The third one is cost of block the edge. The fourth one means directed (d = 0) or undirected (d = 1).

Technical Specification

1. 2 <= n <= 1000

2. 0 <= m <= 100000

3. 0 <= u, v <= n-1

4. 0 < c <= 1000000

5. 0 <= d <= 1

[align=left]Output[/align]
For each test case:

Output the case number and the answer of how many roads are blocked at least.

[align=left]Sample Input[/align]

3

4 5
0 1 3 0
0 2 1 0
1 2 1 1
1 3 1 1
2 3 3 1

6 7
0 1 1 0
0 2 1 0
0 3 1 0
1 4 1 0
2 4 1 0
3 5 1 0
4 5 2 0

3 6
0 1 1 0
0 1 2 0
1 1 1 1
1 2 1 0
1 2 1 0
2 1 1 1


[align=left]Sample Output[/align]

Case 1: 3
Case 2: 2
Case 3: 2


//

在网络中任意流f是最大流,则该网络的所有可能存在的最小割的边一定包含在该流f的某些满流的边中。

先对原网络求一遍最大流后,对残量网络的满流的边赋值为1,非满流的边赋值为inf,对新网络求一遍最大流,该流的值即为最小割的最小边数。

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

const int N=2010;

const int M=550000;//500000 不够

const int inf=(1<<28);

int head
;

struct Edge

{

int v,next,w;

} edge[M];

int cnt,n,s,t;//n从0开始 0->n-1

void addedge(int u,int v,int w)

{

edge[cnt].v=v;

edge[cnt].w=w;

edge[cnt].next=head[u];

head[u]=cnt++;

edge[cnt].v=u;

edge[cnt].w=0;

edge[cnt].next=head[v];

head[v]=cnt++;

}

int sap()

{

int pre
,cur
,dis
,gap
;

int flow=0,aug=inf,u;

bool flag;

for(int i=0; i<n; i++)

{

cur[i]=head[i];

gap[i]=dis[i]=0;

}

gap[s]=n;

u=pre[s]=s;

while(dis[s]<n)

{

flag=0;

for(int &j=cur[u]; j!=-1; j=edge[j].next)

{

int v=edge[j].v;

if(edge[j].w>0&&dis[u]==dis[v]+1)

{

flag=1;

if(edge[j].w<aug) aug=edge[j].w;

pre[v]=u;

u=v;

if(u==t)

{

flow+=aug;

while(u!=s)

{

u=pre[u];

edge[cur[u]].w-=aug;

edge[cur[u]^1].w+=aug;

}

aug=inf;

}

break;

}

}

if(flag) continue;

int mindis=n;

for(int j=head[u]; j!=-1; j=edge[j].next)

{

int v=edge[j].v;

if(edge[j].w>0&&dis[v]<mindis)

{

mindis=dis[v];

cur[u]=j;

}

}

if((--gap[dis[u]])==0)

break;

gap[dis[u]=mindis+1]++;

u=pre[u];

}

return flow;

}

//初始化 cnt=0;memset(head,-1,sizeof(head));

int main()

{

int ci,pl=1;scanf("%d",&ci);

while(ci--)

{

int m,p;scanf("%d%d",&m,&p);

cnt=0;

memset(head,-1,sizeof(head));

n=m;

s=0;t=n-1;//最小割

while(p--)

{

int u,v,w,d;scanf("%d%d%d%d",&u,&v,&w,&d);

if(d) addedge(v,u,w);

addedge(u,v,w);

}

int mincost=sap();//最少费用将s,t隔开

for(int i=0;i<cnt;i++)

{

if(i&1) edge[i].w=0;

else

{

if(edge[i].w==0)

{

edge[i].w=1;

edge[i^1].w=0;

}

else

{

edge[i].w=inf;

edge[i^1].w=0;

}

}

}

int minedge=sap();//最少花费下的最少边数

printf("Case %d: %d\n",pl++,minedge);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: