Harry Potter and the Forbidden Forest
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 158 Accepted Submission(s): 56
Problem Description
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.
Input
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
Output
For each test case:
Output the case number and the answer of how many roads are blocked at least.
Sample Input
34 5
0 1 3 0
0 2 1 0
1 2 1 1
1 3 1 1
2 3 3 16 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 03 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
Sample Output
Case 1: 3
Case 2: 2
Case 3: 2
Author
aMR @ WHU
Source
2011 Multi-University Training Contest 15 - Host by WHU
Recommend
lcy
和USACO 4.4.2 Pollutant Control类似。推荐题解:
http://hi.baidu.com/mengyun1993/blog/item/c30d193c9a85932870cf6cda.html
题目意思是求一个割边最少的最小割集的边数。
要求边最少则对原边权c进行更改:新边权w=1+(m+1)*c
求最大流maxflow,则最小割容量为maxflow/(m+1),最小割的最少边数是maxflow%(m+1)
重赋权后流对边的选择不会变,同时w中1的个数不会超过m,
所以统计1的个数就能求出最少边。
代码:
#include<cstdio>
#include<cstring>
#define N 1005
#define M 400005
#define inf 1ll<<60
#define min(a,b) ((a)<(b)?(a):(b))
int n,m,s,t,num,adj[N],dis[N],q[N];
struct edge
{
__int64 u,v,w,c,pre;
}e[M];
void insert1(__int64 u,__int64 v,__int64 w)
{
e[num]=(edge){u,v,w,w,adj[u]};
adj[u]=num++;
e[num]=(edge){v,u,0,w,adj[v]};//有向图
adj[v]=num++;
}
void insert2(__int64 u,__int64 v,__int64 w)
{
e[num]=(edge){u,v,w,w,adj[u]};
adj[u]=num++;
e[num]=(edge){v,u,w,w,adj[v]};//无向图
adj[v]=num++;
}
int bfs()
{
int i,x,v,head=0,tail=0;
memset(dis,0,sizeof(dis));
dis[s]=1;
q[tail++]=s;
while(head<tail)
{
x=q[head++];
for(i=adj[x];~i;i=e[i].pre)
if(e[i].w&&!dis[v=e[i].v])
{
dis[v]=dis[x]+1;
if(v==t)
return 1;
q[tail++]=v;
}
}
return 0;
}
__int64 dfs(__int64 x,__int64 limit)
{
if(x==t)
return limit;
__int64 i,v,tmp,cost=0;
for(i=adj[x];~i&&cost<limit;i=e[i].pre)
if(e[i].w&&dis[x]==dis[v=e[i].v]-1)
{
tmp=dfs(v,min(limit-cost,e[i].w));
if(tmp)
{
e[i].w-=tmp;
e[i^1].w+=tmp;
cost+=tmp;
}
else
dis[v]=-1;
}
return cost;
}
__int64 Dinic()
{
__int64 ans=0;
while(bfs())
ans+=dfs(s,inf);
return ans;
}
int main()
{
int tt=0,cc;
scanf("%d",&cc);
while(cc--)
{
scanf("%d%d",&n,&m);
int i,u,v,d;
__int64 w,c,ans;
memset(adj,-1,sizeof(adj));
num=0;
s=0;
t=n-1;
for(i=0;i<m;i++)
{
scanf("%d%d%I64d%d",&u,&v,&c,&d);
w=1+(m+1)*c;
if(d)
insert2(u,v,w);//无向边
else
insert1(u,v,w);//有向边
}
ans=Dinic();//maxflow=ans/(m+1);
printf("Case %d: %I64d\n",++tt,ans%(m+1));
}
}