畅通工程续

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 49963    Accepted Submission(s): 18624

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
  1. 3 3
  2. 0 1 1
  3. 0 2 3
  4. 1 2 1
  5. 0 2
  6. 3 1
  7. 0 1 1
  8. 1 2
 
Sample Output
  1. 2
  2. -1
  3.  
  1. //迪杰斯特拉
  2. #include<queue>
  3. #include<stack>
  4. #include<math.h>
  5. #include<stdio.h>
  6. #include<numeric>//STL数值算法头文件
  7. #include<stdlib.h>
  8. #include<string.h>
  9. #include<iostream>
  10. #include<algorithm>
  11. #include<functional>//模板类头文件
  12. using namespace std;
  13.  
  14. const int INF=1e9+7;
  15. const int maxn=100+10;
  16.  
  17. int n,m,st,ed;
  18. int par[maxn];
  19. int vis[maxn];
  20. int tu[maxn][maxn];
  21.  
  22. int dijkstra(int st,int ed)
  23. {
  24. int i,j,k,minn;
  25. memset(vis,0,sizeof(vis));
  26. for(i=0; i<n; i++)
  27. par[i]=tu[st][i];
  28. vis[st]=1;
  29. par[st]=0;
  30. for(i=1; i<n; i++)
  31. {
  32. minn=INF;
  33. for(j=0; j<n; j++)
  34. {
  35. if(!vis[j]&&minn>par[j])
  36. {
  37. k=j;
  38. minn=par[j];
  39. }
  40. }
  41. vis[k]=1;
  42. for(j=0; j<n; j++)
  43. {
  44. if(!vis[j]&&par[j]>par[k]+tu[k][j])
  45. {
  46. par[j]=par[k]+tu[k][j];
  47. }
  48. }
  49. }
  50. }
  51.  
  52. int main()
  53. {
  54. while(~scanf("%d %d",&n,&m))
  55. {
  56. int i,j,u,v,w;
  57. memset(tu,0,sizeof(tu));
  58. for(i=0; i<n; i++)
  59. {
  60. for(j=0; j<n; j++)
  61. {
  62. tu[i][j]=i==j?0:INF;
  63. }
  64. }
  65. while(m--)
  66. {
  67. scanf("%d %d %d",&u,&v,&w);
  68. if(tu[u][v]>w)
  69. tu[u][v]=tu[v][u]=w;
  70. }
  71. scanf("%d %d",&st,&ed);
  72. dijkstra(st,ed);
  73. printf("%d\n",par[ed]==INF?-1:par[ed]);
  74. }
  75. return 0;
  76. }
  77.  
  78. //Floyd弗洛伊德
  79. #include <cstdio>
  80. #include <cstring>
  81. using namespace std;
  82. #define INF 10000
  83. const int maxn = 201;
  84. int tu[maxn][maxn];
  85.  
  86. int main()
  87. {
  88. int n, m;
  89. while (scanf("%d%d", &n, &m) != EOF)
  90. {
  91. memset (tu, INF, sizeof(tu));
  92. int u, v, w;
  93. for (int i = 0; i < m; i++)
  94. {
  95. scanf("%d%d%d", &u, &v, &w);
  96. if (tu[u][v] > w)
  97. tu[u][v] = tu[v][u] = w;
  98. }
  99. for (int i = 0; i < n ; i++)
  100. tu[i][i] = 0;
  101. for (int k = 0; k < n; k++)
  102. for (int i = 0; i < n; i++)
  103. for (int j = 0; j < n; j++)
  104. {
  105. if (tu[i][k] + tu[k][j] < tu[i][j])
  106. tu[i][j] = tu[j][i] = tu[i][k] + tu[k][j];
  107. }
  108. scanf("%d%d", &u, &v);
  109. if (tu[u][v] < INF)
  110. printf("%d\n", tu[u][v]);
  111. else
  112. printf("-1\n");
  113. }
  114. return 0;
  115. }
  1.  
  1.  
  1. //#include<queue>
  2. //#include<stack>
  3. //#include<vector>
  4. //#include<math.h>
  5. //#include<stdio.h>
  6. //#include<numeric>//STL数值算法头文件
  7. //#include<stdlib.h>
  8. //#include<string.h>
  9. //#include<iostream>
  10. //#include<algorithm>
  11. //#include<functional>//模板类头文件
  12. //using namespace std;
  13. //
  14. //const int INF=1e9+7;
  15. //const int maxn=205;
  16. //vector<pair<int,int> >E[maxn];
  17. //int d[maxn],inq[maxn];//inq数组表示是否在队列中,d数组表示起点到当前点的距离
  18. //
  19. //void init()
  20. //{
  21. //    for(int i=0; i<maxn; i++) E[i].clear();
  22. //    for(int i=0; i<maxn; i++) inq[i]=0;
  23. //    for(int i=0; i<maxn; i++) d[i]=INF;
  24. //}
  25. //
  26. //int n,m;
  27. //int main()
  28. //{
  29. //    while(cin>>n>>m)
  30. //    {
  31. //        int i,j;
  32. //        init();
  33. //        for(i=0; i<m; i++)
  34. //        {
  35. //            int x,y,z;
  36. //            scanf("%d %d %d",&x,&y,&z);
  37. //            E[x].push_back(make_pair(y,z));
  38. //            E[y].push_back(make_pair(x,z));
  39. //        }
  40. //        int s,t;
  41. //        scanf("%d %d",&s,&t);
  42. //        queue<int>q;
  43. //        q.push(s),d[s]=0,inq[s]=1;
  44. //        while(!q.empty())
  45. //        {
  46. //            int now=q.front();
  47. //            q.pop();
  48. //            inq[now]=0;
  49. //            for(int i=0; i<E[now].size(); i++)
  50. //            {
  51. //                int v=E[now][i].first;
  52. //                if(d[v]>d[now]+E[now][i].second)
  53. //                {
  54. //                    d[v]=d[now]+E[now][i].second;
  55. //                    if(inq[v]) continue;
  56. //                    inq[v]=1;
  57. //                    q.push(v);
  58. //                }
  59. //            }
  60. //        }
  61. //        if(d[t]==INF) printf("-1\n");
  62. //        else printf("%d\n",d[t]);
  63. //    }
  64. //    return 0;
  65. //}
  66. //"单源最短路"迪杰斯特拉,优先队列
  67. //(m+n)*log(n)的复杂度
  68. //通过中间点来松弛源点到其余各顶点的路径
  69. #include<queue>
  70. #include<stack>
  71. #include<vector>
  72. #include<math.h>
  73. #include<stdio.h>
  74. #include<numeric>//STL数值算法头文件
  75. #include<stdlib.h>
  76. #include<string.h>
  77. #include<iostream>
  78. #include<algorithm>
  79. #include<functional>//模板类头文件
  80. using namespace std;
  81. const int INF=1e9+7;
  82. const int maxn=205;
  83. vector<pair<int,int> >E[maxn];
  84. int d[maxn];//d数组表示起点到当前点的距离
  85. void init()
  86. {
  87.     for(int i=0; i<maxn; i++) E[i].clear();
  88.     for(int i=0; i<maxn; i++) d[i]=INF;
  89. }
  90. int n,m;
  91. int main()
  92. {
  93.     while(cin>>n>>m)
  94.     {
  95.         int i,j;
  96.         init();
  97.         for(i=0; i<m; i++)
  98.         {
  99.             int x,y,z;
  100.             scanf("%d %d %d",&x,&y,&z);
  101.             E[x].push_back(make_pair(y,z));
  102.             E[y].push_back(make_pair(x,z));
  103.         }
  104.         int s,t;
  105.         scanf("%d %d",&s,&t);
  106.         priority_queue<pair<int,int> >q;
  107.         d[s]=0;
  108.         q.push(make_pair(-d[s],s));
  109.         while(!q.empty())
  110.         {
  111.             int now=q.top().second;
  112.             q.pop();
  113.             for(int i=0; i<E[now].size(); i++)
  114.             {
  115.                 int v=E[now][i].first;
  116.                 if(d[v]>d[now]+E[now][i].second)
  117.                 {
  118.                     d[v]=d[now]+E[now][i].second;
  119.                     q.push(make_pair(-d[v],v));
  120.                 }
  121.             }
  122.         }
  123.         if(d[t]==INF) printf("-1\n");
  124.         else printf("%d\n",d[t]);
  125.     }
  126.     return 0;
  127. }
  128.  
  129. //时间复杂度最低

  130. #include<map>
  131. #include<queue>
  132. #include<stack>
  133. #include<vector>
  134. #include<math.h>
  135. #include<cstdio>
  136. #include<sstream>
  137. #include<numeric>//STL数值算法头文件
  138. #include<stdlib.h>
  139. #include <ctype.h>
  140. #include<string.h>
  141. #include<iostream>
  142. #include<algorithm>
  143. #include<functional>//模板类头文件
  144. using namespace std;
  145. typedef long long ll;
  146. const int maxn=1100;
  147. const int INF=0x3f3f3f3f;
  148. int n,m,dis[maxn],head[maxn],cnt[maxn],len;
  149. bool vis[maxn];
  150. struct edge
  151. {
  152.     int to,val,next;
  153. } e[maxn];
  154. void add(int from,int to,int  val)
  155. {
  156.     e[len].to=to;
  157.     e[len].val=val;
  158.     e[len].next=head[from];
  159.     head[from]=len++;
  160. }
  161. bool spfa(int s,int t)
  162. {
  163.     memset(cnt,0,sizeof(cnt));
  164.     memset(vis,0,sizeof(vis));
  165.     for(int i=0; i<n; i++)
  166.         dis[i]=INF;
  167.     queue<int> q;
  168.     q.push(s);
  169.     cnt[s]++;
  170.     vis[s]=true;
  171.     dis[s]=0;
  172.     while(!q.empty())
  173.     {
  174.         int cur=q.front();
  175.         q.pop();
  176.         vis[cur]=false;
  177.         for(int i=head[cur]; i!=-1; i=e[i].next)
  178.         {
  179.             int id=e[i].to;
  180.             if(dis[id] > dis[cur]+e[i].val)
  181.             {
  182.                 dis[id] = dis[cur] + e[i].val;
  183.                 if(!vis[id])
  184.                 {
  185.                     cnt[id]++;
  186.                     vis[id]=true;
  187.                     if(cnt[cur]>n) return false;
  188.                     q.push(id);
  189.                 }
  190.             }
  191.         }
  192.     }
  193.     return true;
  194. }
  195. int main()
  196. {
  197.     while(~scanf("%d%d",&n,&m))
  198.     {
  199.         len=0;
  200.         memset(head,-1,sizeof(head));
  201.         for(int i=0; i<m; i++)
  202.         {
  203.             int from,to,val;
  204.             scanf("%d%d%d",&from,&to,&val);
  205.             add(from,to,val);
  206.             add(to,from,val);
  207.         }
  208.         int s,t;
  209.         scanf("%d%d",&s,&t);
  210.         spfa(s,t);
  211.         if(spfa(s,t)&&dis[t]!=INF) printf("%d\n",dis[t]);
  212.         else printf("-1\n");
  213.     }
  214.     return 0;
  215. }
  216.  
  217.  

hdu 1874 畅通工程续(迪杰斯特拉优先队列,floyd,spfa)的更多相关文章

  1. HDU 1874 畅通工程续-- Dijkstra算法详解 单源点最短路问题

    参考 此题Dijkstra算法,一次AC.这个算法时间复杂度O(n2)附上该算法的演示图(来自维基百科): 附上:  迪科斯彻算法分解(优酷) problem link -> HDU 1874 ...

  2. ACM: HDU 1874 畅通工程续-Dijkstra算法

    HDU 1874 畅通工程续 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Desc ...

  3. HDU 1874畅通工程续(迪杰斯特拉算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)     ...

  4. hdu 1874 畅通工程续

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...

  5. HDU——1874畅通工程续(邻接矩阵弗洛伊德)

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. hdu 1874 畅通工程续 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目分析:输入起点和终点,顶点的个数,已连通的边. 输出起点到终点的最短路径,若不存在,输出-1 ...

  7. HDU 1874 畅通工程续【Floyd算法实现】

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. hdu 1874 畅通工程续(求最短距离,dijkstra,floyd)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 /************************************************* ...

  9. HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)

    题目链接: 传送门 畅通工程续 Time Limit: 1000MS     Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...

随机推荐

  1. 2015/9/29 Python基础(20):类的授权

    类的授权 1.包装包装在Python编程世界中时经常会被提到的一个术语.它是一个通用的名字,意思是对一个已存在的对象进行包装,不管它是数据类型,还是一段代码,可以是对一个已存在的对象,增加新的,删除不 ...

  2. JS中client/offset/scroll等的宽高解析

    原文地址:→传送门 window相关宽高属性 1. window.outerHeight (窗口的外层的高度) / window.outerWidth (窗口的外层的宽度) window.outerH ...

  3. 基本控件文档-UITableView---iOS-Apple苹果官方文档翻译

    //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3496969.html 技术博客http://www.cnblogs.com/ChenYi ...

  4. CodeForces 869B

    Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense ...

  5. html5手机Web单页应用实践--起点移动阅读

    一开始以hybrid形式做了一个android的小说阅读客户端,叫4G阅读.而后由于业务需求,要迅速实现纯手机html5 版的,所以就直接在原先客户端内内嵌的网页进行改版,快速实现以后在优化的过程中发 ...

  6. Friends and Berries URAL - 2067 (计算三点共线和计算的时候的注意点)

    题目链接:https://cn.vjudge.net/problem/URAL-2067 具体思路:判断三点共线就可以了,只有一对点能满足,如果一对就没有那就没有满足的. 在计算的时候,要注意,如果是 ...

  7. tf.name_scope tf.variable_scope学习

    1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...

  8. 深入理解Spring系列之六:bean初始化

    转载 https://mp.weixin.qq.com/s/SmtqoELzBEdZLo8wsSvUdQ <深入理解Spring系列之四:BeanDefinition装载前奏曲>中提到,对 ...

  9. ogg:Extract 进程遇长事务执行 Forcestop 引发的惨案

    http://www.linuxidc.com/Linux/2015-04/115777.htm SQL> select t.addr,t.START_DATE from v$transacti ...

  10. HDU 6198 2017沈阳网络赛 线形递推

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6198 题意:给出一个数k,问用k个斐波那契数相加,得不到的数最小是几. 解法:先暴力打表看看有没有规律 ...