点击打开链接
Destroying the bus stations
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1994 Accepted Submission(s): 613
Total Submission(s): 1994 Accepted Submission(s): 613
Problem 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.
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.
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
Recommend
gaojie | We have carefully selected several similar problems for you:
2491
2489
2492
2490
2487
题意是说有n个点,m条连边,并且边是单向的,每条边的距离为1,源点为1,汇点为n。让你求至少摧毁多少个点能够使得从1号到n号的距离大于k。
本题可以用最大流解决,不过还要重新构图,比较麻烦,也不大会。
想不到的是居然用最小费用最大流便可以解决。
题目的意思是删除最少的点使1,n的最短路大于k。将点转化为边,容量为1,费用为0,然后就是对于那些有道路的城市之间连边,若(u,v)有边,则连边(u+n)->v,容量为inf,费用为花费的时间1,然后就是跑最小费了,若dist[vt]>k,则返回false,最后输出的flow就代表要删除的点的个数。
题意是说有n个点,m条连边,并且边是单向的,每条边的距离为1,源点为1,汇点为n。让你求至少摧毁多少个点能够使得从1号到n号的距离大于k。
本题可以用最大流解决,不过还要重新构图,比较麻烦,也不大会。
想不到的是居然用最小费用最大流便可以解决。
题目的意思是删除最少的点使1,n的最短路大于k。将点转化为边,容量为1,费用为0,然后就是对于那些有道路的城市之间连边,若(u,v)有边,则连边(u+n)->v,容量为inf,费用为花费的时间1,然后就是跑最小费了,若dist[vt]>k,则返回false,最后输出的flow就代表要删除的点的个数。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
const int MAXN=610*610*2+2;
const int inf=1<<29;
int pre[MAXN]; // pre[v] = k:在增广路上,到达点v的边的编号为k
int dis[MAXN]; // dis[u] = d:从起点s到点u的路径长为d
int vis[MAXN]; // inq[u]:点u是否在队列中
int path[MAXN];
int head[MAXN];
int NE,tot,ans,max_flow,map[666][666],k,n;
struct node
{
int u,v,cap,cost,next;
} Edge[MAXN<<2];
void addEdge(int u,int v,int cap,int cost)
{
Edge[NE].u=u;
Edge[NE].v=v;
Edge[NE].cap=cap;
Edge[NE].cost=cost;
Edge[NE].next=head[u];
head[u]=NE++;
Edge[NE].v=u;
Edge[NE].u=v;
Edge[NE].cap=0;
Edge[NE].cost=-cost;
Edge[NE].next=head[v];
head[v]=NE++;
}
bool SPFA(int s,int t) // 源点为0,汇点为sink。
{
int i;
for(i=s;i<=t;i++) dis[i]=inf;
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
fill(dis,dis+2*n+1,inf);
dis[s] = 0;
queue<int>q;
q.push(s);
vis[s] =1;
while(!q.empty()) // 这里最好用队列,有广搜的意思,堆栈像深搜。
{
int u =q.front();
q.pop();
for(i=head[u]; i!=-1;i=Edge[i].next)
{
int v=Edge[i].v;
if(Edge[i].cap >0&& dis[v]>dis[u]+Edge[i].cost)
{
dis[v] = dis[u] + Edge[i].cost;
pre[v] = u;
path[v]=i;
if(!vis[v])
{
vis[v] =1;
q.push(v);
}
}
}
vis[u] =0;
}
if(dis[t]>k)return false;
return dis[t]!=inf;
}
void end(int s,int t)
{
int u, sum = inf;
for(u=t; u!=s; u=pre[u])
{
sum = min(sum,Edge[path[u]].cap);
}
max_flow+=sum; //记录最大流
for(u = t; u != s; u=pre[u])
{
Edge[path[u]].cap -= sum;
Edge[path[u]^1].cap += sum;
ans += sum*Edge[path[u]].cost; // cost记录的为单位流量费用,必须得乘以流量。
}
}
int main()
{
//freopen("in.txt","r",stdin);
int i,j,m,s,t;
while(scanf("%d%d%d",&n,&m,&k),n|m|k)
{
memset(head,-1,sizeof(head));
NE=ans=max_flow=s=0;
s=1;t=n;
for(i=2;i<n;i++)addEdge(i,i+n,1,0);
int a,b;
while(m--)
{
scanf("%d%d",&a,&b);
if(a==s)addEdge(s,b,inf,1);
else addEdge(n+a,b,inf,1);
}
while(SPFA(s,t))
{
end(s,t);
}
printf("%d\n",max_flow);
}
return 0;
}