Invitation Cards

Time Limit: 8000MS   Memory Limit: 262144K
Total Submissions: 25099   Accepted: 8297

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的最短路径来回总和

思路:

因为题目规模太大,不能用二维数组来存储点与点之间的权值,而且考虑到也会超时,所以选用spfa加邻接表的方式来做这道题

1-所有其他点的最短路径很好求,而其他点到1的的最短距离可以通过把起点与终点反向从而求出的便是其他点到1的距离了

代码如下:

  1. #include <iostream>
  2. #include<queue>
  3. #include<cstring>
  4. #include<cstdio>
  5. using namespace std;
  6. const int maxs = +;
  7. const __int64 INF = 0xFFFFFFFF;
  8. __int64 dist1[maxs],dist2[maxs],ans=;
  9. struct Edge
  10. {
  11. int e,next,weight;
  12. }edge1[maxs],edge2[maxs];
  13. int head1[maxs],head2[maxs];
  14. int n,m;
  15. void spfa(Edge edge[],__int64 dist[],int head[maxs])
  16. {
  17. bool vis[maxs];
  18. memset(vis,false,sizeof(vis));
  19. for(int i=;i<=n;i++)
  20. dist[i]=INF;
  21. dist[]=;
  22. queue<int> q;
  23. q.push();
  24. vis[]=true;
  25. while(!q.empty())
  26. {
  27. int cur = q.front();
  28. q.pop();
  29. vis[cur]=false;
  30. for(int i=head[cur];i!=-;i=edge[i].next)
  31. {
  32. if(dist[edge[i].e]>dist[cur]+edge[i].weight)
  33. {
  34. dist[edge[i].e]=dist[cur]+edge[i].weight;
  35. if(!vis[edge[i].e])
  36. {
  37. q.push(edge[i].e);
  38. vis[edge[i].e]=true;
  39. }
  40. }
  41.  
  42. }
  43. }
  44. }
  45. int main()
  46. {
  47. freopen("in.txt","r",stdin);
  48. int T;
  49. scanf("%d",&T);
  50. while(T--)
  51. {
  52. scanf("%d%d",&n,&m);
  53. memset(head1,-,sizeof(head1));
  54. memset(head2,-,sizeof(head2));
  55. memset(edge1,,sizeof(edge1));
  56. memset(edge2,,sizeof(edge2));
  57. for(int i=;i<=m;i++)
  58. {
  59. int a,b,w;
  60. scanf("%d%d%d",&a,&b,&w);
  61. edge1[i].e=b;
  62. edge1[i].weight=w;
  63. edge1[i].next=head1[a];
  64. head1[a]=i;
  65. edge2[i].e=a;
  66. edge2[i].weight=w;
  67. edge2[i].next=head2[b];
  68. head2[b]=i;
  69. }
  70. ans=;
  71. spfa(edge1,dist1,head1);
  72. spfa(edge2,dist2,head2);
  73. for(int i=;i<=n;i++)
  74. ans+=(dist1[i]+dist2[i]);
  75. printf("%I64d\n",ans);
  76. }
  77. return ;
  78. }

poj1511的更多相关文章

  1. ACM/ICPC 之 最短路-SPFA+正逆邻接表(POJ1511(ZOJ2008))

    求单源最短路到其余各点,然后返回源点的总最短路长,以构造邻接表的方法不同分为两种解法. POJ1511(ZOJ2008)-Invitation Cards 改变构造邻接表的方法后,分为两种解法 解法一 ...

  2. POJ-1511 Invitation Cards---Dijkstra+队列优化+前向星正向反向存图

    题目链接: https://vjudge.net/problem/POJ-1511 题目大意: 给定节点数n,和边数m,边是单向边. 问从1节点出发到2,3,...n 这些节点路程和从从这些节点回来到 ...

  3. POJ1511来回最短路

    POJ1511 问你从1到其它点得所有最短路之和  与  其他点到1得所有最短路之和 得总和 思路很明确就是两次最短路,翻转一次地图就好了 一开始就是两次spfa之间处理好数据得更新管理就好 vect ...

  4. POJ-1511 Invitation Cards 往返最短路 邻接表 大量数据下的处理方法

    题目链接:https://cn.vjudge.net/problem/POJ-1511 题意 给出一个图 求从节点1到任意节点的往返路程和 思路 没有考虑稀疏图,上手给了一个Dijsktra(按紫书上 ...

  5. POJ-1511(Dijkstra+优先队列优化+向前星)

    Invitation Cards POJ-1511 从这道题我还是发现了很多的问题,首先就是快速输入输出,这里的ios::---这一行必须先放在main函数第一行,也就是输入最开始的前面,否则系统疯狂 ...

  6. POJ1511 Invitation Cards(多源单汇最短路)

    边取反,从汇点跑单源最短路即可. #include<cstdio> #include<cstring> #include<queue> #include<al ...

  7. poj1511 最短路

    题意:与poj3268一样,所有人需要从各点到一点再从一点到各点,求最短路总和. 与poj3268一样,先正向建图跑 dijkstra ,得到该点到其他所有各点的最短路,即这些人回去的最短路,再用反向 ...

  8. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  9. poj1511,线段树扫描线面积

    经典题,线段树扫描线其实类似区间更新,一般的做法是想象一根扫描线从上扫到下或者从左扫到右,本题的做法是从上扫到下 只要扫到了一根水平线,就将其更新到线段树对应区间中,区间和它的子区间是独立更新的 #i ...

  10. POJ-1511 Invitation Cards (单源最短路+逆向)

    <题目链接> 题目大意: 有向图,求从起点1到每个点的最短路然后再回到起点1的最短路之和. 解题分析: 在求每个点到1点的最短路径时,如果仅仅只是遍历每个点,对它们每一个都进行一次最短路算 ...

随机推荐

  1. Golang之写一个聊天室

    . 海量用户在线聊天系统 . 点对点聊天 . 用户登录&注册 一.服务端开发 . 用户管理 用户id:数字 用户密码:字母数字组合 用户昵称:用来显示 用户性别:字符串 用户头像:url 用户 ...

  2. ==和equals方法:

    Java程序中判断两个变量是否相等有两种方式: 一.利用 == 运算符: 1.1.如果两个变量是基本类型变量,且都是数值型(不一定要求数值类型完全相同),则只要两个变量的值相同,就返回true 1.2 ...

  3. gdb 调试带参数程序

    在gdb中,运行程序使用r或是run命令. 程序的运行,你有可能需要设置下面四方面的事. 1.程序运行参数. set args 可指定运行时参数.(如:set args 10 20 30 40 50) ...

  4. 简单的socket编程

    1.socket 服务器搭建 实例化socket服务器,循环获取请求 package com.orange.util; import java.io.IOException; import java. ...

  5. Largest product from 3 integers

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  6. hadoop学习笔记(一):概念和组成

    一.什么是hadoop Apache Hadoop是一款支持数据密集型分布式应用并以Apache 2.0许可协议发布的开源软件框架.它支持在商品硬件构建的大型集群上运行的应用程序.Hadoop是根据G ...

  7. js模态窗口返回值(table)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. AlertDialog设计对话框

    MainActivity.java        public class MainActivity extends Activity {       TextView show;       Str ...

  9. BeautifulSoup基本步骤

    http://blog.csdn.net/kikaylee/article/details/56841789 ’BeautifulSoup是Python的一个库,最主要的功能就是从网页爬取我们需要的数 ...

  10. 使用for in循环遍历json对象的数据

    使用for in遍历json对象数据,如果数据中的名称有为数字的话,只对正整数有效,那么先会输出为正整数的数据,后面其他的会按照原来数据中定义的顺序不变输出. 针对名称为数字的json对象数据进行测试 ...