首页 > ACM题库 > HDU-杭电 > HDU 1595 find the longest of the shortest-最短路径-[解题报告] C++
2013
12-12

HDU 1595 find the longest of the shortest-最短路径-[解题报告] C++

find the longest of the shortest

问题描述 :

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.

输入:

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.

输出:

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

样例输入:

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

样例输出:

11
13
27

转载请注明出处,谢谢 http://blog.csdn.net/ACM_cxlove?viewmode=contents           by—cxlove

题意是说任意去掉一条边之后的最短路的最长长度为多少。

顶点数为N达到1000的范围,显然枚举所有边再求最短路,复杂度为N^2*O(spfa),会超时的。

仔细想想便知,原图上求一次最短路,如果我们删掉最短路之外的边,之后的最短路还是原图的最短路,那我们只要枚举最短路上的边,然后再求最短路就行了。

最多路径上会有N条边,复杂度为N*O(spfa),1S左右AC。写的代码很囧,特别是记录最短路径上的边的时候,有许多地方可以改进,之前WA了,是因为我只删掉了单向边,因为是无向图,所以需要双向都删除,我找了两次,其实u->v,和v->u的边在前向星中的存储必然是相邻的,idx^1便是另外一条边的位置。懒得改了

 

/*ID:cxlove
PROB:HDU 1595
DATA:2012.4.4
HINT:最短路径+枚举最短路径上的边
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define inf 1<<30
using namespace std;
struct Node{
    int u,v,w,next;
}edge[1000005];
struct EDGE{
    int u,v,w,idx1,idx2;
}path[1005];
int start[1005],pre[1005],dist[1005];
int n,m,cnt,tot;
void addedge(int u,int v,int w){
    edge[cnt].u=u;edge[cnt].v=v;edge[cnt].w=w;
    edge[cnt].next=start[u];start[u]=cnt++;
}
void Init(){
    int u,v,w;
    cnt=0;tot=0;
    memset(start,-1,sizeof(start));
    memset(pre,-1,sizeof(pre));
    while(m--){
        scanf("%d%d%d",&u,&v,&w);
        addedge(u,v,w);
        addedge(v,u,w);
    }
}
void spfa(){
    bool flag[1005];
    for(int i=1;i<=n;i++)
        dist[i]=inf;
    memset(flag,false,sizeof(flag));
    queue<int >que;
    que.push(1);
    flag[1]=true;
    dist[1]=0;
    while(!que.empty()){
        int u=que.front();
        que.pop();
        flag[u]=false;
        for(int i=start[u];i!=-1;i=edge[i].next){
            int v=edge[i].v,w=edge[i].w;
            if(dist[v]>dist[u]+w){
                dist[v]=dist[u]+w;
                pre[v]=u;
                if(!flag[v]){
                    flag[v]=true;
                    que.push(v);
                }
            }
        }
    }
}
void find_path(){
    int v=n;
    while(pre[v]!=-1){
        path[tot].u=pre[v];
        path[tot].v=v;
        path[tot].w=dist[v]-dist[pre[v]];
        int k=start[pre[v]];
        while(edge[k].v!=v)
            k=edge[k].next;
        path[tot].idx1=k;
        k=start[v];
        while(edge[k].v!=pre[v])
            k=edge[k].next;
        path[tot++].idx2=k;
        v=pre[v];
    }
}
void slove(){
    int ans=0;
    for(int i=0;i<tot;i++){
        int idx1=path[i].idx1,idx2=path[i].idx2;
        int t1=edge[idx1].w,t2=edge[idx2].w;
        edge[idx1].w=inf;edge[idx2].w=inf;
        spfa();
        ans=max(ans,dist[n]);
        edge[idx1].w=t1;edge[idx2].w=t2;
    }
    printf("%d\n",ans);
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        Init();
        spfa();;
        find_path();
        slove();
    }
    return 0;
}

 

解题报告转自:http://blog.csdn.net/acm_cxlove/article/details/7426697


  1. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?

  2. 你的理解应该是:即使主持人拿走一个箱子对结果没有影响。这样想,主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率,但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3

  3. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks