两次SPFA

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<vector>
  5. #include<queue>
  6. #include<algorithm>
  7. using namespace std;
  8.  
  9. const int INF=0x7FFFFFFF;
  10. const int maxn=+;
  11. const int Maxn=+;
  12. int N,M;
  13. int U[Maxn],V[Maxn],C[Maxn];
  14. struct edge
  15. {
  16. int from,to,cost;
  17. }e[Maxn];
  18. vector<int>G[maxn];
  19. int dis1[maxn],flag1[maxn];
  20. int dis2[maxn],flag2[maxn];
  21.  
  22. void init()
  23. {
  24. for(int i=;i<maxn;i++) G[i].clear();
  25. }
  26.  
  27. void spfa()
  28. {
  29. queue<int>Q;
  30. for(int i=;i<maxn;i++) dis1[i]=INF;
  31. dis1[]=;
  32. flag1[]=;
  33. Q.push();
  34. while(!Q.empty())
  35. {
  36. int h=Q.front();Q.pop(); flag1[h]=;
  37. for(int i=;i<G[h].size();i++)
  38. {
  39. int id=G[h][i];
  40. if(e[id].from==h)
  41. {
  42. if(dis1[h]+e[id].cost<dis1[e[id].to])
  43. {
  44. dis1[e[id].to]=dis1[h]+e[id].cost;
  45. if(flag1[e[id].to]==)
  46. {
  47. flag1[e[id].to]=;
  48. Q.push(e[id].to);
  49. }
  50. }
  51. }
  52. else if(e[id].to==h)
  53. {
  54. if(dis1[h]+e[id].cost<dis1[e[id].from])
  55. {
  56. dis1[e[id].from]=dis1[h]+e[id].cost;
  57. if(flag1[e[id].from]==)
  58. {
  59. flag1[e[id].from]=;
  60. Q.push(e[id].from);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67.  
  68. void SPFA()
  69. {
  70. queue<int>Q;
  71. for(int i=;i<maxn;i++) dis2[i]=INF;
  72. dis2[N-]=;
  73. flag2[N-]=;
  74. Q.push(N-);
  75. while(!Q.empty())
  76. {
  77. int h=Q.front();Q.pop(); flag2[h]=;
  78. for(int i=;i<G[h].size();i++)
  79. {
  80. int id=G[h][i];
  81. if(e[id].from==h)
  82. {
  83. if(dis2[h]+e[id].cost<dis2[e[id].to])
  84. {
  85. dis2[e[id].to]=dis2[h]+e[id].cost;
  86. if(flag2[e[id].to]==)
  87. {
  88. flag2[e[id].to]=;
  89. Q.push(e[id].to);
  90. }
  91. }
  92. }
  93. else if(e[id].to==h)
  94. {
  95. if(dis2[h]+e[id].cost<dis2[e[id].from])
  96. {
  97. dis2[e[id].from]=dis2[h]+e[id].cost;
  98. if(flag2[e[id].from]==)
  99. {
  100. flag2[e[id].from]=;
  101. Q.push(e[id].from);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. int main()
  109. {
  110. while(~scanf("%d%d",&N,&M)){
  111. init();
  112. for(int i=;i<=M;i++)
  113. {
  114. scanf("%d%d%d",&U[i],&V[i],&C[i]);
  115. e[i].from=U[i];
  116. e[i].to=V[i];
  117. e[i].cost=C[i];
  118. G[U[i]].push_back(i);
  119. G[V[i]].push_back(i);
  120. }
  121. spfa();
  122. SPFA();
  123.  
  124. int ans=;
  125. int Len=dis1[N-];
  126. for(int i=;i<=M;i++)
  127. {
  128. if(dis1[U[i]]+dis2[V[i]]+C[i]==Len||dis2[U[i]]+dis1[V[i]]+C[i]==Len)
  129. ans=ans+C[i]+C[i];
  130. }
  131. printf("%d\n",ans);
  132. }
  133. return ;
  134. }

UVALive 6885 Flowery Trails的更多相关文章

  1. UVALive 6885 Flowery Trails 最短路

    Flowery Trails 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid= ...

  2. UVALive 6885 Flowery Trails 最短路枚举

    题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723 题意: 给你一个n点m图的边 1到n有多条最短路 ...

  3. HNU 13375 Flowery Trails (spfa最短路)

    求最短路径覆盖的全部边权值和. 思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径. 这题很好 #include <cstring> #in ...

  4. kuangbin带你飞 最短路 题解

    求一个图最短路边的办法.好像下面的那个有问题.单向边和双向边一定是有区别的.这个比较容易.参照该文的最短路网络流题目和连通图题目一题求最短路关节边 另外上述2个题目的代码好像有问题. 在UVALIVE ...

  5. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  6. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  7. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  8. Codeforces 209 C. Trails and Glades

    Vasya went for a walk in the park. The park has n glades, numbered from 1 to n. There are m trails b ...

  9. CodeForces 209C Trails and Glades

    C. Trails and Glades time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. javacript参数传递表单验证

    <!doctype html> <html> <head> <meta charset="utf-8"> <style typ ...

  2. InvalidateRect只是增加重绘区域,在下次WM_PAINT的时候才生效

    emWIN里面的无效重绘和windows很类似. WM_InvalidateArea()和WM_InvalidateRect()只重绘指定的区域,其他区域不会重绘,这样避免了闪烁,重绘发生在下次WM_ ...

  3. Java heap space

    //首先检查程序有没有限入死循环 这个问题主要还是由这个问题 java.lang.OutOfMemoryError: Java heap space 引起的.第一次出现这样的的问题以后,引发了其他的问 ...

  4. IDEA类文件不编译问题

    用IDEA的人遇到过类文件上有个小叉吗? 1.在 .gitignore 里面把这个文件去掉 2.setting->builder->compiler->子目录 去掉不编译的文件

  5. C语言隐式强制类型转换

    今天又被精度问题困扰,把最基本的东西忘了. int n = 5; int cnt = 5.5; double sum = (n-cnt);  运算完后sum是 -0.5.不知道什么时候n转换成doub ...

  6. nefu 115 斐波那契的整除

    Description 已知斐波那契数列有如下递归定义,f(1)=1,f(2)=1, 且n>=3,f(n)=f(n-1)+f(n-2),它的前几项可以表示为1, 1,2 ,3 ,5 ,8,13, ...

  7. 删除 mysql 日志文件后 ,启动出错

    把 mysql-bin.index 里面的索引全部删除

  8. 第4章 流程控制----编写Java程序,应用for循环打印菱形

    package four; public class fouroneone { public static void main(String args[]){ ;i<=;i+=){ ;kong& ...

  9. Idea 设置根目录

    1.在根目录下新建一个目录yx360-war-ctm-tea,在该目录下新建一个build.gradle文件,输入: apply plugin: 'war' 来引入war插件,war插件会在项目的目录 ...

  10. ubuntu 连接VPN 命令

    1  注册MXVPN 2 启动VPN  pptpsetup --create mxvpn1 --server xx.xx.xx.xx --username *** --password *** --e ...