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

本文探讨了一种使用最小费用最大流算法来优化城市公交网络,特别是通过摧毁最少的公交车站来限制军事车辆到达机场的时间,从而解决了一个看似不可能的任务。通过将城市公交站拆分为边,作者提出了一种方法来计算完成任务所需的最小数量的公交车站摧毁,以确保军事行动无法在规定时间内到达目标。实例和代码展示了如何应用这一算法,以及在实际问题中的应用和挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
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

 
练习赛没做出来。
搜解题报告有说是最大流(最小割),有说是最小费用最大流。
用最大流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());
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值