hdu 1595 最短路算法dijkstra

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;
}
时间: 2024-10-10 19:46:52

hdu 1595 最短路算法dijkstra的相关文章

hdu 2544 最短路 (dijkstra,floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目大意:找到两点间最短的距离值. 代码一:(dijkstra算法) 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int n,Min,node[105],visit[105],map[105][105]; 5 void set() 6 { 7 for (int i=1; i<=n; i

hdu 1595 最短路

题意是要我们求出最短路中一条路坏的情况下最大的时间: 我们先将最短路求出并记录路径,然后枚举最短路中每一条路坏的情况,求出最大的时间.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<cmath> using namespace st

最短路算法(dijkstra,bellman_ford,floyd)

最短路算法 dijkstra(初级的最短路算法,适合稠密图,可用邻接表优化) bool relax(int u,int v) { double tmp=max(dist[u],edge[u][v]); if(tmp<dist[v]){ dist[v]=tmp; } } void dijkstra() { memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++){ int x; double mindist=INF; for(int j=0;j<n;j

hdu 2544 最短路 (dijkstra算法)

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 34855    Accepted Submission(s): 15096 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

HDU 2544 最短路(Dijkstra算法)

分析:模板题 1 #include"iostream" 2 #define INF 65535 3 using namespace std; 4 const int maxn=100+10; 5 int w[maxn][maxn],d[maxn],n,m; 6 void Dijkstra() 7 { 8 int final[maxn]; 9 for(int i=1;i<=n;i++) 10 { 11 final[i]=0; 12 d[i]=w[1][i]; 13 } 14 fin

最短路算法 —— Dijkstra算法

用途: 解决单源最短路径问题(已固定一个起点,求它到其他所有点的最短路问题) 算法核心(广搜): (1)确定的与起点相邻的点的最短距离,再根据已确定最短距离的点更新其他与之相邻的点的最短距离. (2)之后的更新不需要再关心最短距离已确定的点 三种实现模板: 一.矩阵朴素版 二.vector简单版 三.静态邻接表有点复杂版 1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #includ

图的最短路算法 Dijkstra及其优化

单源最短路径算法 时间复杂度O(N2) 优化后时间复杂度为O(MlogN)(M为图中的边数 所以对于稀疏图来说优化后更快) 不支持有负权的图 #include<iostream> using namespace std; const int maxn=1024; const int inf=1<<30; int n,m; int d[maxn]; int v[maxn]; int G[maxn][maxn]; void init() { for(int i=1;i<=n;i+

ACM: HDU 2544 最短路-Dijkstra算法

HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M<

HDU 2544 最短路(我的dijkstra算法模板、SPAFA算法模板)

思路:这道题是基础的最短路径算法,可以拿来试一下自己对3种方法的理解 dijkstra主要是从第一个点开始枚举,每次枚举出当当前最小的路径,然后再以那最小的路径点为起点,求出它到其它未标记点的最短距离 bellman-ford 算法则是假设有向网中有n 个顶点.且不存在负权值回路,从顶点v1 和到顶点v2 如果存在最短路径,则此路径最多有n-1 条边.这是因为如果路径上的边数超过了n-1 条时,必然会重复经过一个顶点,形成回路:而如果这个回路的权值总和为非负时,完全可以去掉这个回路,使得v1到v