POJ 3921 HDU 2485 Destroying the bus stations 最小费用最大流

 
Destroying the bus stations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 641   Accepted: 188

Description

Gabiluso is one of the greatest spies in his country. Now he's trying to complete an “impossible” mission - to make it slow for the army of City Colugu to reach the airport. City Colugu has n bus stations and m roads. Each road connects two bus stations directly, and all roads are one way streets. In order to keep the air clean, the government bans all military vehicles. So the army must take buses to go to the airport. There may be more than one road between two bus stations. If a bus station is destroyed, all roads connecting that station will become no use. What's Gabiluso needs to do is destroying some bus stations to make the army can't get to the airport in k minutes. It takes exactly one minute for a bus to pass any road. All bus stations are numbered from 1 to n. The No.1 bus station is in the barrack and the No. n station is in the airport. The army always set out from the No. 1 station.

No.1 station and No. n station can't be destroyed because of the heavy guard. Of course there is no road from No.1 station to No. n station.

Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.

Input

There are several test cases. Input ends with three zeros.
For each test case:
The first line contains 3 integers, n, m and k. (0 < n <= 50,0 < m <= 4000, 0 < k < 1000)
Then m lines follows. Each line contains 2 integers, s and f, indicating that there is a road from station No. s to station No. f.

Output

For each test case, output the minimum number of stations Gabiluso must destroy.

Sample Input

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

Sample Output

2

Source

Beijing 2008
 
练习赛没做出来。
搜解题报告有说是最大流(最小割),有说是最小费用最大流。
用最大流AC了,但是看HDU说有组数据过不掉:
8 10 5
1 2
2 3
3 4
4 5
5 6
6 8
1 7
7 8
4 7
7 4
正解是最小费用最大流。
题意是删除最少的点使1到n的最短路大于k。
把点拆成边,相当于去掉最少的边也就是最小割,
每一次spfa后去掉一个点,当前的最短路就被断开了,直到最短路大于k。
个人理解如此。
 
代码:
#include<cstdio>
#include<cstring>
#define N 1005
#define inf 999999999
#define min(a,b) ((a)<(b)?(a):(b))

int n,m,k,s,t,num;
int low[N],pre[N],adj[N],q[N];
struct edge
{
	int u,v,c,w,next;
	edge(){}
	edge(int uu,int vv,int ww,int cc,int n)
	{u=uu;v=vv;w=ww;c=cc;next=n;}
}e[10005];
void insert(int u,int v,int w,int c)
{
	e[num]=edge(u,v,w,c,adj[u]);
	adj[u]=num++;
	e[num]=edge(v,u,-w,0,adj[v]);
	adj[v]=num++;
}
int spfa()
{
	int i,x,f[N]={0},head=0,tail=0;
	q[++tail]=s;
	memset(low,0x3f,sizeof(low));
	pre[s]=-1;
	low[s]=0;
	while(head!=tail)
	{
		x=q[head=(head+1)%N];
		f[x]=0;
		for(i=adj[x];i!=-1;i=e[i].next)
			if(e[i].c&&low[e[i].v]>low[x]+e[i].w)
			{
				pre[e[i].v]=i;
				low[e[i].v]=low[x]+e[i].w;
				if(!f[e[i].v])
				{
					f[e[i].v]=1;
					q[tail=(tail+1)%N]=e[i].v;
				}
			}
	}
	return low[t]<inf;
}
int mincost()
{
	int ans=0;
	while(spfa())
	{
		if(low[t]>k)
			break;
		int v;
		ans++;
		for(v=pre[t];v!=-1;v=pre[e[v].u])
		{
			e[v].c--;
			e[v^1].c++;
		}
	}
	return ans;
}
int main()
{
	int i,u,v;
	while(~scanf("%d%d%d",&n,&m,&k),n)
	{
		num=0;
		memset(adj,-1,sizeof(adj));
		s=0;
		t=n+n-1;
		while(m--)
		{
			scanf("%d%d",&u,&v);
			u--;v--;
			insert(u*2+1,v*2,1,1);
		}
		insert(s,1,0,inf);
		insert(2*n-2,t,0,inf);
		for(i=1;i<n-1;i++)
			insert(i*2,i*2+1,0,1);
		printf("%d\n",mincost());
	}
}

你可能感兴趣的:(struct,input,insert,each,AIR,output)