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

    畅通工程续

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

    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

    思路:水题大放送!最短路不管它了

      1 #include <cstdio>
      2 #include <cstring>   
      3 #include <iostream>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <cstdlib>
      7 #include <queue>
      8 using namespace std;
      9 
     10 const int maxn=110,maxm=20010,INF=10000000;
     11 struct qq
     12 {
     13     int n,to,z,ne;
     14     friend bool operator < (qq a,qq b)
     15     {
     16         return a.z>b.z;
     17     }
     18 } e[maxm],s,ya;
     19 
     20 priority_queue<qq> q;
     21 int d[maxn],node,x,y,z,cnt,to,n,m,h[maxn];
     22 bool f[maxn];
     23 int st[maxn],pp[maxn],ans;
     24 
     25 void addedge(int x,int y,int z)
     26 {
     27     cnt++;
     28     e[cnt].n=x;
     29     e[cnt].to=y;
     30     e[cnt].z=z;
     31     e[cnt].ne=h[x];
     32     h[x]=cnt;
     33 }
     34 
     35 void close()
     36 {
     37     exit(0);
     38 }
     39 
     40 void dijkstra(int st)
     41 {
     42     memset(f,false,sizeof(f));
     43     while (!q.empty())
     44         q.pop();
     45     f[st]=true;
     46     for (int i=0;i<=n;i++)
     47         d[i]=INF;
     48     d[st]=0;
     49     for (int p=h[st];p!=-1;p=e[p].ne)
     50     {
     51         s.n=st;
     52         s.to=e[p].to;
     53         s.z=e[p].z;
     54         q.push(s);
     55     }
     56     while (!q.empty())
     57     {
     58         s=q.top();
     59         q.pop();
     60         to=s.to;
     61         if (f[to]) continue;
     62         d[to]=s.z;
     63         f[to]=true;
     64         for (int p=h[to];p!=-1;p=e[p].ne)
     65         {
     66             node=e[p].to;
     67             if (not f[node])
     68             {
     69                 ya.n=to;
     70                 ya.to=node;
     71                 ya.z=d[to]+e[p].z;
     72                 q.push(ya);
     73             }
     74         }
     75     }
     76 }
     77 
     78 void init()
     79 {
     80     while (scanf("%d %d",&n,&m)!=EOF)
     81     {
     82         memset(h,-1,sizeof(h));
     83         cnt=0;
     84         for (int i=1;i<=m;i++)
     85         {
     86             scanf("%d %d %d",&x,&y,&z);
     87             addedge(x,y,z);
     88             addedge(y,x,z);
     89         }
     90         int st,en;
     91         scanf("%d %d",&st,&en);
     92         dijkstra(st);
     93         if (d[en]!=INF)
     94             printf("%d\n",d[en]);
     95         else
     96             printf("-1\n");
     97     }
     98 }
     99 
    100 
    101 int main ()
    102 {
    103     init();
    104     close();
    105 }
  • 相关阅读:
    Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
    Educational Codeforces Round 60 D dp + 矩阵快速幂
    Educational Codeforces Round 60 C 思维 + 二分
    Codeforces Round #544 (Div. 3) dp + 双指针
    Codeforces Round #542(Div. 2) CDE 思维场
    UVA
    UVA
    UVA
    UVA
    UVA
  • 原文地址:https://www.cnblogs.com/cssystem/p/3046007.html
Copyright © 2011-2022 走看看