Big Christmas Tree(poj-3013)最短路

Big Christmas Tree
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 25823   Accepted: 5577

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 ve (0 ≤ ve ≤ 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 abc 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

这题其实就是一个裸的最短路,根据题意,
边权乘以字数点权和最小,
自己转化一下思维就是就是一个点到1点所经历的最短路乘以当前点的点权
这题有一个超级坑点,当n==1 || n==0 的时候要特判一下
还有这个特判不能直接放在输入n,m后面
要等到所有数据输入完才 特判 continue
我特判放错了位置 ,wa了一页
还有这题不能用vector建图 ,
会超时的,以后还是要学会打邻接表,快一定没错。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 const long long INF = 0x3ffffffffffff;
10 const int maxn = 5e4+10;
11 struct node {
12     int v;
13     int w;
14     int next;
15 }edge[2*maxn];
16 int vis[maxn],head[maxn],w[maxn];
17 long long d[maxn];
18 void spfa(int n)
19 {
20     memset(vis,0,sizeof(vis));
21     for (int i=1 ;i<=n ;i++)  d[i]=INF;
22     d[1]=0;
23     vis[1]=1;
24     queue<int>q;
25     q.push(1);
26     while(!q.empty()){
27         int u=q.front();
28         q.pop();
29         vis[u]=0;
30         for (int i=head[u] ;i!=-1  ;i=edge[i].next ) {
31             int v=edge[i].v;
32             int w=edge[i].w;
33             if (d[v]>d[u]+w){
34                 d[v]=d[u]+w;
35                 if (!vis[v]) {
36                     q.push(v);
37                     vis[v]=1;
38                 }
39             }
40         }
41     }
42 }
43 int main() {
44     int t;
45     scanf("%d",&t);
46     while(t--){
47         int n,m,e=0;
48         scanf("%d%d",&n,&m);
49         for (int i=1 ;i<=n ;i++) {
50             scanf("%d",&w[i]);
51             head[i]=-1;
52         }
53         for (int i=0 ;i<m ;i++ ){
54             int a,b,c;
55             scanf("%d%d%d",&a,&b,&c);
56             edge[e].v=b;
57             edge[e].w=c;
58             edge[e].next=head[a];
59             head[a]=e++;
60             edge[e].v=a;
61             edge[e].w=c;
62             edge[e].next=head[b];
63             head[b]=e++;
64         }
65         if (n==1 || n==0) {
66             printf("0
");
67             continue;
68         }
69         long long ans=0;
70         int flag=0;
71         spfa(n);
72         for (int i=1 ;i<=n ;i++ ){
73             if (d[i]==INF) {
74                 flag=1;
75                 break;
76             }
77             else ans+=d[i]*w[i];
78         }
79         if (flag ) printf("No Answer
");
80         else printf("%lld
",ans);
81     }
82     return 0;
83 }
 
原文地址:https://www.cnblogs.com/qldabiaoge/p/8949398.html