[2016-04-05][POJ][1511][Invitation Cards]

  • 时间:2016-04-05 12:57:22 星期二

  • 题目编号:[2016-04-05][POJ][1511][Invitation Cards]

  • 题目大意:给定一个有向图,从点1出发,分别到各个站点后,又回到点1,问最少需要多少车费,

  • 分析:

    • 从1跑一次最短路,然后矩阵转置,再跑一次最短路,两次求和
    • 这里不能用邻接矩阵保存,所以改成邻接表,然后矩阵转置的操作变成重新加一次边
  • 遇到的问题:用vector存图超时,改用数组实现

  1. #include <queue>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cstdio>
  5. using namespace std;
  6. typedef long long ll;
  7. const int maxn = 1E6 + 20;
  8. const int maxm = 1E6 + 20;
  9. int head[maxn],nxt[maxm],mend[maxm],cost[maxm];
  10. void addedge(int u,int v, int c){
  11. static int q = 2;
  12. mend[q] = v;
  13. nxt[q] = head[u];
  14. cost[q] = c;
  15. head[u] = q++;
  16. }
  17. struct Edge{
  18. int u,v,c;
  19. Edge(int _u = 0, int _v = 0,int _c = 0):u(_u),v(_v),c(_c){}
  20. }e[maxn];
  21. struct mNode{
  22. ll u,c;
  23. mNode(ll _u = 0,ll _c = 0):u(_u),c(_c){}
  24. bool operator < (const mNode & a)const{
  25. return c > a.c;
  26. }
  27. };
  28. bool vis[maxn];
  29. ll d[maxn];
  30. ll Dijkstra(int s,int n){
  31. memset(vis,0,sizeof(bool) * (n + 2));
  32. memset(d,0x3f,sizeof(ll) * (n + 2));
  33. priority_queue<mNode> q;
  34. d[s] = 0;
  35. q.push(mNode(s,0));
  36. mNode tmp;
  37. while(!q.empty()){
  38. tmp = q.top();
  39. q.pop();
  40. int u = tmp.u;
  41. if(vis[u]) continue;
  42. vis[u] = 1;
  43. for(int qq = head[u];~qq;qq = nxt[qq]){
  44. int v = mend[qq];
  45. if(!vis[v] && d[v] > d[u] + cost[qq]){
  46. d[v] = d[u] + cost[qq];
  47. q.push(mNode(v,d[v]));
  48. }
  49. }
  50. }
  51. ll ans = 0;
  52. for(int i = 2; i <= n ; ++i)
  53. ans += d[i];
  54. return ans;
  55. }
  56. inline void ini(int n){
  57. memset(head,-1,sizeof(int)*(n + 2));
  58. }
  59. int main(){
  60. int t,n,m,a,b,c;
  61. scanf("%d",&t);
  62. while(t--){
  63. scanf("%d%d",&n,&m);
  64. ini(n);
  65. for(int i = 0;i < m ; ++i){
  66. scanf("%d%d%d",&a,&b,&c);
  67. e[i] = Edge(a,b,c);
  68. addedge(a,b,c);
  69. }
  70. ll ans;
  71. ans = Dijkstra(1,n);
  72. ini(n);
  73. for(int i = 0;i < m ; ++i){
  74. addedge(e[i].v,e[i].u,e[i].c);
  75. }
  76. ans += Dijkstra(1,n);
  77. printf("%I64d\n",ans);
  78. }
  79. return 0;
  80. }

来自为知笔记(Wiz)

时间: 2024-10-28 19:25:02

[2016-04-05][POJ][1511][Invitation Cards]的相关文章

POJ 1511 Invitation Cards

题目来源:http://poj.org/problem?id=1511 题目很长,花了不少时间才理解题意,目的就是为了求出来回两次最小路径(即为本题的差旅费)之和, 第一次从CCS(1)出发到各个点路径最小,SPFA算法没得说,回来时终点是确定的都是CCS(1),相当于把路 径反过来,即把有向图去反方向,又是从1出发到各个点路径最小,再用一个SPFA.注意ans要用long long 不然也WA,这个地方WA了好几次,虽然更改后AC了,但还是不明白,题目明明写了smaller than 1000

POJ 1511 Invitation Cards (最短路)

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 19215   Accepted: 6311 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

HDU 1535 &amp;&amp; POJ 1511 Invitation Cards (SPFA 模板 + 反向建图)

Invitation Cards HDU: Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) POJ: Time Limit: 8000 MS     Memory Limit: 262144 K       Problem Description In the age of television, not many people attend theater performa

POJ 1511 Invitation Cards 【最短路,spfa算法,Dijkstra算法堆优化】

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 25219   Accepted: 8346 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

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

题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. POJ 3268 //#include <bits/stdc++.h> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace

(简单) POJ 1511 Invitation Cards,SPFA。

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

poj 1511 Invitation Cards (spfa+邻接表)

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 19527   Accepted: 6375 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

POJ 1511 Invitation Cards 图论题解

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

POJ 1511 Invitation Cards(逆向思维 SPFA)

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