首页 > 代码库 > POJ3013 Big Christmas Tree[转换 最短路]
POJ3013 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 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
22 11 11 2 157 7200 10 20 30 40 50 601 2 12 3 32 4 23 5 43 7 23 6 31 5 9
Sample Output
151210
Source
点有w,路径有也有权值,price[i]定义为i和所有后代sigma{w}*i与父的路径权值乘积;构造一棵生成树使代价最小
//// 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;}
POJ3013 Big Christmas Tree[转换 最短路]