首页 > 编程语言 > 详细

hdu 1595 最短路算法dijkstra

时间:2015-04-21 17:48:17      阅读:392      评论:0      收藏:0      [点我收藏+]

find the longest of the shortest

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


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,表示有n个城市m条路,接下来就是m行,每行三个数字表示起点终点和花费的时间,注意,路径是双向的。然后要你求最短路径,但是这m条路径终有一条路不能走,所以你要求最坏的情况下的最短路径。
思路:考虑最坏的情况下的最短路径的话,那不能走的那条路肯定在最短路径上面,所有你要找出最短路径,然后分别尝试去掉最短路径中的任意一条路,然后再求短路径,最后取最小路径中的最大值就是最坏的情况下的最短路径,本代码中使用的是dijkstra算法求最短路。
ac代码:
#include<iostream>
#include<cstdio>
#include<climits>
#include<cstring>
using namespace std;
int road[1005][1005];
int n,m;
int fa[1005];
int dist[1005];
int vis[1005];
int sum;
int Min(int a,int b)
{
    return a<b?a:b;
}
int Max(int a,int b)
{
    return a>b?a:b;
}
int dijk(int start,int last)
{
    for(int i=1;i<=n;i++)
    {
        dist[i]=INT_MAX;
    }
    dist[start]=0;
    memset(vis,0,sizeof(vis));
    for(int j=0;j<n;j++)
    {
        int m=INT_MAX,x;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&dist[i]<m)
            {
                m=dist[i];
                x=i;
            }
        }
        vis[x]=1;
        for(int i=1;i<=n;i++)
        {
            if(road[x][i]!=-1)
            {
                dist[i]=Min(dist[i],dist[x]+road[x][i]);
            }
        }
    }
    return dist[last];
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(road,-1,sizeof(road));
        for(int i=0;i<m;i++)
        {
            int start,last,temp;
            scanf("%d%d%d",&start,&last,&temp);
            road[start][last]=road[last][start]=temp;
        }
        int maxn=dijk(1,n);
        sum=0;
        int x=n;
        fa[sum++]=x;
        while(x!=1)
        {
            for(int i=1;i<=n;i++)
            {
                if(dist[i]==INT_MAX||road[i][x]==-1)continue;
                if(dist[x]==dist[i]+road[i][x])
                {
                    fa[sum++]=i;
                    x=i;
                    break;
                }
            }
        }
        for(int i=1;i<sum;i++)
        {
            int s=fa[i];
            int t=fa[i-1];
            int temp=road[s][t];
            road[s][t]=road[t][s]=-1;
            maxn=Max(maxn,dijk(1,n));
            road[s][t]=road[t][s]=temp;
        }
        printf("%d\n",maxn);
    }
    return 0;
}

 

hdu 1595 最短路算法dijkstra

原文:http://www.cnblogs.com/lthb/p/4444855.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!