首页 > 代码库 > POJ - 3013 Big Christmas Tree
POJ - 3013 Big Christmas Tree
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
题意:要建一棵圣诞树,使得总的花费最小。具体规则是:圣诞树是一颗无向树形图,其中,编号为1的节点为根节点,原始图中每条边具有边权(unit):材料的单位价值,每个点也有一个权(weight):点的重量。生成树中,各个点处的花费是指向该点的边权(unit)* 该点的子树中所有点的重量(weight)和,总的花费则是生成树中所有点的花费之和。
思路:推导到其实题目要求的计算方法就是每个点的权值*它到根节点的最短路,只有最短路才能使最小花费
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #define inf 1ll<<61 using namespace std; typedef long long ll; const int maxn = 50005; struct Edge { int from, to, dist; }; struct HeapNode { int d, u; bool operator< (const HeapNode rhs) const { return d > rhs.d; } }; struct Dijkstra { int n, m; // 点数和边数 vector<Edge> edges; //边列表 vector<int> G[maxn]; // 每个点出发的边编号(0开始) bool done[maxn]; // 是否已标记 ll d[maxn]; //s 到各个点的距离 int p[maxn]; //最短路中上一个点,也可以是上一条边 void init(int n) { this->n = n; for (int i = 0; i < n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int dist) { edges.push_back((Edge){from, to, dist}); m = edges.size(); G[from].push_back(m-1); } void dijkstra(int s) { priority_queue<HeapNode> Q; for (int i = 0; i < n; i++) d[i] = inf; d[s] = 0; memset(done, 0, sizeof(done)); Q.push((HeapNode){0, s}); while (!Q.empty()) { HeapNode x = Q.top(); Q.pop(); int u = x.u; if (done[u]) continue; done[u] = true; for (int i = 0; i < G[u].size(); i++) { Edge &e = edges[G[u][i]]; if (d[e.to] > d[u] + e.dist) { d[e.to] = d[u] + e.dist; p[e.to] = e.from; Q.push((HeapNode){d[e.to], e.to}); } } } } }solve; int v, e; int weight[maxn]; int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%d", &v, &e); solve.init(v); for (int i = 0; i < v; i++) scanf("%d", &weight[i]); for (int i = 0; i < e; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--, b--; solve.AddEdge(a, b, c); solve.AddEdge(b, a, c); } solve.dijkstra(0); ll ans = 0; for (int i = 0; i < v; i++) { if (solve.d[i] == inf) { ans = -1; break; } ans += weight[i]*solve.d[i]; } if (ans == -1) printf("No Answer\n"); else printf("%lld\n", ans); } return 0; }