问从1号点到各个点的距离+各个点到1号点之间的距离和的最小值

详解键连接https://www.cnblogs.com/csx-zzh/p/13411588.html

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.

The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

Output

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

Sample Input

  1. 2
  2. 2 2
  3. 1 2 13
  4. 2 1 33
  5. 4 6
  6. 1 2 10
  7. 2 1 60
  8. 1 3 20
  9. 3 4 10
  10. 2 4 5
  11. 4 1 50

Sample Output

  1. 46
  2. 210
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <queue>
  5. using namespace std;
  6. #define ll long long
  7. #define inf 0x7fffffff
  8. #define N 1212121
  9. int n,m,t,tot;
  10. struct node
  11. {
  12. int u,v,w,next;
  13. }e[N];
  14. int head[N];
  15. int dis[N];
  16. int vis[N];
  17. void add(int u,int v,int w)
  18. {
  19. e[tot].u=u;
  20. e[tot].v=v;
  21. e[tot].w=w;
  22. e[tot].next=head[u];
  23. head[u]=tot++;
  24. }
  25. ll spfa(int s)
  26. {
  27. for(int i=1;i<=n;i++)
  28. {
  29. vis[i]=0;
  30. dis[i]=inf;
  31. }
  32. vis[s]=1;
  33. dis[s]=0;
  34. queue<int >q;
  35. q.push(s);
  36. while(!q.empty())
  37. {
  38. int now=q.front();
  39. q.pop();
  40. vis[now]=0;
  41. for(int i=head[now];i!=-1;i=e[i].next)
  42. {
  43. if(dis[e[i].v]>dis[now]+e[i].w)
  44. {
  45. dis[e[i].v]=dis[now]+e[i].w;
  46. if(!vis[e[i].v])
  47. {
  48. vis[e[i].v]=1;
  49. q.push(e[i].v);
  50. }
  51. }
  52. }
  53. }
  54. ll ans=0;
  55. for(int i=1;i<=n;i++)
  56. {
  57. if(dis[i]!=inf)
  58. ans+=dis[i];
  59. }
  60. return ans;
  61. }
  62. int main()
  63. {
  64. scanf("%d",&t);
  65. while(t--)
  66. {
  67. int u,v,w;
  68. scanf("%d%d",&n,&m);
  69. memset(head,-1,sizeof(head));
  70. tot=0;
  71. for(int i=0;i<m;i++)
  72. {
  73. scanf("%d%d%d",&u,&v,&w);
  74. add(u,v,w);
  75. }
  76. ll sum=spfa(1);
  77. tot=0;
  78. memset(head,-1,sizeof(head));
  79. for(int i=0;i<m;i++)
  80. {
  81. u=e[i].u;
  82. v=e[i].v;
  83. w=e[i].w;
  84. add(v,u,w);
  85. }
  86. sum+=spfa(1);
  87. printf("%lld\n",sum);
  88. }
  89. }

E - E(最短路解决源点到多点,多点到源点的和(有向图))的更多相关文章

  1. D - D (最短路解决源点到多点,多点到源点的和(有向图))

    问从1号点到各个点的距离+各个点到1号点之间的距离和的最小值 In the age of television, not many people attend theater performances ...

  2. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. P1828 香甜的黄油 Sweet Butter 最短路 寻找一个点使得所有点到它的距离之和最小

    P1828 香甜的黄油 Sweet Butter 闲来无事 写了三种最短路(那个Floyed是不过的) 题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1 ...

  4. POJ 1511 最短路spfa

    题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...

  5. Wormholes 最短路判断有无负权值

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  6. 详解zkw算法解决最小费用流问题

    网络流的一些基本概念 很多同学建立过网络流模型做题目, 也学过了各种算法, 但是对于基本的概念反而说不清楚. 虽然不同的模型在具体叫法上可能不相同, 但是不同叫法对应的思想是一致的. 下面的讨论力求规 ...

  7. Bellman-Ford 求含负权最短路

    该算法详解请看   https://www.cnblogs.com/tanky_woo/archive/2011/01/17/1937728.html 单源最短路   当图中存在负权边时 迪杰斯特拉就 ...

  8. 最短路之SPFA算法

    部分来自:http://blog.csdn.net/juststeps/article/details/8772755 求最短路径的算法有许多种,除了排序外,恐怕是OI界中解决同一类问题算法最多的了. ...

  9. 关于dijkstra的优化 及 多源最短路

    先来看这样一道题目 给你N个点,M条双向边,要求求出1号点到其他所有点的距离.其中 2 <= N <= 1e5,  1 <=M <= 1e6. 对于这样的一道题目 我们当然不可 ...

随机推荐

  1. 4.k8s存储之Volume、PV、PVC和StatefulSet

    3.Volume 容器磁盘上的文件的生命周期是短暂的,这就使得在容器中运行重要应用时会出现一些问题.首先,当容器崩溃时,kubelet 会重启它,但是容器中的文件将丢失--容器以干净的状态(镜像最初的 ...

  2. 如果生成allure报告过程中报错AttributeError: module 'allure' has no attribute 'severity_level'

    1.pip uninstall pytest-allure-adaptor 2.pip install allure-pytest 3.搞定 快去吃饭吧

  3. linux中常用服务的安装

    安装环境:centos7.5 配置离线yum源参考:https://blog.csdn.net/mayh554024289/article/details/54236336vi /etc/yum.co ...

  4. 求素数个数的优化-LeetCode204

    问题 计数质数 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 第一种解法容易想到但是会 超时 ...

  5. oracle出现未选定行

    初学oracle,在SQLplus输入查询命令 出现了以下情况.. 后来了解到oracle的SQL语句其中有些词必须大写才会有效. 在这个语句中将username后面的值改为大写就可以了. 还有一种就 ...

  6. SpringBoot魔法堂:@MatrixVariable参数注解使用详解

    前言 RFC3986定义URI的路径(Path)中可包含name-value片段,扩充了以往仅能通过查询字符串(Query String)设置可选参数的囧境. 假如现在需要设计一个用于"搜索 ...

  7. Spring MVC 接收 LocalDate、LocalTime 和 LocalDateTime Java 8 时间类型参数

    使用 Spring MVC 时,很多业务场景下 Controller 需要接收日期时间参数.一个简单的做法是使用 String 接收日期时间字符串(例如:2020-01-29),然后在代码中将其转换成 ...

  8. 忒修斯的Mac

    我有一台Mac笔记本,用了快6年了,当初买它的时候还借了几千块. 三年前,它的屏幕坏了,修理的方式就是直接换屏,而换屏其实就是上半部分连壳带屏幕整个换掉,简单的说:另一台电脑的上半身嫁接过来. 今年, ...

  9. Go 语言编译过程

    走进Golang之编译器原理_大愚Talk-CSDN博客 https://blog.csdn.net/hel12he/article/details/103061921 go编译器 - 知乎 http ...

  10. Map类型数据导出Excel--poi

    https://blog.csdn.net/KevinChen2019/article/details/101064790 <dependency> <groupId>org. ...