zoukankan      html  css  js  c++  java
  • 最短路

    最短路

    Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 25   Accepted Submission(s) : 18

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    Problem Description

    在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

    Input

    输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
    输入保证至少存在1条商店到赛场的路线。

    Output

    对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间

    Sample Input

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

    Sample Output

    3
    2
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    #include <queue>
    
    using namespace std;
    
    #define INF 0xfffffff
    
    #define N 1002
    
    bool vis[N];
    int n, m, dist[N], G[N][N];
    
    void Init()
    {
        memset(vis, false, sizeof(vis));
    
        for(int i = 0; i <= n; i++)
        {
            dist[i] = INF;
    
            for(int j = 0; j <= n; j++)
                G[i][j] = G[j][i] = INF;
        }
    }
    
    int dij(int s, int e)
    {
        dist[s] = 0;
    
        for(int i = 0; i <= n; i++)
        {
            int index = 0, Min = INF;
    
            for(int j = 0; j <= n; j++)
            {
                if(!vis[j] && dist[j] < Min)
                    Min = dist[j], index = j;
            }
    
            vis[index] = true;
    
            for(int j = 0; j <= n; j++)
            {
                if(!vis[j] && dist[j] > dist[index] + G[index][j])
                    dist[j] = dist[index] + G[index][j];
            }
        }
        return dist[e];
    }
    
    int main()
    {
        int a, b, c;
    
        while(cin >> n >> m, n+m)
        {
            Init();
    
            for(int i = 0; i < m; i++)
            {
                cin >> a >> b >> c;
    
                G[a][b] = G[a][b] < c ? G[a][b] : c;
                G[b][a] = G[a][b];
            }
            cout << dij(1, n) << endl;
        }
        return 0;
    }
    让未来到来 让过去过去
  • 相关阅读:
    C#实现ASE加密解密
    c# 深复制
    Jenkins + Git +IIS 部署
    c#模拟Http请求
    TCP/IP学习
    c# 字符串中包含 "" 时提示:无法识别的转义序列
    部署.net core项目到IIS后HTTP 错误 500.19
    .net core读取配置文件appsetting.json
    asp.net提示“未能加载文件或程序集“XXXXXXXX.dll”或它的某一个依赖项。找不到指定的模块。”
    WCF错误404.17 请求的内容似乎是脚本,因而无法由静态文件处理程序来处理
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4658149.html
Copyright © 2011-2022 走看看