Destroying the bus stations
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2930 Accepted Submission(s): 979
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
题目大意:给定一个图,每条边的边权为1,要求你切掉一些边,使得切掉后的最短路长度大于k,保证有解。
思路:最短路选择spfa,而减去哪些边则应该搜索,而全部搜索太大,就选择迭代加深搜索。第一次写迭代加深,wa了好多次,也有莫名的TLE。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int MAXN=57;
struct edge
{
int next,node;
};
edge e[MAXN*MAXN];
int head[MAXN],tot;
int dist[MAXN],pre[MAXN];
bool inqueue[MAXN],go[MAXN];
int n,m,k;
void addedge(int u,int v)
{
e[tot].next=head[u];
e[tot].node=v;
head[u]=tot++;
}
void spfa(int u)
{
memset(inqueue,0,sizeof(inqueue));
memset(dist,0x3f,sizeof(dist));
queue <int> que;
dist[u]=0;
que.push(u);
inqueue[u]=true;
while (!que.empty())
{
int x=que.front();
que.pop();
inqueue[u]=false;
for (int i=head[x];i!=-1;i=e[i].next)
{
int v=e[i].node;
if (inqueue[v] || go[v])
{
continue;
}
if (dist[v]>dist[x]+1)
{
dist[v]=dist[x]+1;
inqueue[v]=true;
que.push(v);
pre[v]=x;
}
}
}
}
bool p=false;
int path[MAXN][MAXN];
void DFS(int size)
{
spfa(1);
if (dist[n]>k)
{
p=true;
return ;
}
if (size==0)
{
return ;
}
int cnt=0;
for (int i=n;i>1;i=pre[i])
{
path[size][cnt++]=i;
}
for (int i=1;i<cnt;i++)
{
int index=path[size][i];
if (go[index])
{
continue;
}
go[index]=true;
DFS(size-1);
go[index]=false;
}
}
int main()
{
int u,v;
while (~scanf("%d%d%d",&n,&m,&k),n+m+k)
{
tot=0;
memset(head,-1,sizeof(head));
for (int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
}
p=false;
for (int i=0;i<n;i++)
{
DFS(i);
if (p)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}