传送门:

http://poj.org/problem?id=1511

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008

题目大意:

给定p个点,还有他们的边q,(有向图)求从结点1出发到所有结点和所有结点到1的最短路径之和。

其中1 <= P,Q <= 1000000

思路:

和上次 POJ 3268 Silver Cow Party 一样,有向图倒着建立就把从所有结点到1的最短路径改为了从1到所有,那么只需两次dijkstra即可。

还有就是数据量大,要用优先队列的dijkstra,堆优化的更好。

嗯,第一次写这个,参考了别人的。

code1:2014/1/1更新。。。

今天自己写的------

直接用数组模拟链表快了好多,zoj排名挺靠前面的,嘻嘻,不过poj又被虐。。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. using namespace std;
  5. const int MAXN=1000000+10;
  6. bool vis[MAXN];
  7. int head[2][MAXN];
  8. int n,m;
  9. long long ans;
  10. struct edge
  11. {
  12. int to;
  13. int cost;
  14. int next;
  15. }e[2][MAXN];
  16.  
  17. struct node
  18. {
  19. int from;
  20. int cost;
  21. node(int f,int c){from=f;cost=c;}
  22. bool operator < (const node& b)const
  23. {
  24. return cost > b.cost;
  25. }
  26. };
  27.  
  28. int len;
  29. void insert(int from,int to,int cost)
  30. {
  31. e[0][len].to=to;
  32. e[1][len].to=from;
  33.  
  34. e[0][len].cost=e[1][len].cost=cost;
  35.  
  36. e[0][len].next = head[0][ from ];
  37. e[1][len].next = head[1][ to ];
  38.  
  39. head[0][from]=len;
  40. head[1][to]=len;
  41.  
  42. len++;
  43. }
  44.  
  45. void dijkstra(int kind)
  46. {
  47. memset(vis,0,sizeof(vis));
  48. priority_queue<node> q;
  49. q.push(node(1,0));
  50.  
  51. int num=0;
  52. while(!q.empty())
  53. {
  54. node cur=q.top();
  55. q.pop();
  56.  
  57. if(vis[cur.from]==true) continue;
  58.  
  59. vis[cur.from]=true;
  60. ans+=cur.cost;
  61. num++;
  62.  
  63. if(num==n)
  64. break;
  65.  
  66. //这里写得真纠结- -|||
  67.  
  68. for(int ne=head[kind][cur.from];ne!=-1;ne=e[kind][ ne ].next)
  69. {
  70. if(!vis[e[kind][ ne ].to])
  71. q.push(node(e[kind][ ne ].to,e[kind][ ne ].cost+cur.cost));
  72. }
  73. }
  74. }
  75.  
  76. int main()
  77. {
  78. int T;
  79. scanf("%d",&T);
  80. int from,to,cost;
  81. while(T--)
  82. {
  83. len=0;
  84. memset(head,-1,sizeof(head));
  85. scanf("%d%d",&n,&m);
  86. for(int i=0;i<m;i++)
  87. {
  88. scanf("%d%d%d",&from,&to,&cost);
  89. insert(from,to,cost);
  90. }
  91. ans=0;
  92. dijkstra(0);
  93. dijkstra(1);
  94. printf("%lld\n",ans);
  95. }
  96. return 0;
  97. }

code 2:

2013/12/31第一次写,基本上是模仿别人的

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. using namespace std;
  5. const int MAXN=1000000+10;
  6. long long ans;
  7. bool vis[MAXN];
  8. int n,m;
  9. struct edge
  10. {
  11. int to,cost;
  12. edge* next;
  13. edge(){next=NULL;}
  14. }*e[2][MAXN];
  15.  
  16. struct node
  17. {
  18. int cost,id;
  19. bool operator < (const node& b) const
  20. {
  21. return cost>b.cost;
  22. }
  23. node(int i,int c){ cost =c;id=i;}
  24. };
  25.  
  26. inline void addedge(int from,int to,int cost)
  27. {
  28. edge *p=new edge;
  29. edge *q=new edge;
  30.  
  31. p->next=e[0][from];
  32. q->next=e[1][to];
  33.  
  34. p->to=to;
  35. q->to=from;
  36.  
  37. p->cost=q->cost=cost;
  38.  
  39. e[0][from]=p;
  40. e[1][to]=q;
  41. }
  42.  
  43. void dijkstra(int kind)
  44. {
  45. memset(vis,0,sizeof(vis));
  46. priority_queue<node> q;
  47. int num=0;
  48. node cur(1,0);
  49. q.push(cur);
  50. while(!q.empty())
  51. {
  52. cur=q.top();
  53. q.pop();
  54. if(vis[cur.id]==true)
  55. continue;
  56.  
  57. vis[cur.id]=true;
  58. ans+=cur.cost;
  59. num++;
  60. if(num==n)
  61. break;
  62.  
  63. for(edge *p=e[kind][cur.id];p!=NULL;p=p->next)
  64. {
  65. if(vis[p->to]==false)
  66. {
  67. node temp(p->to,p->cost+cur.cost);
  68. q.push(temp);
  69. }
  70. }
  71. }
  72.  
  73. }
  74.  
  75. int main()
  76. {
  77. int T;
  78. scanf("%d",&T);
  79. while(T--)
  80. {
  81. memset(e,NULL,sizeof(e));
  82.  
  83. scanf("%d%d",&n,&m);
  84. for(int i=0;i<m;i++)
  85. {
  86. int from,to,cost;
  87. scanf("%d%d%d",&from,&to,&cost);
  88. addedge(from,to,cost);
  89. }
  90. ans=0;
  91. dijkstra(0);
  92. dijkstra(1);
  93. printf("%lld\n",ans);
  94. }
  95. return 0;
  96. }

POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra的更多相关文章

  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

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

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

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

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

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

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

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

  7. Poj 1511 Invitation Cards(spfa)

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

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

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

  9. POJ 1511 Invitation Cards 链式前向星+spfa+反向建边

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 27200   Accepted: 902 ...

随机推荐

  1. 上市公司恋上互联网金融 目前已有14家涌入P2P

    时至今日,互联网金融已蔚然成风,诸多上市公司正前赴后继介入到P2P业务中,据记者初步统计,目前至少有14家A股上市公司参与了P2P业务.央行6月份的报告显示,中国当前有600多家P2P公司,交易额达到 ...

  2. ssh-keygen && ssh-copy-id 生成管理传输秘钥

  3. 洛谷 P2360 地下城主

    P2360 地下城主 题目描述 你参加了一项秘密任务,在任务过程中你被困在了一个3D的地下监狱里面,任务是计时的,你现在需要在最短的时间里面从地牢里面逃出来继续你的任务.地牢由若干层组成,每一层的形状 ...

  4. 洛谷 P1358 扑克牌

    P1358 扑克牌 题目描述 组合数学是数学的重要组成部分,是一门研究离散对象的科学,它主要研究满足一定条件的组态(也称组合模型)的存在.计数以及构造等方面的问题.组合数学的主要内容有组合计数.组合设 ...

  5. Oracle 11gR2光钎链路切换crs服务发生crash

    Oracle 11gR2光钎链路切换crs服务发生crash 背景: 我们将Oracle 11gR2(11.2.0.4)在RedHat EnterPrise 5.8上通过RDAC完毕的多路径链路冗余. ...

  6. C语言之文件操作06——写数据到文本文件遇0停止

    //文件 /* =============================================================== 题目:输入10个篮球运动员的身高数据(cm)保存至D盘文 ...

  7. IPv6地址表示方法详解

    IPv6是互联网协议的第六版:最初它在IETF的 IPng选取过程中胜出时称为互联网新一代网际协议(IPng),IPv6是被正式广泛使用的第二版互联网协议. 现有标准IPv4只支持大概40亿(4×10 ...

  8. 10lession-if-else条件语句

    python的条件选择语句跟其他语言的及其相似,这里就不做详细记录,仅仅是看个例子好了 #!/usr/bin/python "]: print('1 in [1,2,3,"4&qu ...

  9. 内存泄漏与指针悬挂&野指针介绍

    内存泄漏概念:内存泄漏时指动态申请的内存空间没有正常释放,但是也不能继续使用的情况. 例如: char *ch1; ch1 = new char('A'); char = *ch2 = new cha ...

  10. 36.Node.js 工具模块--OS模块系统操作

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js os 模块提供了一些基本的系统操作函数.我们可以通过以下方式引入该模 ...