求最短路径覆盖的全部边权值和。

思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径。

这题很好

  1. #include <cstring>
  2. #include <cmath>
  3. #include <queue>
  4. #include <vector>
  5. #include <cstdio>
  6. #include <algorithm>
  7. using namespace std;
  8. typedef long long ll;
  9. const int maxn = ;
  10. const int maxm = ;
  11. const int INF = 0x3f3f3f3f;
  12. int n, m;
  13. struct ee {
  14. int to;
  15. int nxt;
  16. int w;
  17. } edge[maxm];
  18.  
  19. int head[maxn], tol;
  20. void init() {
  21. memset(head, -, sizeof head );
  22. tol = ;
  23. }
  24. void add(int u, int v, int w) {
  25. edge[tol].to = v;
  26. edge[tol].w = w;
  27. edge[tol].nxt = head[u];
  28. head[u] = tol++;
  29. }
  30. int d1[maxn], d2[maxn];
  31. bool vis[maxn];
  32. void spfa(int s, int t, int d[]) {
  33. for(int i=; i<n; ++i) d[i] = INF;
  34. memset(vis, false, sizeof vis );
  35. queue<int> q;
  36. q.push(s);
  37. d[s] = ;
  38. vis[s] = true;
  39. while(!q.empty()) {
  40. int u = q.front();
  41. q.pop();
  42. vis[u] = false;
  43. for(int i=head[u]; ~i; i=edge[i].nxt) {
  44. int &v = edge[i].to;
  45. int &cost = edge[i].w;
  46. if(d[v] > d[u] + cost) {
  47. d[v] = d[u] + cost;
  48. if(!vis[v]) {
  49. vis[v] = true;
  50. q.push(v);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. void solve() {
  57. int s = , t = n-;
  58. spfa(s, t, d1);
  59. spfa(t, s, d2);
  60. ll ans = ;
  61. int minn = d1[t];
  62. for(int u=; u<n; u++) {
  63. for(int i=head[u]; ~i; i=edge[i].nxt) {
  64. int &v=edge[i].to;
  65. int &cost=edge[i].w;
  66. if(d1[u]+d2[v]+cost==minn) {
  67. ans+=cost;
  68. }
  69. }
  70. }
  71. printf("%I64d\n", ans*);
  72. }
  73. int main() {
  74. int u, v, l;
  75. while(~scanf("%d%d", &n, &m)) {
  76. init();
  77. for(int i=; i<m; ++i) {
  78. scanf("%d%d%d", &u, &v, &l);
  79. add(u, v, l);
  80. add(v, u, l);
  81. }
  82. solve();
  83. }
  84. return ;
  85. }

HNU 13375 Flowery Trails (spfa最短路)的更多相关文章

  1. UVALive 6885 Flowery Trails 最短路

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

  2. NOIP2013 华容道 (棋盘建图+spfa最短路)

    #include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...

  3. UVALive 6885 Flowery Trails 最短路枚举

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

  4. poj1502 spfa最短路

    //Accepted 320 KB 16 ms //有n个顶点,边权用A表示 //给出下三角矩阵,求从一号顶点出发到各点的最短路的最大值 #include <cstdio> #includ ...

  5. HNU 12847 Dwarf Tower(最短路+队列优化)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12847 解题报告:有n样物品,编号从1到n第i样物品可以通过金 ...

  6. hiho(1081),SPFA最短路,(非主流写法)

    题目链接:http://hihocoder.com/problemset/problem/1081 SPFA求最短路,是不应-羁绊大神教我的,附上头像. 我第一次写SPFA,我用的vector存邻接表 ...

  7. hdu 5545 The Battle of Guandu spfa最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...

  8. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  9. BZOJ 1003 物流运输 (动态规划 SPFA 最短路)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...

随机推荐

  1. 详解MySQL中EXPLAIN解释命令(转)

    explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了: 如: expla ...

  2. 在SQL中导入Excel数据时强制以文本类型导入

    Excel不是关系型数据库,在导入到sql中时对于数值型,sql有时int型会处理成float,有时数字文本混排的列,sql会认为是数值型,导入的结果有的数据变成了null,但是用sql导出excel ...

  3. ToString()字符转换类型

    100.ToString("n");结果是100.00 100.ToString("c");结果是¥100.00 100.ToString("e&qu ...

  4. Vijos P1003 等价表达式 随机数+单调栈

    题目链接:https://vijos.org/p/1003 题意: 1. 表达式只可能包含一个变量‘a’. 2. 表达式中出现的数都是正整数,而且都小于10000. 3. 表达式中可以包括四种运算‘+ ...

  5. 【viewResolver】 springmvc jsp

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> < ...

  6. Mac操作系统常用快捷键

    复制:cmd+c      粘贴:cmd+v      剪切:先cmd+c,再cmd+opt+v 显示桌面:cmd+F3      切换输入法:cmd+space 打开Spotlight:ctrl+s ...

  7. SDC(4)–set_clock_groups 与–add选项

    1,set_clock_groups  -exclusive 有多个时钟,但是多个时钟不会同时生效 例如: 2,-add 只有一个时钟输入源,但是始终的频率等可能变 例如:

  8. 移动端Reactive Native轮播组件

    移动端Reactive Native轮播组件 总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reac ...

  9. matlab怎么同时显示imshow 两幅图片

    matlab怎么同时显示imshow 两幅图片 matlab怎么同时显示imshow 两幅图片 方法一:subplot()函数 subplot(2,1,1); subplot(2,1,2); 分上下或 ...

  10. eclipse, Log4j配置(真心的详细~)

    转自: http://www.cnblogs.com/alipayhutu/archive/2012/06/21/2558249.html a). 新建Java Project>>新建pa ...