标签:acm algorithm poj dijkstra priority_queue
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 21413 | Accepted: 4635 |
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 throughn. 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 numbersv, 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 integersa, b, c indicating the edge which is able to connect two nodesa 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
题意:最短路问题,ans += dist[i] * weight[i] 。
分析:直接利用dijkstra+priority_queue求最短路。
题目链接:http://poj.org/problem?id=3013
代码清单:
#include<map> #include<cmath> #include<queue> #include<stack> #include<cstdio> #include<cstring> #include<string> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int maxv = 50000 + 5; const int maxe = 50000 + 5; const ll max_dis = 1e18; struct Edge{ int to; ll dis; Edge(int to,int dis){ this -> to = to; this ->dis = dis; } }; int T,v,e,a,b; ll w[maxv],dist[maxv],c; vector<Edge>G[maxv]; typedef pair<ll,int>P; void dijkstra(int s,int n){ fill(dist+1,dist+1+n,max_dis); priority_queue<P,vector<P>,greater<P> >q; while(q.size()) q.pop(); dist[s]=0; q.push(P(0,s)); while(q.size()){ P p=q.top(); q.pop(); int v=p.second; if(dist[v]<p.first) continue; for(int i=0;i<G[v].size();i++){ Edge& e=G[v][i]; if(dist[e.to]>dist[v]+e.dis){ dist[e.to]=dist[v]+e.dis; q.push(P(dist[e.to],e.to)); } } } ll ans=0; for(int i=1;i<=n;i++){ if(dist[i]==max_dis){ printf("No Answer\n"); return ; } ans+=dist[i]*w[i]; } printf("%I64d\n",ans); return ; } int main(){ scanf("%d\n",&T); while(T--){ for(int i=1;i<=maxv;i++) G[i].clear(); scanf("%d%d",&v,&e); for(int i=1;i<=v;i++) scanf("%I64d",&w[i]); for(int i=1;i<=e;i++){ scanf("%d%d%I64d",&a,&b,&c); G[a].push_back(Edge(b,c)); G[b].push_back(Edge(a,c)); } dijkstra(1,v); }return 0; }
POJ_3013_Big Christmas Tree(最短路)
标签:acm algorithm poj dijkstra priority_queue
原文地址:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/45132669