zoukankan      html  css  js  c++  java
  • HDU-1595Find the longest of shortest(最短路径的最长路Dijkstra+记录路径)

    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.

    InputEach 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.OutputIn 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

    题解:本题是让求去掉最短路上的其中一条后的最短路径;我们可以先处理处最短路径(Dijkstra)并记录所经过的节点,然后依次去掉最短路径上的每一条线段,再Dijkstra,找到最小值即可;

    AC代码为:

    #include<bits/stdc++.h> 
    using namespace std; 
    const int INF=0x3f3f3f3f;  
    int N,M,U,V,W,dis[1010],fa[1010],Map[1010][1010],vis[1010]; 
    int Dijkstra(int temp) 
    { 
        memset(vis,0,sizeof vis); 
        memset(dis,INF,sizeof dis);
        dis[1]=0; 
        for(int i=1;i<=N;i++) 
        { 
            int min_dis=INF,u=-1; 
            for(int j=1;j<=N;j++) 
            { 
                if(!vis[j] && dis[j]<min_dis) 
                { 
                    min_dis=dis[j]; u=j; 
                } 
            } 
            vis[u]=1; 
            for(int j=1;j<=N;j++) 
            { 
                if(!vis[j]&&dis[j]>Map[u][j]+dis[u])  
                { 
                    dis[j]=Map[u][j]+dis[u]; 
                    if(temp) fa[j]=u; 
                } 
            } 
        } 
        return dis[N]; 
    } 
    int main() 
    { 
        ios::sync_with_stdio(false); cin.tie(0); 
        while(cin>>N>>M) 
        { 
            for(int i=1;i<=N;i++) 
            { 
                for(int j=1;j<=N;j++) 
                    i==j? Map[i][j]=0:Map[i][j]=INF; 
            } 
            for(int i=1;i<=M;i++)  
            { 
                cin>>U>>V>>W; 
                Map[U][V]=Map[V][U]=W; 
            } 
            int Max=Dijkstra(1),x=N; 
            while(x!=1) 
            { 
                int flag=Map[x][fa[x]]; 
                Map[x][fa[x]]=Map[fa[x]][x]=INF; 
                Max=max(Max,Dijkstra(0));  
                Map[x][fa[x]]=Map[fa[x]][x]=flag; x=fa[x]; 
            } 
            cout<<Max<<endl; 
        } 
        return 0; 
    }
    View Code
  • 相关阅读:
    广度优先搜索-八数码问题
    广度优先搜索-鸣人和佐助
    广度优先搜索-迷宫问题
    广度优先搜索-抓住那头牛
    Unity面试题汇总(第一部分)
    独立项目-Socket通讯 应用/客户端和服务器的简单通讯-04
    独立项目-Socket通讯 服务器端代码-04
    独立项目-Socket通讯 客户端代码-03
    独立项目-Socket通讯 发送数据包和接收数据包过程图-02
    独立项目-Socket通讯 服务器端架构图-01
  • 原文地址:https://www.cnblogs.com/csushl/p/9386764.html
Copyright © 2011-2022 走看看