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): 1130    Accepted Submission(s): 333


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
 
代码:
View Code
#include <iostream>
#include <cstring>

using namespace std;
const int maxm=10005;
const int maxn=105;

struct aaa
{
int s,f,next;
};
aaa c[maxm];
int sta[maxn],fa[maxn],zh[maxn];
int d[maxn][maxn],e[maxn];
bool b[maxn];
int n,m,now,tot;
bool goal;
void ins(int s,int f)
{
now++;
c[now].s=s,c[now].f=f;c[now].next=sta[s],sta[s]=now;
}

void bfs()
{
int i,c1,op,k,t;
c1=0,op=1;
for(i=1;i<=n;i++)
fa[i]=0;
zh[1]=1;
fa[1]=-1;
while(c1<op)
{
c1++,k=zh[c1];
for(t=sta[k];t;t=c[t].next)
if(b[c[t].f]&&fa[c[t].f]==0)
{
zh[++op]=c[t].f;
fa[c[t].f]=c[t].s;
if(c[t].f==n) break;
}
if(fa[n]) break;
}
}

void dfs(int deep)
{
int i,c1,op,l,k;
if(goal) return;
bfs();
if(fa[n]==0)
{
goal=true;return;
}
l=0;
for(k=n;k>1;k=fa[k])
l++,d[deep][l]=k;
if(l>m)
{ goal=true;
return;
}
if(deep>tot) return;
for(i=2;i<=l;i++)
{
b[d[deep][i]]=false;
if(e[d[deep][i]]==0) dfs(deep+1);
b[d[deep][i]]=true;
e[d[deep][i]]++;
}
for(i=2;i<=l;i++)
e[d[deep][i]]--;
}

int make()
{
int i,j;
goal=false;
for(i=0;i<=n;i++)
{
tot=i;
for(j=1;j<=n;j++)
b[j]=true;
memset(e,0,sizeof(e));
dfs(1);
if(goal) return i;
}
return n;
}

int main()
{
int i,s,f,g;
while(true)
{
cin>>n>>g>>m;
if(n==0) break;
memset(sta,0,sizeof(sta));
now=0;
for(i=1;i<=g;i++)
{
cin>>s>>f;
ins(s,f);
}
cout<<make()<<endl;
}
return 0;
}

转载于:https://www.cnblogs.com/one--world--one--dream/archive/2011/12/28/2305334.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值