POJ3921Destroying the bus stations

一叶落寞,万物失色。

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
题意:给你一个图(可能有重边),要求你破坏一些节点使得从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;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值