• HDU 1874 畅通工程续 最短路


    畅通工程续
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
     

    Description

    某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。 

    现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

    Input

    本题目包含多组数据,请处理到文件结束。 
    每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。 
    接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。 
    再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。

    Output

    对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1. 

    Sample Input

    3 3
    0 1 1
    0 2 3
    1 2 1
    0 2
    3 1
    0 1 1
    1 2

    Sample Output

    2
    -1



    floyd算法:
    这种算法比较容易理解但是数据大的话就做不出来了
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <iomanip>
    #include <math.h>
    #include <map>
    using namespace std;
    #define FIN     freopen("input.txt","r",stdin);
    #define FOUT    freopen("output.txt","w",stdout);
    #define INF     0x3f3f3f3f
    #define lson    l,m,rt<<1
    #define rson    m+1,r,rt<<1|1
    typedef long long LL;
    
    int Map[205][205];
    int n,m;
    
    int llss(int s,int e){
        for(int z=0;z<n;z++)
           for(int i=0;i<n;i++)
               for(int j=0;j<n;j++){
                    if(Map[i][j]>Map[i][z]+Map[z][j])
                        Map[i][j]=Map[i][z]+Map[z][j];
                }
    
        if(Map[s][e]==INF)
            return -1;
        else
            return Map[s][e];
    
    }
    
    int main()
    {
        //FIN
        int st,ed,cost;
        int s,e;
        while(~scanf("%d%d",&n,&m))
        {
            memset(Map,INF,sizeof(Map));
            for(int i=0;i<n;i++){
                Map[i][i]=0;
            }
            for(int i=1;i<=m;i++){
                scanf("%d%d%d",&st,&ed,&cost);
                Map[ed][st]=Map[st][ed]=min(Map[st][ed],cost);
            }
            scanf("%d%d",&s,&e);
            int ans=llss(s,e);
            printf("%d
    ",ans);
    
        }
    }
    

      

    dijkstra算法:

    这种降低了时间复杂度

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <iomanip>
    #include <math.h>
    #include <map>
    using namespace std;
    #define FIN     freopen("input.txt","r",stdin);
    #define FOUT    freopen("output.txt","w",stdout);
    #define INF     0x3f3f3f3f
    #define lson    l,m,rt<<1
    #define rson    m+1,r,rt<<1|1
    typedef long long LL;
    
    const int MAXN=205;
    
    int Map[MAXN][MAXN];
    int dis[MAXN];
    int vis[MAXN];
    int n,m;
    
    int llss(int s,int e){
        for(int i=0;i<n;i++){
            dis[i]=Map[s][i];
            vis[i]=0;
        }
        vis[s]=1;
        dis[s]=0;
    
        int temp,k;
    
        for(int i=0;i<n;i++){
            temp=INF;
            for(int j=0;j<n;j++){
                if(vis[j]==0&&temp>dis[j]){
                    k=j;
                    temp=dis[j];
                }
            }
            if(temp==INF)  break;
            vis[k]=1;
            for(int j=0;j<n;j++){
                if(dis[j]>dis[k]+Map[k][j])  dis[j]=dis[k]+Map[k][j];
            }
    
        }
        if(dis[e]==INF)  return -1;
        else return dis[e];
    
    
    }
    
    int main()
    {
        //FIN
        int st,ed,cost;
        int s,e;
        while(~scanf("%d%d",&n,&m))
        {
            memset(Map,INF,sizeof(Map));
            for(int i=0;i<n;i++){
                Map[i][i]=0;
            }
            for(int i=1;i<=m;i++){
                scanf("%d%d%d",&st,&ed,&cost);
                Map[ed][st]=Map[st][ed]=min(Map[st][ed],cost);
            }
            scanf("%d%d",&s,&e);
            int ans=llss(s,e);
            printf("%d
    ",ans);
    
        }
    }
    

      

  • 相关阅读:
    你最喜欢的程序员漫画
    编程名言名句
    查看一个数是不是2的n次方
    程序员需要有多懒 ?- cocos2d-x 数学函数、常用宏粗整理
    C++中string、int、char之间转换
    cocos2d-x 中使用 srand((unsigned)time(NULL))重新设置一个随机种子
    ASP.NET MVC SignalR 配合VUE
    VS Visual Studio光标无法自动到行尾,点哪里就在哪里解决办法
    .NET Core Entity Framework 代码优先配置
    Image Bitmap转MemoryStream后,上传遇到问题,报Index异常
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5751171.html
Copyright © 2020-2023  润新知