题目链接:http://poj.org/problem?id=1511

就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的.

因为点和边很多, 所以用dijkstra优先队列的做法.

起点到其他点的最短距离之和就是dij一下 . 要求其他点到起点的最短距离的话就是把原先边的方向反向一下,然后再求起点到其他点的最短距离之和 , 同样dij一下.

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. #include <queue>
  6.  
  7. using namespace std;
  8. const int MAXN = 1e6 + ;
  9. typedef long long LL;
  10. typedef pair <LL , LL> P;
  11. vector <P> edge[MAXN]; //这里邻接表二元组(pair)的second代表边的终点标号, first代表边的距离
  12. bool ok[MAXN];
  13. LL d[MAXN] , INF = 1e12;
  14. int a[MAXN] , b[MAXN] , c[MAXN]; //分别代表边的起点 终点 权值
  15.  
  16. void init(int n) {
  17. for(int i = ; i <= n ; i++) {
  18. d[i] = INF;
  19. ok[i] = false;
  20. edge[i].clear();
  21. }
  22. }
  23.  
  24. void dijkstra(int s) {
  25. d[s] = ;
  26. priority_queue <P , vector<P> , greater<P> > Q; //优先队列 二元组的first:最短距离 second:点的标号
  27. Q.push(P( , s)); //压入起点
  28. while(!Q.empty()) {
  29. P p = Q.top();
  30. Q.pop();
  31. int v = p.second;
  32. if(ok[v]) {
  33. continue;
  34. }
  35. ok[v] = true;
  36. for(int i = ; i < edge[v].size() ; i++) {
  37. P temp = edge[v][i];
  38. if(d[temp.second] > d[v] + temp.first) {
  39. d[temp.second] = d[v] + temp.first;
  40. Q.push(P(d[temp.second] , temp.second));
  41. }
  42. }
  43. }
  44. }
  45.  
  46. int main()
  47. {
  48. int t , n , m;
  49. LL u , v , cost;
  50. scanf("%d" , &t);
  51. while(t--) {
  52. scanf("%d %d" , &n , &m);
  53. init(n);
  54. for(int i = ; i < m ; i++) {
  55. scanf("%d %d %d" , &a[i] , &b[i] , &c[i]);
  56. edge[a[i]].push_back(P(LL(c[i]) , LL(b[i])));
  57. }
  58. LL res = ;
  59. dijkstra();
  60. for(int i = ; i <= n ; i++) {
  61. res += d[i];
  62. }
  63. init(n);
  64. for(int i = ; i < m ; i++) {
  65. edge[b[i]].push_back(P(LL(c[i]) , LL(a[i]))); //每条边反向一下
  66. }
  67. dijkstra();
  68. for(int i = ; i <= n ; i++) {
  69. res += d[i];
  70. }
  71. printf("%lld\n" , res);
  72. }
  73. }

POJ 1511 - Invitation Cards (dijkstra优先队列)的更多相关文章

  1. POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))

    题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...

  2. 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 / ...

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

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

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

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

  5. [POJ] 1511 Invitation Cards

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

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

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

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

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

  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. CentOS5.5 正式开始安装 Oracle 11g r2(图形界面安装)

    一.下载oracle 官方网站, 可以下载最新版本 Oracle Database 11g Release http://www.oracle.com/index.html CentOS5. i386 ...

  2. Jqgrid入门-结合Struts2+json实现数据展示(五)

    DEMO用的是ssh框架实现的,具体怎么搭建的就不多做说明了.分页表格的数据操作难点就是数据展现.至于增删改直接用hibernate原生的方法实现即可.         初步分析:表格要实现分页,那么 ...

  3. Facebook存储技术方案:找出“暖性BLOB”数据

    Facebook公司已经在其近线存储体系当中彻底弃用RAID与复制机制,转而采用分布式擦除编码以隔离其所谓的“暖性BLOB”. 暖性?BLOB?这都是些什么东西?大家别急,马上为您讲解: BLOB—— ...

  4. android-async-http

    安装 http://blog.csdn.net/wangwei_cq/article/details/9453345 包内的一些基本的参数 http://www.cnblogs.com/manuose ...

  5. iphone 如何清空UIWebView的缓存

      iphonecachingapplicationcookiescacheperformance I actually think it may retain cached information ...

  6. [反汇编练习] 160个CrackMe之016

    [反汇编练习] 160个CrackMe之016. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  7. jsoup使用选择器语法来查找元素

    问题 你想使用类似于CSS或jQuery的语法来查找和操作元素. 方法 可以使用Element.select(String selector) 和 Elements.select(String sel ...

  8. 如何打开和关闭Oracle Flashback

    1.打开flashback: 关闭数据库 SQL>shutdown immediate; 启动到mount方式 SQL>startup mount; 如果归档没有打开,打开归档[因为fla ...

  9. mybatis Mapper XML 文件

    MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% ...

  10. 按钮点击WIN8 磁贴效果

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...