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

    最短路

    Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 180 Accepted Submission(s): 100

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

    Input
    输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商 店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1& lt;=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

    最短路:裸的最短路,没有用spfa(不稳定),用的dijkstra的一种加堆方式,复杂度是Elog(E)的,但复杂度说不清楚那个Log(E),网上有篇文章专门做了分析的

      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 
     24 void addedge(int x,int y,int z)
     25 {
     26     cnt++;
     27     e[cnt].n=x;
     28     e[cnt].to=y;
     29     e[cnt].z=z;
     30     e[cnt].ne=h[x];
     31     h[x]=cnt;
     32 }
     33 
     34 void close()
     35 {
     36     exit(0);
     37 }
     38 
     39 void dijkstra()
     40 {
     41     memset(f,false,sizeof(f));
     42     while (!q.empty())
     43         q.pop();
     44     f[1]=true;
     45     for (int i=1;i<=n;i++)
     46         d[i]=INF;
     47     d[1]=0;
     48     for (int p=h[1];p!=-1;p=e[p].ne)
     49     {
     50         s.n=1;
     51         s.to=e[p].to;
     52         s.z=e[p].z;
     53         q.push(s);
     54     }
     55     while (!q.empty())
     56     {
     57         s=q.top();
     58         q.pop();
     59         to=s.to;
     60         if (f[to]) continue;
     61         d[to]=s.z;
     62         f[to]=true;
     63         for (int p=h[to];p!=-1;p=e[p].ne)
     64         {
     65             node=e[p].to;
     66             if (not f[node])
     67             {
     68                 ya.n=to;
     69                 ya.to=node;
     70                 ya.z=d[to]+e[p].z;
     71                 q.push(ya);
     72             }
     73         }
     74     }
     75 }
     76 
     77 void init()
     78 {
     79     while (scanf("%d %d",&n,&m)!=EOF)
     80     {
     81         memset(h,-1,sizeof(h));
     82         cnt=0;
     83         if (n==0 && m==0) break;
     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         dijkstra();
     91         printf("%d\n",d[n]);
     92     }
     93 }
     94 
     95 
     96 int main ()
     97 {
     98     init();
     99     close();
    100 }
  • 相关阅读:
    记住密码 cookie+MD5 的应用[转]
    TDD测试[转]
    架构模式随笔
    MVC架构探究及其源码实现
    强大的SqlCacheDependency【转】
    使用Nant构建入门
    web架构设计经验分享[转]
    DIV+CSS布局大全
    如何突破Windows 2003 远程桌面连接数限制
    大型互联网网站架构心得[转]
  • 原文地址:https://www.cnblogs.com/cssystem/p/3045948.html
Copyright © 2011-2022 走看看