[关闭]
@Chilling 2017-04-26T20:04:03.000000Z 字数 3859 阅读 904

POJ-3013: Big Christmas Tree(DIJ | SPFA)

最短路


Description

Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of the tree is shown in right picture.
此处输入图片的描述

The tree can be represented as a collection of numbered nodes and some edges. The nodes are numbered 1 through n. The root is always numbered 1. Every node in the tree has its weight. The weights can be different from each other. Also the shape of every available edge between two nodes is different, so the unit price of each edge is different. Because of a technical difficulty, price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge).

Suby wants to minimize the cost of whole tree among all possible choices. Also he wants to use all nodes because he wants a large tree. So he decided to ask you for helping solve this task by find the minimum cost.

Input

The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case consists of several lines. Two numbers v, e (0 ≤ v, e ≤ 50000) are given in the first line of each test case. On the next line, v positive integers wi indicating the weights of v nodes are given in one line. On the following e lines, each line contain three positive integers a, b, c indicating the edge which is able to connect two nodes a and b, and unit price c.

All numbers in input are less than 216.

Output

For each test case, output an integer indicating the minimum possible cost for the tree in one line. If there is no way to build a Christmas tree, print “No Answer” in one line.

Sample Input

2
2 1
1 1
1 2 15
7 7
200 10 20 30 40 50 60
1 2 1
2 3 3
2 4 2
3 5 4
3 7 2
3 6 3
1 5 9

Sample Output

15
1210

题意:哈理工1419是本题的中文题意。

分析: 第二个样例


得到:最小的答案=每个点*该点到根结点的最短路

注意: INF开到0x3f3f3f3f3f3f3f3f


  1. #include<queue>
  2. #include<stdio.h>
  3. #include<vector>
  4. #include<string.h>
  5. #define INF 0x3f3f3f3f3f3f3f3f
  6. using namespace std;
  7. typedef long long LL;
  8. const int maxn=50005;
  9. int vv[maxn];
  10. int pos[maxn*2],vis[maxn],num;
  11. LL dis[maxn],ans;
  12. struct node1
  13. {
  14. int en,val,next;
  15. }a[maxn*2];
  16. struct node
  17. {
  18. int id,val;
  19. node(int idd=0,int vall=0)
  20. {
  21. id=idd;
  22. val=vall;
  23. }
  24. friend bool operator <(node x,node y)
  25. {
  26. return x.val>y.val;
  27. }
  28. };
  29. void add(int st,int en,int val)
  30. {
  31. a[num].en=en;
  32. a[num].val=val;
  33. a[num].next=pos[st];
  34. pos[st]=num++;
  35. }
  36. void dij()
  37. {
  38. dis[1]=0;
  39. priority_queue<node>q;
  40. q.push(node(1,dis[1]));
  41. while(!q.empty())
  42. {
  43. node now=q.top();
  44. q.pop();
  45. if(vis[now.id]) continue;
  46. vis[now.id]=1;
  47. for(int i=pos[now.id];i!=-1;i=a[i].next)
  48. {
  49. if(!vis[a[i].en]&&dis[a[i].en]>dis[now.id]+a[i].val)
  50. {
  51. dis[a[i].en]=dis[now.id]+a[i].val;
  52. q.push(node(a[i].en,dis[a[i].en]));
  53. }
  54. }
  55. }
  56. }
  57. void init()
  58. {
  59. ans=0;
  60. num=0;
  61. memset(vis,0,sizeof(vis));
  62. memset(dis,0x3f,sizeof(dis));
  63. memset(pos,-1,sizeof(pos));
  64. }
  65. int main()
  66. {
  67. int t;
  68. int v,e;
  69. int x,y,z;
  70. scanf("%d",&t);
  71. while(t--)
  72. {
  73. init();
  74. scanf("%d%d",&v,&e);
  75. for(int i=1;i<=v;i++)
  76. scanf("%d",&vv[i]);
  77. while(e--)
  78. {
  79. scanf("%d%d%d",&x,&y,&z);
  80. add(x,y,z);
  81. add(y,x,z);
  82. }
  83. dij();
  84. int flag=1;
  85. for(int i=1;i<=v;i++)
  86. {
  87. if(dis[i]==INF)
  88. {
  89. flag=0;
  90. break;
  91. }
  92. ans+=dis[i]*vv[i];
  93. }
  94. if(flag)
  95. printf("%lld\n",ans);
  96. else
  97. printf("No Answer\n");
  98. }
  99. return 0;
  100. }
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<vector>
  4. #include<queue>
  5. #define INF 0x3f3f3f3f3f3f3f3f
  6. using namespace std;
  7. typedef long long LL;
  8. const int maxn=50005;
  9. int v,e;
  10. int vv[maxn];
  11. int cnt[maxn],vis[maxn];
  12. struct node
  13. {
  14. int en,val,next;
  15. }a[maxn*2];
  16. int pos[maxn],num;
  17. LL dis[maxn],ans;
  18. void add(int st,int en,int val)
  19. {
  20. a[num].en=en;
  21. a[num].val=val;
  22. a[num].next=pos[st];
  23. pos[st]=num++;
  24. }
  25. void init()
  26. {
  27. num=0;
  28. ans=0;
  29. memset(pos,-1,sizeof(pos));
  30. }
  31. void spfa()
  32. {
  33. queue<int>q;
  34. for(int i=1;i<=v;i++)
  35. dis[i]=INF,vis[i]=0,cnt[i]=0;
  36. dis[1]=0;
  37. cnt[1]++;
  38. q.push(1);
  39. while(!q.empty())
  40. {
  41. int u=q.front();
  42. q.pop();
  43. vis[u]=0;
  44. for(int i=pos[u];i!=-1;i=a[i].next)
  45. {
  46. int w=a[i].en;
  47. int val=a[i].val;
  48. if(dis[w]>dis[u]+val)
  49. {
  50. dis[w]=dis[u]+val;
  51. if(!vis[w])
  52. {
  53. vis[w]=1;
  54. cnt[w]++;
  55. if(cnt[w]>=v)
  56. return;
  57. q.push(w);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. int main()
  64. {
  65. int t;
  66. int x,y,z;
  67. scanf("%d",&t);
  68. while(t--)
  69. {
  70. init();
  71. scanf("%d%d",&v,&e);
  72. for(int i=1;i<=v;i++)
  73. scanf("%d",&vv[i]);
  74. while(e--)
  75. {
  76. scanf("%d%d%d",&x,&y,&z);
  77. add(x,y,z);
  78. add(y,x,z);
  79. }
  80. spfa();
  81. int flag=1;
  82. for(int i=1;i<=v;i++)
  83. {
  84. if(dis[i]==INF)
  85. {
  86. flag=0;
  87. break;
  88. }
  89. ans+=dis[i]*vv[i];
  90. }
  91. if(flag)
  92. printf("%lld\n",ans);
  93. else
  94. printf("No Answer\n");
  95. }
  96. return 0;
  97. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注