HDU 2485 Destroying the bus stations

Destroying the bus stations

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2492    Accepted Submission(s): 813


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
 
解题:直接跑费用流,直到最短路大于k
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = 0x3f3f3f3f;
 4 const int maxn = 500;
 5 struct arc{
 6     int to,flow,cost,next;
 7     arc(int x = 0,int y = 0,int z = 0,int nxt = -1){
 8         to = x;
 9         flow = y;
10         cost = z;
11         next = nxt;
12     }
13 }e[maxn*maxn];
14 int head[maxn],d[maxn],p[maxn],tot,S,T,n,m,k;
15 void add(int u,int v,int flow,int cost){
16     e[tot] = arc(v,flow,cost,head[u]);
17     head[u] = tot++;
18     e[tot] = arc(u,0,-cost,head[v]);
19     head[v] = tot++;
20 }
21 bool spfa(){
22     memset(d,0x3f,sizeof d);
23     memset(p,-1,sizeof p);
24     queue<int>q;
25     bool in[maxn] = {};
26     q.push(S);
27     d[S] = 0;
28     while(!q.empty()){
29         int u = q.front();
30         q.pop();
31         in[u] = false;
32         for(int i = head[u]; ~i; i = e[i].next){
33             if(e[i].flow && d[e[i].to] > d[u] + e[i].cost){
34                 d[e[i].to] = d[u] + e[i].cost;
35                 p[e[i].to] = i;
36                 if(!in[e[i].to]){
37                     in[e[i].to] = true;
38                     q.push(e[i].to);
39                 }
40             }
41         }
42     }
43     return p[T] > -1 && d[T] <= k;
44 }
45 int solve(int ret = 0){
46     while(spfa()){
47         int minF = INF;
48         for(int i = p[T]; ~i; i = p[e[i^1].to])
49             minF = min(minF,e[i].flow);
50         for(int i = p[T]; ~i; i = p[e[i^1].to]){
51             e[i].flow -= minF;
52             e[i^1].flow += minF;
53         }
54         ret += minF;
55     }
56     return ret;
57 }
58 int main(){
59     int u,v;
60     while(scanf("%d%d%d",&n,&m,&k) == 3 && (n||m||k)){
61         memset(head,-1,sizeof head);
62         tot = 0;
63         for(int i = 1; i <= n; ++i) add(i,i + n,1,0);
64         for(int i = 0; i < m; ++i){
65             scanf("%d%d",&u,&v);
66             add(u + n,v,INF,1);
67         }
68         S = 1 + n;
69         T = n;
70         printf("%d\n",solve());
71     }
72     return 0;
73 }
View Code

 可是貌似是错误的

10 11 5

1 2
2 3
3 4
4 5
5 10
2 9
1 6
6 7
7 8
8 9
9 10

据说只能暴力,这组数据过不了

posted @ 2015-11-14 18:50  狂徒归来  阅读(450)  评论(0编辑  收藏  举报