HDU 1595 find the longest of the shortest(枚举,最短路)

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1595


题目:

find the longest of the shortest

Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 667    Accepted Submission(s): 220


Problem Description
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she doesn't live in the same city, she started preparing for the long journey.We know for every road how many minutes it takes to come from one city to another.
Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road. It is possible to come from Marica's city to Mirko's no matter which road is closed.
Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.
 

Input
Each case there are two numbers in the first row, N and M, separated by a single space, the number of towns,and the number of roads between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith numbers from 1 to N, Mirko is located in city 1, and Marica in city N.
In the next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤ V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B, and that it is crossable in V minutes.
 

Output
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
 

Sample Input
  
  
5 6 1 2 4 1 3 3 2 3 1 2 4 4 2 5 7 4 5 1 6 7 1 2 1 2 3 4 3 4 4 4 6 4 1 5 5 2 5 2 5 6 5 5 7 1 2 8 1 4 10 2 3 9 2 4 10 2 5 1 3 4 7 3 5 10
 

Sample Output
  
  
11 13 27
 


题目大意:
有一城市,这个城市有n个地点和m条连接他们的路,点的编号是从1到n,小X住在1,他想去n。
但是最近正在维修公路,也就是说这m条路有且只有一条是坏的,但是小X不知道是哪一条,一条很关键的路坏了路程就会增加很多,所以小X想知道从1到n *最*坏*情*况* 下的路程。你能帮助他吗?


分析与总结:

1.  最直接的想法,就是枚举每一条给的路径,把这条路径“删掉",删掉可以把邻接矩阵对应的边赋值为INF,然后依次求最短路。

但是所有的边数太多了,有1000*999/2条, 不用想了肯定是超时的。

所以选择删哪些边是最重要的。


2.  只需要先求一次最短路,记录好路径,然后枚举删除这条路径上的边,再求最短路,所有最短路中最大的那个就是答案。这样最多就只需要删除n次就可以了。为什么只要枚举的是最短路上的边就行了呢?很简单,如果枚举的是其他边,那么将会毫无影响,再次进行最短路算法时,得出的还是和原来的最短路一样。记录路径的方法是在松弛的时候用一个数组pre记录。



代码:

#include<cstring>
#include<cstdio>
#include<iostream>
#include<queue>
#include<utility>
using namespace std;

typedef pair<int,int>pii;
const int INF = 1000000000;
const int VN  = 1005;
const int EN  = VN*VN/2;

int n, m;
int d[VN];
int w[VN][VN];
int pre[VN];
bool inq[VN];
bool flag;

void init(){
    flag=true;
    memset(pre, -1, sizeof(pre));
    for(int i=0; i<=n; ++i){
        w[i][i] = INF;
        for(int j=i+1; j<=n; ++j)
            w[i][j] = w[j][i] = INF;
    }
}

void Dijkstra(int src){
    int tmp_pre[VN];
    memset(tmp_pre, -1, sizeof(tmp_pre));
    memset(inq, 0, sizeof(inq));
    for(int i=1; i<=n; ++i)d[i]=INF;
    d[src] = 0;
    priority_queue<pii,vector<pii>,greater<pii> >q;
    q.push(make_pair(d[src],src));
    while(!q.empty()){
        pii x=q.top(); q.pop();
        int u=x.second;
        if(d[u]!=x.first) continue;
        for(int v=1; v<=n; ++v){
            int tmp=d[u]+w[u][v];
            if(w[u][v]!=INF && d[v]>tmp){
                tmp_pre[v] = u;
                d[v] = tmp;
                q.push(make_pair(d[v],v));
            }
        }
    }
    if(flag){
        memcpy(pre, tmp_pre, sizeof(tmp_pre));
        flag=false;
    }
}

int main(){
    int u,v,c;
    while(~scanf("%d%d",&n,&m)){
        init();
        for(int i=0; i<m; ++i){
            scanf("%d%d%d",&u,&v,&c);
            if(w[u][v]>c) w[u][v]=w[v][u]=c;
        }

        Dijkstra(1);

        int j=n, ans=-INF;

        while(pre[j]!=-1){
            int cost = w[j][pre[j]]; //备份
            w[j][pre[j]] = w[pre[j]][j] = INF;

            Dijkstra(1);
            if(d[n]!=INF && d[n]>ans)ans=d[n];

            w[j][pre[j]] = w[pre[j]][j] = cost;
            j = pre[j];
        }
        printf("%d\n", ans);
    }
    return 0;
}


 
 

——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值