zoukankan      html  css  js  c++  java
  • hdu1595 最短路问题(dijkstra&&spfa)

    find the longest of the shortest

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


    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
     
    Author
    ailyanlu
     
    Source
    本题的大概题意是先求出最短路,之后再最短路中依次删掉每一条边,再求最短路,取最长的便是结果
    dijkstra算法代码实现
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=1005;
    const int INF=0x7fffffff;
    int map[maxn][maxn];
    bool vis[maxn];
    int pre[maxn];
    int n,m;
    int dis[maxn];
    void dijkstra(int start){
        for(int i=1;i<=n;i++)
            dis[i]=INF;
        memset(vis,false,sizeof(vis));
        dis[1]=0;
        for(int i=1;i<=n;i++){
            int k=-1;
            int tmin=INF;
            for(int j=1;j<=n;j++){
                if(!vis[j]&&dis[j]<tmin){
                   tmin=dis[j];
                   k=j;
                }
            }
    
            vis[k]=true;
            for(int j=1;j<=n;j++){
                if(map[k][j]!=INF)
                if(!vis[j]&&dis[k]+map[k][j]<dis[j]){
                         dis[j]=dis[k]+map[k][j];
                       if(start)
                           pre[j]=k;
                }
            }
        }
    }
    
    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)
                        map[i][j]=0;
                    else
                        map[i][j]=map[j][i]=INF;
               }
            }
    
            int u,v,w;    
           for(int i=1;i<=m;i++){
             scanf("%d%d%d",&u,&v,&w);
              map[u][v]=map[v][u]=w;
           }
           memset(pre,0,sizeof(pre));
           dijkstra(1);
           int ans=dis[n];
        //   printf("---->%d
    ",ans);
            for(int i=n;i!=1;i=pre[i]){
                int temp=map[i][pre[i]];
                map[i][pre[i]]=INF;
                map[pre[i]][i]=INF;
                dijkstra(0);
                if(dis[n]>ans)
                ans=dis[n];
             //   printf("--->%d
    ",temp);
                 map[i][pre[i]]=temp;
                 map[pre[i]][i]=temp;
            } 
            printf("%d
    ",ans);
    
    
       }
       return 0;   
    }

     spfa算法实现

    #include<stdio.h>
    #include<queue>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<vector>
    using namespace std;
    const int MAXN=1010;
    const int INF=0x7fffffff;
    struct Edge
    {
        int v;
        int cost;
        Edge(int _v=0,int _cost=0):v(_v),cost(_cost) {}
    };
    vector<Edge>E[MAXN];
    void addedge (int u,int v,int w)
    {
        E[u].push_back(Edge(v,w));
        E[v].push_back(Edge(u,w));
    }
    bool vis[MAXN];//在队列标志
    int dist[MAXN];
    int pre[MAXN];
    int n,m;
    void  spfa(int x,int y,int judge)
    {
        memset(vis,false,sizeof(vis));
        for(int i=1; i<=n; i++)
            dist[i]=INF;
        vis[1]=true;
        dist[1]=0;
        queue<int>que;
        while(!que.empty())
            que.pop();
        que.push(1);
        while(!que.empty())
        {
            int u=que.front();
            que.pop();
            vis[u]= false;
            for(int i=0; i<E[u].size(); i++)
            {
                int v=E[u][i].v;
                if((u==x&&v==y)||(u==y&&v==x))
                    continue;
                if(dist[v]>dist[u]+E[u][i].cost)
                {
                    dist[v]=dist[u]+E[u][i].cost;
                    if(judge)
                        pre[v]=u;
                    if(!vis[v])
                    {
                        vis[v]= true;
                        que.push(v);
                    }
                }
            }
        }
     
    }
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
           for(int i=1;i<=n;i++)
               E[i].clear();
           int u,v,w;
            for(int i=1;i<=m;i++ ){
               scanf("%d%d%d",&u,&v,&w);
               addedge(u,v,w);
            }
            memset(pre,0,sizeof(pre));
            spfa(0,0,1);
         int   ans=dist[n];
            for(int i=n;i!=1;i=pre[i]){
                 spfa(i,pre[i],0);
            int temp=dist[n];
                  if(temp>ans)
                      ans=temp;
            }
            printf("%d
    ",ans);
        } 
        return 0;
    }
  • 相关阅读:
    Java Applet实现五子棋游戏
    CrawlScript脚本语言实现网络爬虫
    Eclipse集成Git的实践
    以图表形式分析和统计数据
    爬虫抓取分页数据的简单实现
    爬虫的简单实现
    使用htmlparser爬虫技术爬取电影网页的全部下载链接
    使用webcollector爬虫技术获取网易云音乐全部歌曲
    百度地图API-覆盖物
    百度地图API-控件
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4820090.html
Copyright © 2011-2022 走看看