码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3013 Big Christmas Tree

时间:2014-09-21 13:27:50      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   java   ar   

Big Christmas Tree

Time Limit: 3000ms
Memory Limit: 131072KB
This problem will be judged on PKU. Original ID: 3013
64-bit integer IO format: %lld      Java class name: Main
 
 

bubuko.com,布布扣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

Source

 
解题:看似最小生成树,其实是求最短路。
 
如何证明?

1_a_2_b_3_c_4

2*a + (a+b)*3 + (a+b+c)*4

=> a*(2+3+4) + b*(3+4) + c*4

假设1号顶点的权重为1,

1到2有一条边,权为a。

 

 
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <queue>
 6 #define LL long long
 7 #define pli pair<LL,int>
 8 #define INF 0x3f3f3f3f3f3f3f3fLL
 9 using namespace std;
10 const int maxn = 50010;
11 struct arc{
12     int to,next;
13     LL w;
14     arc(int x = 0,LL y = 0,int z = -1){
15         to = x;
16         w = y;
17         next = z;
18     }
19 };
20 LL w[maxn],d[maxn];
21 int n,m,tot,head[maxn];
22 arc e[maxn<<2];
23 void add(int u,int v,LL w){
24     e[tot] = arc(v,w,head[u]);
25     head[u] = tot++;
26     e[tot] = arc(u,w,head[v]);
27     head[v] = tot++;
28 }
29 priority_queue< pli,vector< pli > ,greater< pli > >q;
30 void Dijkstra(){
31     while(!q.empty()) q.pop();
32     for(int i = 1; i <= n; i++) d[i] = INF;
33     d[1] = 0;
34     q.push(make_pair(d[1],1));
35     while(!q.empty()){
36         pli p= q.top();
37         q.pop();
38         if(p.first > d[p.second]) continue;
39         for(int i = head[p.second]; ~i; i = e[i].next){
40             if(d[e[i].to] > d[p.second] + e[i].w){
41                 d[e[i].to] = d[p.second] + e[i].w;
42                 q.push(make_pair(d[e[i].to],e[i].to));
43             }
44         }
45     }
46 }
47 int main(){
48     int t,u,v,wt;
49     scanf("%d",&t);
50     while(t--){
51         scanf("%d %d",&n,&m);
52         for(int i = 1; i <= n; i++)
53             scanf("%I64d",w+i);
54         memset(head,-1,sizeof(head));
55         for(int i = tot = 0; i < m; i++){
56             scanf("%d %d %d",&u,&v,&wt);
57             add(u,v,wt);
58         }
59         if(n == 0 || n == 1){
60             puts("0");
61             continue;
62         }
63         Dijkstra();
64         bool flag = true;
65         LL MST = 0;
66         for(int i = 2; i <= n; i++){
67             if(d[i] == INF) {flag = false;break;}
68             MST += d[i]*w[i];
69         }
70         flag?printf("%I64d\n",MST):puts("No Answer");
71     }
72     return 0;
73 }
View Code

 

POJ 3013 Big Christmas Tree

标签:des   style   blog   http   color   io   os   java   ar   

原文地址:http://www.cnblogs.com/crackpotisback/p/3984275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!