zoukankan      html  css  js  c++  java
  • hdu 1595(最短路变形好题)

    find the longest of the shortest

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


    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
    题意:从1到n点如果切掉一条路,那么在最坏情况下的最短路是多少??
    题解:这个题开始的时候没想请,去枚举每一条边,然后超时。后来发现如果被堵死的路不是最短路中的某条,那么求的最短路肯定就还会是原来的最短路,所以我们只要枚举最短路中的每段路,然后再求最短路就行了。
    #include <stdio.h>
    #include <math.h>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <stdlib.h>
    #include <queue>
    using namespace std;
    const int N = 1005;
    const int M = 50005;
    const int INF = 999999999;
    /*struct Node{
        int u,v;
    }node[M];*/
    int graph[N][N];
    bool vis[N];
    int low[N];
    int pre[N];
    int n,m;
    int dijkstra(int s,bool flag){
        for(int i=1;i<=n;i++){
            if(flag){
                pre[i] = s;
            }
            low[i] = graph[s][i];
            vis[i] = false;
        }
        vis[s] = true;
        for(int i=1;i<n;i++){
            int Min = INF;
            for(int j=1;j<=n;j++){
                if(low[j]<Min&&!vis[j]){
                    Min = low[j];
                    s = j;
                }
            }
            vis[s] = true;
            for(int j=1;j<=n;j++){
                if(low[j]>low[s]+graph[s][j]&&!vis[j]){
                    low[j] = low[s]+graph[s][j];
                    if(flag){
                        pre[j] = s;
                    }
                }
            }
        }
        return low[n];
    }
    int main()
    {
          while(scanf("%d%d",&n,&m)!=EOF){
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    if(i==j) graph[i][j] = 0;
                    else graph[i][j] = INF;
                }
            }
            for(int i=0;i<m;i++){
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                graph[u][v]=graph[v][u] = min(w,graph[u][v]);
                //scanf("%d%d%d",&node[i].u,&node[i].v,&w);
                //graph[node[i].u][node[i].v]=graph[node[i].v][node[i].u] = min(w,graph[node[i].u][node[i].v]);
            }
            int res = dijkstra(1,1);
            int Max = -1;
            /*for(int i=0;i<m;i++){ ///枚举每一段TLE
                int temp = graph[node[i].u][node[i].v];
                graph[node[i].u][node[i].v]=graph[node[i].v][node[i].u] = INF;
                int cost = dijkstra(1);
                Max = max(cost,Max);
                graph[node[i].u][node[i].v]=graph[node[i].v][node[i].u] = temp;
            }*/
            int temp = n;
            while(temp!=1){
                int k = graph[temp][pre[temp]];
                graph[pre[temp]][temp] = graph[temp][pre[temp]] = INF;
                int cost = dijkstra(1,0);
                Max = max(cost,Max);
                graph[pre[temp]][temp] = graph[temp][pre[temp]] =k;
                temp = pre[temp];
            }
            printf("%d
    ",Max);
        }
    }

     ----update----

    利用链式前向星可以考虑到重边效果,虽然这里并没有.

    #include <string.h>
    #include <algorithm>
    #include <stdlib.h>
    #include <math.h>
    #include <stdio.h>
    #include <queue>
    using namespace std;
    const int maxn = 1005;
    const int INF = 0xfffffff;
    int n,m;
    struct Edge{
        int u,v,w;
        int next;
    }edge[maxn*maxn];
    int id[maxn*maxn]; //如果当前最短路中包含点v,那么 id[v] 则是 v 所用到的边的编号.
    int head[maxn],tot = 0;
    void addedge(int u,int v,int w,int &k){
        edge[k].v = v,edge[k].w = w;
        edge[k].next = head[u],head[u] = k++;
    }
    void init(){
        memset(head,-1,sizeof(head));
        memset(id,-1,sizeof(id));
        tot = 0;
    }
    bool vis[maxn];
    int d[maxn],pre[maxn];
    int spfa(int s,int t,bool flag){
        memset(vis,false,sizeof(vis));
        for(int i=0;i<=maxn;i++){
            d[i] = INF;
            if(flag){
                pre[i] = s;
            }
        }
        queue<int> q;
        q.push(s);
        vis[s] = true;
        d[s] = 0;
        while(!q.empty()){
            int u = q.front();
            q.pop();
            vis[u] = false;
            for(int k=head[u];k!=-1;k=edge[k].next){
                int v = edge[k].v,w=edge[k].w;
                if(d[u]+w<d[v]){
                    d[v] = d[u]+w;
                    if(!vis[v]){
                        vis[v] = true;
                        q.push(v);
                    }
                    if(flag){
                        pre[v] = u;
                        id[v] = k;
                    }
                }
            }
        }
        if(d[t]>=INF) return -1;
        return d[t];
    }
    int main(){
        while(scanf("%d %d",&n,&m)!=EOF){
            init();
            for(int i=0;i<m;i++){
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w,tot);
                addedge(v,u,w,tot);
            }
            int res;
            res = spfa(1,n,1);
            int MAX = res;
            int temp=n;
            while(temp!=1){
                int w = edge[id[temp]].w;
                edge[id[temp]].w = INF;
                MAX = max(spfa(1,n,0),MAX);
                edge[id[temp]].w = w;
                temp = pre[temp];
            }
            printf("%d
    ",MAX);
        }
        return 0;
    }
  • 相关阅读:
    伐木工和森林的故事(一)
    EclipsePDT PHP的开发环境配置
    奇怪的using
    [团队开发]SERVER2008下无法安装VS2008 SP1 和 TFS2008 SP1补丁
    写在七夕
    一点点的松懈,就可以毁掉自己!
    2008,到今天我不后悔
    细节决定成败,注意的事情需要做到,而不是听完了当耳边风
    正视差距,展望2008!
    ZendStudio5.5调式环境配置
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5511309.html
Copyright © 2011-2022 走看看