POJ3013 Big Christmas Tree[轉換 最短路]

Candy?發表於2016-09-12
Big Christmas Tree
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 23387   Accepted: 5063

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

Source

POJ Monthly--2006.09.29, Kim, Chan Min (kcm1700@POJ)

點有w,路徑有也有權值,price[i]定義為i和所有後代sigma{w}*i與父的路徑權值乘積;構造一棵生成樹使代價最小

最小生成樹?沒法建圖,不知道後代有誰
把式子轉換一下,不就是sigma{w[i]*d[i]}...............
dijk 94ms
 
//
//  main.cpp
//  poj3013
//
//  Created by Candy on 9/12/16.
//  Copyright © 2016 Candy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=50005,M=50005;
const ll INF=1e19;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x;
}
int T,n,m,w[N],a,b,c;
struct edge{
    int v,w,ne;
}e[M<<1];
int h[N],cnt=0;
inline void ins(int a,int b,int c){
    cnt++;
    e[cnt].v=b;e[cnt].w=c;e[cnt].ne=h[a];h[a]=cnt;
    cnt++;
    e[cnt].v=a;e[cnt].w=c;e[cnt].ne=h[b];h[b]=cnt;
}

struct hn{
    int u;
    ll d;
    bool operator <(const hn &rhs)const{return d>rhs.d;}
};
ll d[N];
priority_queue<hn> q;
bool vis[N];
void dijkstra(int s){
    for(int i=1;i<=n;i++) d[i]=INF;
    memset(vis,0,sizeof(vis));
    q.push((hn){s,0});d[s]=0;
    while(!q.empty()){
        hn x=q.top();q.pop();
        int u=x.u;
        if(vis[u]) continue; vis[u]=1;
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v,w=e[i].w;
            if(d[v]>d[u]+w){
                d[v]=d[u]+w;
                q.push((hn){v,d[v]});
            }
        }
    }
}
int main(int argc, const char * argv[]) {
    T=read();
    while(T--){
        
        memset(h,0,sizeof(h));cnt=0;
        n=read();m=read();
        for(int i=1;i<=n;i++) w[i]=read();
        for(int i=1;i<=m;i++){
            a=read();b=read();c=read();
            ins(a,b,c);
        }
        dijkstra(1);
        ll ans=0,flag=0;
        for(int i=2;i<=n;i++){
            if(d[i]==INF) {printf("No Answer\n");flag=1;break;}
            ans+=w[i]*d[i];
        }
        if(!flag) printf("%lld\n",ans);
        
    }
    return 0;
}

 

相關文章