HDU 3987 Harry Potter and the Forbidden Forest

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
  
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
 

Sample Output
  
Case 1: 3 Case 2: 2 Case 3: 2
 

Author
aMR @ WHU
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3986  3988  3983  3984  3985 
 
只知道先跑最小割,求边数还真是不太会。
先跑一遍dinic,之后将满流的边容量设为1,否则,容量设为inf,再跑dinic。
不会证明,算啦,背会就好啦!
注意加无向边时,一定要当作加了两条有向边。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int N=1005;
const int M=100005;
const int inf=1e9+7;
int T,n,m,s,t,cas,cnt,ans,hd[N],d[N];
queue<int>q;
struct edge
{
	int to,nxt,f;
}v[4*M];
void addedge(int x,int y,int z)
{
	v[++cnt].to=y;
	v[cnt].f=z;
	v[cnt].nxt=hd[x];
	hd[x]=cnt;
}
void addedges(int x,int y,int z)
{
	addedge(x,y,z),addedge(y,x,0);
}
bool bfs()
{
	memset(d,0,sizeof(d));
	d[s]=1;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=hd[u];i;i=v[i].nxt)
			if(v[i].f&&!d[v[i].to])
			{
				d[v[i].to]=d[u]+1;
				q.push(v[i].to);
			}
	}
	return d[t];
}
int dfs(int u,int lft)
{
	if(u==t||lft==0)
		return lft;
	int r=lft;
	for(int i=hd[u];i;i=v[i].nxt)
		if(r&&v[i].f&&d[v[i].to]==d[u]+1)
		{
			int w=dfs(v[i].to,min(r,v[i].f));
			v[i].f-=w,v[i^1].f+=w,r-=w;
			if(!r)
				return lft;
		}
	if(!r)
		d[u]=0;
	return lft-r;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		ans=0,cnt=1;
		memset(hd,0,sizeof(hd));
		scanf("%d%d",&n,&m);
		s=0,t=n-1;
		for(int i=1;i<=m;i++)
		{
			int x,y,z,k;
			scanf("%d%d%d%d",&x,&y,&z,&k);
			if(k==0)
				addedges(x,y,z);
			else
				addedges(x,y,z),addedges(y,x,z);
			
		}
		while(bfs())
			dfs(s,inf);
		for(int i=2;i<=cnt;i+=2)
			if(!v[i].f)
				v[i].f=1,v[i^1].f=0;
			else
				v[i].f=inf,v[i^1].f=0;
		while(bfs())
			ans+=dfs(s,inf);
		printf("Case %d: %d\n",++cas,ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值