Destroying the bus stations
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2313 Accepted Submission(s): 739
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.
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
Recommend
gaojie | We have carefully selected several similar problems for you: 2491 2489 2492 2490 2487
题意:n个点m条边的图,求至少摧毁多少点可以使得从1到n的路径大于k。
思路:先预处理求出最短路,还要拆点,i与i‘之间连一条权为1的边,若边(u,v)满足dist1[u]+dist1[v]<k则添加 u‘ 到 v 的边,权为INF,求一次最大流即可。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define MAXN 1005 #define MAXM 10005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; struct Edge { int to,next,cap,flow; }edge[MAXM]; int tol; int head[MAXN]; int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN]; void init() { tol=0; memset(head,-1,sizeof(head)); } //加边,单向图三个参数,双向图四个参数 void addedge(int u,int v,int w,int rw=0) { edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; edge[tol].flow=0; head[u]=tol++; edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v]; edge[tol].flow=0; head[v]=tol++; } //输入参数:起点,终点,点的总数 //点的编号没有影响,只要输入点的总数 int sap(int start,int end,int N) { memset(gap,0,sizeof(gap)); memset(dep,0,sizeof(dep)); memcpy(cur,head,sizeof(head)); int u=start; pre[u]=-1; gap[0]=N; int ans=0; while (dep[start]<N) { if (u==end) { int Min=INF; for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) if (Min>edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow; for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) { edge[i].flow+=Min; edge[i^1].flow-=Min; } u=start; ans+=Min; continue; } bool flag=false; int v; for (int i=cur[u];i!=-1;i=edge[i].next) { v=edge[i].to; if (edge[i].cap-edge[i].flow && dep[v]+1==dep[u]) { flag=true; cur[u]=pre[v]=i; break; } } if (flag) { u=v; continue; } int Min=N; for (int i=head[u];i!=-1;i=edge[i].next) if (edge[i].cap-edge[i].flow && dep[edge[i].to]<Min) { Min=dep[edge[i].to]; cur[u]=i; } gap[dep[u]]--; if (!gap[dep[u]]) return ans; dep[u]=Min+1; gap[dep[u]]++; if (u!=start) u=edge[pre[u]^1].to; } return ans; } struct EDGE { int u,v,w,next; }E1[MAXM],E2[MAXM]; int n,m,k,num1,num2; int head1[MAXN],head2[MAXN],dist1[MAXN],dist2[MAXN],inq[MAXN]; void init2() { num1=0;num2=0; memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2)); } void add1(int u,int v) { E1[num1].u=u; E1[num1].v=v; E1[num1].w=1; E1[num1].next=head1[u]++; head1[u]=num1++; } void add2(int u,int v) { E2[num2].u=u; E2[num2].v=v; E2[num2].w=1; E2[num2].next=head2[u]++; head2[u]=num2++; } void spfa(int s,int* head,int* dist,EDGE * E) { queue<int>Q; memset(inq,0,sizeof(inq)); dist[s]=0; inq[s]=1; Q.push(s); while (!Q.empty()) { int u=Q.front(); Q.pop(); for (int i=head[u];i+1;i=E[i].next) { int v=E[i].v; if (dist[v]>dist[u]+E[i].w) { dist[v]=dist[u]+E[i].w; if (!inq[v]) { inq[v]=1; Q.push(v); } } } } } int main() { int i,j,u,v; while (scanf("%d%d%d",&n,&m,&k)) { if (n==0&&m==0&&k==0) break; init(); init2(); for (i=0;i<m;i++) { scanf("%d%d",&u,&v); add1(u,v); add2(v,u); } memset(dist1,INF,sizeof(dist1)); memset(dist2,INF,sizeof(dist2)); spfa(1,head1,dist1,E1); spfa(n,head2,dist2,E2); for (i=0;i<num1;i++) { if (dist1[E1[i].u]+dist2[E1[i].v]<k) addedge(E1[i].u+n,E1[i].v,INF); } for (i=1;i<=n;i++) addedge(i,i+n,1); printf("%d\n",sap(n+1,n,2*n)); } return 0; }