一叶落寞,万物失色。
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到n得路径长度大于一个数k,求出最小破坏得点数(不能破坏1和n题目保证没有直接从1到n的路径,路径得权值恒为1)
解题思路:首先我们可以求出这个图中1到n的最短路径(如果最短路径都大于K啦那么就可以啦),因为边权恒为1所以直接BFS就好啦,然后我们保存他最短路径的路线,在来暴力破坏哪些点可以最少
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#define ll long long
#define oo 1000000000
#define pi acos(-1)
using namespace std;
int n,m,k,pre[55];
bool note[55],arc[55][55];
queue<int> myqueue;
bool BFS()
{
int h,i,dis[55];
memset(dis,0x7f,sizeof(dis));
while (!myqueue.empty()) myqueue.pop();
myqueue.push(1);
dis[1]=0;
while (!myqueue.empty())
{
h=myqueue.front();
myqueue.pop();
if (h==n) break;
for (i=1;i<=n;i++)
if (arc[h][i] && !note[i] && dis[i]>oo)
{
dis[i]=dis[h]+1;
pre[i]=h;
myqueue.push(i);
}
}
if (dis[n]>k) return true;
return false;
}
bool FindAns(int p)
{
int way[55],i,m;
if (BFS()) return true;
if (!p) return false;
i=pre[n];
m=0;
while (i!=1)
{
way[++m]=i;
i=pre[i];
}
for (i=m;i>=1;i--)
{
note[way[i]]=true;
if (FindAns(p-1)) return true;
note[way[i]]=false;
}
return false;
}
int main()
{
int p,x,y,ans;
while (~scanf("%d%d%d",&n,&m,&k))
{
if (!n && !m && !k) break;
memset(arc,false,sizeof(arc));
while (m--)
{
scanf("%d%d",&x,&y);
arc[x][y]=true;
}
for (ans=0;ans<n;ans++)
{
memset(note,false,sizeof(note));
if (FindAns(ans)) break;
}
printf("%d\n",ans);
}
return 0;
}