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://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, include
, 最短路径条数
, 路径
, and
, The
, v5.6.5
, V3.4.4
that
shortest、shortest path、graphshortestpath、shortest path tree、k shortest path,以便于您获取更多的相关知识。

时间: 2024-08-31 01:03:23

HDU 1595 find the longest of the shortest(枚举,最短路)的相关文章

hdu 1595 find the longest of the shortest

点击打开链接hdu 1595 思路:最短路+优先队列+Dijstra+枚举边 分析: 1 题目要求的是删掉一条边之和求出的最短路中的最大值. 2 很明显,肯定是要先求出原图的最短路并且记录父亲节点.现在我们可以想,如果要枚举所有的边,显然这个是不可能的实现的.所以我们仔细分析可以知道其实能够对最短路产生影响的就是原图最短路上的边,所以我们只需要去枚举删除最短路径上面边然后求最短路即可,最后得到ans 3 这一题的n <= 1000 , m<=n*(n-1)/2 , 刚开始我用的SPFA,然后就

HDU 1535 Invitation Cards:多源点到单点最短路

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 题目: Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1044    Accepted Submission(s): 459 Problem Description In the age of te

HDU 1598 find the most comfortable road (枚举+Kruskal)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 题目: Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的"舒适度"有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,fl

HDOJ(HDU) 1562 Guess the number(水题,枚举就行)

Problem Description Happy new year to everybody! Now, I want you to guess a minimum number x betwwn 1000 and 9999 to let (1) x % a = 0; (2) (x+1) % b = 0; (3) (x+2) % c = 0; and a, b, c are integers between 1 and 100. Given a,b,c, tell me what is the

HDU 3986 Harry Potter and the Final Battle

链接: http://acm.hdu.edu.cn/showproblem.php?pid=3986 题目: Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1139    Accepted Submission(s): 359 Problem Description

最短路专题【完结】

第一题 hdu 1317 XYZZY 点击打开hdu 1317 思路: 1 题目的图是一个有向图,并且可能存在环.第一个点的能量值为100,边的权值利用能量大小,例如2点为-60,如果1->2那么value[1][2] = -602 题目明确指出如果是要win的话,那么必须是经过的每条边都要大于0.那么我们只要把那些经过松弛操作后的点大于0的入队即可,小于等于0的点肯定不会出现在最终的路径上.3 如果存在正环的话,那么就有能量值无限大,那么这个时候只要判断这个点能否到达n4 判断是否是有环还是五

HDU 2363 Cycling:二分+枚举+限制最短路,好题

链接: http://acm.hdu.edu.cn/showproblem.php?pid=2363 题目大意: 小明从家里走到学校去考试, 路上有各个交叉点,它们有自己的海拔高度. 小明从家里走到学校的路上,必然会经过不同的交叉点,因此会不断的走上走下,忐忐忑忑,这让他很不安,会影响他考试的发挥.因此,他要选择一条起伏最小的路去学校.所谓的"起伏最小",是指这条路上海拔最高的点与海拔最低的点的差值最小. 在起伏最小的前提下,还要求出路程距离最短. 分析与总结: 这题让我想起以前做过的

OSPF 开放式最短路径优先协议

今天偶尔跟同事聊到网络的问题,这一阵子忙的,把学过的东西都忘得迷迷糊糊的了,今晚回来重新把这个点先整理下来,待我有时间再把这块串成线. 看了一篇很有意思的小文章,转载过来,比我们老师讲的有意思,不过哈哈,当年茱莉雅老师给我们讲的也不差.嘿嘿....来,先看个小童话: http://kingdee.blog.51cto.com/98119/27310 可以把整个网络(一个自治系统AS)看成一个王国,这个王国可以分成几个 区(area),现在我们来看看区域内的某一个人(你所在的机器root)是怎样得

HDU 2807 The Shortest Path:最短路+矩阵快速比较

链接: http://acm.hdu.edu.cn/showproblem.php?pid=2807 题目: The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1421    Accepted Submission(s): 436 Problem Description There are N citi