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

    在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的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 <bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<set>
    #include<vector>
    #include<iomanip>
    using namespace std;
    typedef long long ll;
    const int inf = 0x3f3f3f3f;
    const int mod = 7;
    const double eps = 1e-8;
    const int mx = 20005; //check the limits, dummy
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
    bool isprime(int n) { if (n <= 1)return 0; for (int i = 2; i * i <= n; i++)if (n % i == 0)return 0; return 1; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(ll i=(a),_=(b);i>=_;i--)
    #define clr(a,b) memset(a, b, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pair
    inline ll qpow(ll a, ll b) { return b ? ((b & 1) ? a * qpow(a * a % mod, b >> 1) % mod : qpow(a * a % mod, b >> 1)) % mod : 1; }
    //inline ll qpow(ll a, ll b, ll c) { return b ? ((b & 1) ? a * qpow(a * a % c, b >> 1) % c : qpow(a * a % c, b >> 1)) % c : 1; }
    void ca(int kase, int ans) { cout << "Case #" << kase << ": " << ans << endl; }
    void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    int n, m, t, k;
    const int NUM = 105;
    int graph[NUM][NUM];//邻接矩阵存图
    void floyd() {
        int s = 1;//起点
        re(k, 1, n + 1) {
            re(i, 1, n + 1) {
                if(graph[i][k]!=inf)
                    re(j, 1, n + 1) {
                    if (graph[i][j] > graph[i][k] + graph[k][j])
                        graph[i][j] = graph[i][k] + graph[k][j];
                }
            }
        }
        cout << graph[s][n] << endl;
    }
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        while (~scanf("%d%d",&n,&m)&&n&&m)
        {
            re(i, 1, n + 1)
                re(j, 1, n + 1)
                graph[i][j] = inf;
            while (m--) {
                int a, b, c;
                sc(a), sc(b), sc(c);
                graph[a][b] = graph[b][a] = c;
            }
            floyd();
        }
        return 0;
    }
  • 相关阅读:
    【剑指offer】一些简单题05 06
    【剑指offer】11 旋转数组的最小数字
    【剑指offer】10 斐波那契数列
    【剑指offer】 03 数组中重复的数字
    【剑指Offer】 04 二维数组中的查找
    HPCC复习部份
    软工提纲复习
    软件工程复习
    Scala
    [转]对TCP/IP网络协议的深入浅出归纳
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12917796.html
Copyright © 2011-2022 走看看