畅通工程续

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. java用于控制可见性的4个访问修饰符

    仅对本类可见——private 对所有类可见——public 对本包的所有子类可见——protected 对本包可见——默认(很遗憾)不需要修饰符

  2. ASP.NET读取RSS

    从网上找的一段读取RSS的代码,经测能用: /// <summary> /// 加载RSS /// </summary> /// <param name="Rs ...

  3. 【CodeForces】671 C. Ultimate Weirdness of an Array

    [题目]C. Ultimate Weirdness of an Array [题意]给定长度为n的正整数序列,定义一个序列的价值为max(gcd(ai,aj)),1<=i<j<=n, ...

  4. Mac 10.9x下安装配置phonegap3.0开发环境 (涉及android sdk配置)

    最近突然想弄一下phonegap,之前一直是听说,没亲自配置开发过.结果配置过程非常艰难啊.特别是android平台的配置,那叫一个麻烦,网上搜了半天都没找到非常好的资料.文章也都是抄来抄去,最烦的就 ...

  5. Openflow Plugin学习笔记2

    OpenDaylight OpenFlow Plugin 过载保护 过载保护 OF Plugin中的过载保护按如下流程工作: ConnectionConductor将消息送入队列,是最靠近OFJava ...

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

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

  7. linux 下 genymotion 模拟器不能安装app

    提示: "应用未安装" 解决方法: 下载: Genymotion-ARM-Translation_v1.1.zip 进入genymotion 的tools用adb传进去: ./ad ...

  8. USB基础介绍

    (转)USB (Universal Serial Bus) 全文地址:http://vlewang.blog.163.com/blog/static/105878151201032804347546/ ...

  9. SSO单点登录的发展由来以及实现原理【转】

    单点登录以及权限,在很早之前都有写过,不过都比较简单,今天就具体说一下,以及下一步要做的 1.web单系统应用 早期我们开发web应用都是所有的包放在一起打成一个war包放入tomcat容器来运行的, ...

  10. 安装完ODTwithODAC112012,出现ORA-12560:TNS:协议适配器错误

    参考:http://blog.csdn.net/tan_yixiu/article/details/6762357 操作系统:windows2008 Enterprise 64位 开发工具:VS201 ...