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): 27010    Accepted Submission(s): 9742


    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
     
    Author
    linle
     
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2544 2066 2112 1217 1233 
     
     
    最短路问题
    边看边写的SPFA算法
     
     1 #include <iostream>
     2 #include <queue>
     3 using namespace std;
     4 
     5 const int maxm = 1010;
     6 const int maxn = 201;
     7 const int inf = 99999999;
     8 int v[maxm],w[maxm],next[maxm];//与边相关的量
     9 int first[maxn],inq[maxn],d[maxn],e;//与点相关的量
    10 
    11 void init()//初始化
    12 {
    13     for(int i =0;i<maxn;i++)
    14         first[i]=-1;
    15     e=0;
    16 }
    17 
    18 void add_eage(int a,int b,int c)//加边
    19 {
    20     v[e]=b;
    21     w[e]=c;
    22     next[e]=first[a];
    23     first[a]=e;
    24     e++;
    25 }
    26 
    27 void spfa(int begin)//重点!!!
    28 {
    29     queue<int> q;
    30     for(int i=0;i<maxn;i++)//把距离初始化为无穷大
    31         d[i]=inf;
    32     d[begin] = 0;//对起点进行处理
    33     inq[begin] = 1;//这是点是否在集合里的标志
    34     q.push(begin);
    35     while(!q.empty())
    36     {
    37         int u = q.front();//取出现在在队头的点
    38         q.pop();
    39         inq[u] = 0;
    40         for(int i = first[u];i!=-1;i=next[i])//对该点的没一条边进行遍历
    41         {
    42             if(d[v[i]]>d[u]+w[i])//松弛操作
    43             {
    44                 d[v[i]] = d[u]+w[i];
    45                  if(inq[v[i]]==0)
    46                  {
    47                      q.push(v[i]);
    48                      inq[v[i]] = 1;
    49                  }
    50             }
    51         }
    52     }
    53 }
    54 
    55 int main()
    56 {
    57 
    58     int N,M,A,B,X,S,T;
    59     cin.sync_with_stdio(false);
    60     while(cin>>N>>M)
    61     {
    62         init();
    63         for(int i =0;i<M;i++)
    64         {
    65             cin>>A>>B>>X;
    66             add_eage(A,B,X);
    67             add_eage(B,A,X);//双向图
    68         }
    69         cin>>S>>T;
    70         spfa(S);
    71         if(d[T] != inf)
    72             cout<<d[T]<<endl;
    73         else
    74             cout<<"-1"<<endl;
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    Linux软件管理
    Linux计划任务与进程管理
    PHP学习 Day_04
    Linux网络管理
    单播组播的实现
    Linux 下的tmpfs文件系统(/dev/shm)
    11.Linux date命令的用法
    Luogu P3783 [SDOI2017]天才黑客
    Luogu P3768 简单的数学题
    Luogu P2336 [SCOI2012]喵星球上的点名
  • 原文地址:https://www.cnblogs.com/Run-dream/p/3873500.html
Copyright © 2011-2022 走看看