Destroying the bus stations
Destroying the bus stations
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1832 | Accepted: 595 |
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
大神的标程
1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 const int maxm=10005; 6 const int maxn=105; 7 8 struct aaa 9 { 10 int s,f,next; 11 }; 12 aaa c[maxm]; 13 int sta[maxn],fa[maxn],zh[maxn]; 14 int d[maxn][maxn],e[maxn]; 15 bool b[maxn]; 16 int n,m,now,tot; 17 bool goal; 18 void ins(int s,int f) 19 { 20 now++; 21 c[now].s=s,c[now].f=f;c[now].next=sta[s],sta[s]=now; 22 } 23 24 void bfs() 25 { 26 int i,c1,op,k,t; 27 c1=0,op=1; 28 for(i=1;i<=n;i++) 29 fa[i]=0; 30 zh[1]=1; 31 fa[1]=-1; 32 while(c1<op) 33 { 34 c1++,k=zh[c1]; 35 for(t=sta[k];t;t=c[t].next) 36 if(b[c[t].f]&&fa[c[t].f]==0) 37 { 38 zh[++op]=c[t].f; 39 fa[c[t].f]=c[t].s; 40 if(c[t].f==n) break; 41 } 42 if(fa[n]) break; 43 } 44 } 45 46 void dfs(int deep) 47 { 48 int i,c1,op,l,k; 49 if(goal) return; 50 bfs(); 51 if(fa[n]==0) 52 { 53 goal=true;return; 54 } 55 l=0; 56 for(k=n;k>1;k=fa[k]) 57 l++,d[deep][l]=k; 58 if(l>m) 59 { goal=true; 60 return; 61 } 62 if(deep>tot) return; 63 for(i=2;i<=l;i++) 64 { 65 b[d[deep][i]]=false; 66 if(e[d[deep][i]]==0) dfs(deep+1); 67 b[d[deep][i]]=true; 68 e[d[deep][i]]++; 69 } 70 for(i=2;i<=l;i++) 71 e[d[deep][i]]--; 72 } 73 74 int make() 75 { 76 int i,j; 77 goal=false; 78 for(i=0;i<=n;i++) 79 { 80 tot=i; 81 for(j=1;j<=n;j++) 82 b[j]=true; 83 memset(e,0,sizeof(e)); 84 dfs(1); 85 if(goal) return i; 86 } 87 return n; 88 } 89 90 int main() 91 { 92 int i,s,f,g; 93 while(true) 94 { 95 cin>>n>>g>>m; 96 if(n==0) break; 97 memset(sta,0,sizeof(sta)); 98 now=0; 99 for(i=1;i<=g;i++) 100 { 101 cin>>s>>f; 102 ins(s,f); 103 } 104 cout<<make()<<endl; 105 } 106 return 0; 107 }