zoukankan      html  css  js  c++  java
  • HDU 1874 畅通工程续

    畅通工程续

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 9566    Accepted Submission(s): 3200


    Problem 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
     
     
    注意:两地之间可能有多条路。所以输入时要去最小值。
     
    View Code
    #include <set>
    #include <map>
    #include <list>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cctype>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    #include <cstdlib>
    #include <cassert>
    #include <iostream>
    #include <algorithm>

    using namespace std;
    //Constant Declaration
    /*
    --------------------------*/
    //#define LL long long
    #define LL __int64
    const int M=250;//最多点数
    const int INF=1<<30;
    const double EPS = 1e-11;
    const double PI = acos(-1.0);
    /*--------------------------*/
    // some essential funtion
    /*
    ----------------------------------*/
    void Swap(int &a,int &b){ int t=a;a=b;b=t; }
    int Max(int a,int b){ return a>b?a:b; }
    int Min(int a,int b){ return a<b?a:b; }
    int Gcd(int a,int b){ while(b){b ^= a ^=b ^= a %= b;} return a; }
    /*----------------------------------*/
    //for (i = 0; i < n; i++)
    /*
    ----------------------------------*/

    int d[M];//表示i到源点的最短距离
    int g[M][M];//邻接矩阵
    bool used[M];//标记i是否被用过

    void init(int n)
    {
    int i, j;
    for (i = 0; i < n; i++)
    {
    for (j = 0; j < n; j++)
    {
    g[i][j] = INF;//初始化图没有边,默认为INF,为了一定更新
    }
    }
    for (i = 0; i < n; i++)
    {
    d[i] = INF;
    }

    memset(used, false, sizeof(used));
    }

    int dijkstra(int star, int end, int n)//起点,终点,总点数(编号为1,2...n)
    {
    int min_num = 1;//最小值的位置
    int i;
    d[star] = 0;//起点到起点的最短距离为0,很重要的一步
    for (int cnt = 0; cnt < n; cnt++)//注意别用while(n--),这样会改变n的值。n次贪心
    {
    int min = INF;
    for (i = 0; i < n; i++)
    {
    if (!used[i] && d[i] < min)
    {
    min = d[i];
    min_num = i;
    }
    }

    used[min_num] = 1;

    //把d[min_num]作为中间点,对相邻的点做松弛
    for (i = 0; i < n; i++)
    {
    if (!used[i] && d[i] > d[min_num] + g[min_num][i])
    {
    d[i] = d[min_num] + g[min_num][i];
    }
    }

    }
    return d[end];
    }
    int main()
    {
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //int t, case1 = 0;
    //scanf("%d", &t);
    int n, m;
    int i, j;
    while (scanf("%d%d", &n, &m) != EOF)
    {
    init(n);
    while (m--)
    {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    g[b][a] = g[a][b] = Min(c, g[a][b]);//此题为无向图
    }
    int star, end;
    scanf("%d%d", &star, &end);
    int ans = dijkstra(star, end, n);
    if(INF == ans)
    {
    puts("-1");
    }
    else
    {
    printf("%d\n", ans);
    }
    }

    return 0;
    }
  • 相关阅读:
    企业级管理软件快速开发平台在同一个数据库上进行多个系统开发
    企业级管理软件快速开发平台设计思想分享
    由IT代码工转行做销售2年,给自己的销售管理团队做了个CRM,欢迎大家批评指正!
    探讨未来平台化开发技术
    企业级管理软件快速开发平台极致业务基础平台开发效果一览
    封装原生js的Ajax方法
    正则表达式之圆括号(转)
    完美/兼容版添加事件以及删除事件
    判断浏览器版本及浏览器类型
    使用Normalize.css重置默认样式
  • 原文地址:https://www.cnblogs.com/qiufeihai/p/2382470.html
Copyright © 2011-2022 走看看