Invitation Cards
Time Limit: 8000MS   Memory Limit: 262144K
Total Submissions: 33435   Accepted: 11104

Description

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到所有点和所有点到1的最短路之和。

小小变形的最短路。

所有点到1的最短路。将边都反转过来跑一遍1的单源最短路即可。证明可以自己想一下,很简单。

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<queue>
  5.  
  6. using namespace std;
  7.  
  8. const int maxn=;
  9. const long long inf=;
  10.  
  11. int input[maxn+][];
  12.  
  13. int to[maxn+];
  14. int w[maxn+];
  15. int nex[maxn+];
  16. int head[maxn+];
  17.  
  18. //建图
  19. void build(int m)
  20. {
  21. memset(head,-,sizeof(head));
  22. int u0,v0,w0;
  23. for(int i=;i<m;i++)
  24. {
  25. u0=input[i][];
  26. v0=input[i][];
  27. w0=input[i][];
  28. to[i]=v0;w[i]=w0;
  29. nex[i]=head[u0];head[u0]=i;
  30. }
  31. }
  32.  
  33. int vis[maxn+];
  34. int dis[maxn+];
  35.  
  36. void spfa(int n)
  37. {
  38. memset(vis,,sizeof(vis));
  39. for(int i=;i<=n;i++)
  40. dis[i]=inf;
  41. queue<int> q;
  42. q.push();
  43. vis[]=;dis[]=;
  44. while(!q.empty())
  45. {
  46. int u0=q.front();q.pop();
  47. for(int i=head[u0];i!=-;i=nex[i])
  48. {
  49. int v0=to[i],w0=w[i];
  50. if(dis[u0]+w0<dis[v0])
  51. {
  52. dis[v0]=dis[u0]+w0;
  53. if(!vis[v0])
  54. {
  55. q.push(v0);
  56. vis[v0]=;
  57. }
  58. }
  59. }
  60. }
  61. }
  62.  
  63. int main()
  64. {
  65. int nn;
  66. scanf("%d",&nn);
  67. while(nn--)
  68. {
  69. int n,m;
  70. scanf("%d%d",&n,&m);
  71. for(int i=;i<m;i++)
  72. scanf("%d%d%d",input[i],input[i]+,input[i]+);
  73.  
  74. long long ans=;
  75.  
  76. build(m);
  77. spfa(n);
  78. for(int i=;i<=n;i++)
  79. ans+=1LL*dis[i];
  80.  
  81. for(int i=;i<m;i++)
  82. swap(input[i][],input[i][]);
  83. build(m);
  84. spfa(n);
  85. for(int i=;i<=n;i++)
  86. ans+=1LL*dis[i];
  87.  
  88. printf("%lld\n",ans);
  89. }
  90. return ;
  91. }

poj 1511 Invitation Cards (最短路)的更多相关文章

  1. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  2. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  3. poj 1511 Invitation Cards(最短路中等题)

    In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...

  4. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  5. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

  6. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  7. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  8. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  9. (简单) POJ 1511 Invitation Cards,SPFA。

    Description In the age of television, not many people attend theater performances. Antique Comedians ...

随机推荐

  1. HTML_本地存储

    在HTML5当中,新增了很多的存储方式,这里我先介绍两种,方便我们的使用和操作,具体新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问 ...

  2. nginx配置路径问题

    编译了一个程序放在服务器上,通过nginx配置转发访问.例如在配置下图的地址 d:\wayne\nginxWeb\www: 发现无法正常运行,查看error.log发现是有问题的,当创建文件时,ngi ...

  3. Docker从入门到实践(4-1)

    使用 Docker 镜像 在之前的介绍中,我们知道镜像是 Docker 的三大组件之一. Docker 运行容器前需要本地存在对应的镜像,如果本地不存在该镜像,Docker 会从镜像仓库下载该镜像. ...

  4. vue cli3.0 封装组件全局引入js文件并发布到npm

    首先用 vue create创建一个项目 当前的项目目录是这样的: 首先需要创建一个 packages 目录,用来存放组件 然后将 src 目录改为 examples 用作示例 二.修改配置 启动项目 ...

  5. 护网杯2019 mergeheap --pwn

    护网 又是签到 一天 这道题一开始 不懂得如何泄露 libc 信息,就蒙了  后来群里师傅也是刚刚好 做出 到这里 我就接着做了 . 先看下保护,发现  全开了 然后 就看下流程 大概 就是添加  c ...

  6. mysql数据库终端上的增删改查及权限等相关操作

    ctrl + c 终止 [linux] service mysql start 启动mysql service mysql stop 停止mysql service mysql restart 重启m ...

  7. Kotlin协程通信机制: Channel

    Coroutines Channels Java中的多线程通信, 总会涉及到共享状态(shared mutable state)的读写, 有同步, 死锁等问题要处理. 协程中的Channel用于协程间 ...

  8. 把Git Repository建到U盘上去

    Git很火.原因有三: 它是大神Linus Torvalds的作品,天然地具备神二代的气质和品质: 促进了生产力的发展,Git的分布式版本控制理念,并非首创,但非常适合开源社区的协作方式(不存在mas ...

  9. LeetCode 链表题 ( Java )

    leetcode 237. 删除链表中的节点 链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 示例 : 输入: he ...

  10. 带着canvas去流浪系列之七 绘制水球图

    [摘要] 用原生canvasAPI实现百度echarts 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 任务说明 使用原生canvasAPI绘制 ...