HDU2485 Destroying the bus stations 迭代加深搜索

ACM-ICPC训练题解 同时被 3 个专栏收录
241 篇文章 5 订阅
11 篇文章 0 订阅
7 篇文章 0 订阅

Destroying the bus stations

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3140    Accepted Submission(s): 1045


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

题解:询问最少删除几个点,使得从1到n的最短路的长度超过k。

这中套路我们应该往迭代加深的方向去想,从0到n-1设置迭代深度。对于每一个深度,删除最短路径上的一个点,再跑最短路,再删除点,直到深度为0,如果最后跑出来的最短路长度>k,那么答案就是当前的深度。否则的话,我们再把初始迭代深度+1,重复上述操作。

代码:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 55;
int head[MAXN];
int cnt;
struct edge{ 
    int v; 
    int next; 
    int cost; 
}Es[4000];  
void init(){ 
    cnt = 0; 
    memset(head,-1,sizeof(head)); 
}
inline void add_edge(int i,int j,int cost){   
    Es[cnt].v = j; 
    Es[cnt].cost = cost; 
    Es[cnt].next = head[i]; 
    head[i] = cnt++; 
} 
int flag;
int n,m,k;
int d[MAXN],drop[MAXN],inq[MAXN],pre[MAXN][MAXN];
void spfa(int s,int* p){
    memset(d,0x3f,sizeof(d));
    memset(inq,0,sizeof(inq));
    queue<int> Q;
    Q.push(s);
    d[s] = 0;
    inq[s] = 1;
    while(!Q.empty())
    {
        int u = Q.front();Q.pop();
        inq[u] = 0;
        for(int e = head[u];e != -1;e = Es[e].next){
            int v = Es[e].v;
            int c = Es[e].cost;
            if(drop[v]) continue;
            if(d[v] > d[u] + c){
                d[v] = d[u] + c;
                p[v] = u;
                if(!inq[v]){
                    inq[v] = 1;
                    Q.push(v);
                }
            }
        }
    }    
}
void dfs(int dep){
    spfa(1,pre[dep]);
    if(d[n] > k) {
        flag = 1;
        return ;
    }
    if(!dep) return;
    for(int pa = pre[dep][n];pa != 1;pa = pre[dep][pa])
    {
        if(drop[pa]) continue;
        drop[pa] = 1;
        dfs(dep-1);
        if(flag) return;
        drop[pa] = 0;
        //pa = pre[dep][pa];
    }
    
}
int main(){
    while(scanf("%d%d%d",&n,&m,&k) != EOF && n+m+k){
        init();
        memset(drop,0,sizeof(drop));
        for(int i = 1;i <= m;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            add_edge(a,b,1);
        }
        flag = 0;
        for(int i = 0;i < n;i++){
            dfs(i);
            if(flag){
                printf("%d\n",i);
                break;
            }    
        }
    }
    return 0;
} 



  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值