Big Christmas Tree

Source: Internet
Author: User

Description

Christmas is coming-KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of the tree are shown in right picture.

The tree can be represented as a collection of numbered nodes and some edges. The nodes is numbered 1 throughn. The root is always numbered 1. Every node in the tree have its weight. The weights can different from each of the other. Also the shape of every available edge between, the nodes is different, and the unit price for each edge is different. Because of a technical difficulty, price of an edge would be (sum of weights of all descendant nodes) x (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 on the first line of the input file. Each test case consists of several lines. The numbersv, e (0≤ v, e ≤50000) is given in the first line of all test case. On the next line,v positive integers wi indicating the weights of v nodes is given in one line . On the following e lines, each line contain three positive integersa, b, C IND Icating the edge which is able to connect with the nodesa and b, and unit price C.

All numbers in input is less than 216.

Output

For each test case, the output an integer indicating the minimum possible cost for the tree in one line. If there is no-to-build a Christmas tree, print "No Answer" in the 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


The problem: In fact, is to seek the shortest path, anyway, I did not read, it seems that others feel and the topic is completely different.


SPFA (array adjacency table):

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector >using namespace Std; Const long Long INF = 0x3ffffffffffff;struct node{int to;int cost; Node () {}node (int a,int b) {to = A;cost = b;}}; int First[50005];int next[100005]; Node e[100005];bool Visited[50005];long Long D[50005];long long w[50005];void SPFA (int n) {memset (visited,false,sizeof ( visited)); for (int i = 1;i <= n;i++) {d[i] = INF;} D[1] = 0;queue<int> Q;q.push (1); visited[1] = True;while (!q.empty ()) {int x = Q.front (); Q.pop (); visited[x] = false; for (int i = first[x];i! = -1;i = Next[i]) {int t = e[i].to;if (D[t] > D[x] + e[i].cost) {d[t] = D[x] + e[i].cost;if (!visit Ed[t]) {Q.push (t); visited[t] = true;}}}} int main () {int Ncase;cin>>ncase;while (ncase--) {int n,m;scanf ("%d%d", &n,&m); for (int i = 1;i <= n;i++) { First[i] = -1;scanf ("%lld", w + i);} int k = 0;for (int i = 0;i < m;i++) {int u,v,c;scanf ("%d%d%d", &u,&v,&c); e[k].to = V;e[k].cost = C;nexT[K] = First[u];first[u] = k++;e[k].to = U;e[k].cost = C;next[k] = first[v];first[v] = k++;} if (n = = 0 | | m = = 0) {printf ("0\n"); continue;} SPFA (n); bool flag = true;long long ans = 0;for (int i = 1;i <= n;i++) {if (d[i] = = INF) {flag = False;break;} Ans + = d[i] * w[i];} if (flag) {printf ("%lld\n", ans);} else{printf ("No answer\n");}} return 0;}

Djistra (array adjacency table):

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector >using namespace Std; Const __int64 INF = 10000000000;struct node{int to;__int64 cost; Node () {}node (int A,__int64 b) {to = A;cost = b;} BOOL operator< (Node t) const{return cost > T.cost;}}; int First[50005];int next[100005]; Node e[100005];bool Visited[50005];__int64 d[50005];__int64 w[50005];__int64 ans = 0;void Djistra (int n) {memset (visited , false,sizeof (visited)); for (int i = 1;i <= n;i++) {d[i] = INF;} D[1] = 0;priority_queue<node> q;q.push (node (1,0)), ans = 0;int cnt = 0;while (!q.empty ()) {Node p = q.top (); Q.pop (); if (Visited[p.to]) {continue;} Cnt++;ans + = d[p.to] * W[p.to];visited[p.to] = true;for (int i = first[p.to];i! = -1;i = Next[i]) {int x = e[i].to;if (!visit ED[X] && d[x] > d[p.to] + e[i].cost) {d[x] = d[p.to] + e[i].cost;q.push (Node (x,d[x]));}}} if (cnt! = N) {ans =-1;}} int main () {int Ncase;cin>>ncase;while (ncase--) {int n,m;scanf ("%d%d", &n,&AMP;M); for (int i = 1;i <= n;i++) {First[i] = -1;scanf ("%i64d", w + i);}    int k = 0;for (int i = 0;i < m;i++) {int U,v;__int64 c;scanf ("%d%d%i64d", &u,&v,&c); e[k].to = V;e[k].cost = C;next[k] = First[u];first[u] = k++;e[k].to = U;e[k].cost = C;next[k] = first[v];first[v] = k++;} if (n = = 0 | | n = = 1) {printf ("0\n"); continue;} Djistra (n); if (ans! =-1) {printf ("%i64d\n", ans);} else{printf ("No answer\n");}} return 0;}

Time-out vector:

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector > #include <utility>using namespace std; Const __int64 INF = 0x3fffffffffff;struct node{int to;int cost; Node () {}node (int a,int b) {to = A;cost = b;} BOOL operator< (Node t) const{return cost > T.cost;}}; typedef Pair<__int64,int>plli;priority_queue<plli, vector<plli>, greater<plli> > Q;vector <Node> vec[50005];bool visited[50005];__int64 d[50005];int w[50005];__int64 ans;void djistra (int n) {memset ( Visited,false,sizeof (visited)); for (int i = 1;i <= n;i++) {d[i] = INF;} D[1] = 0;ans = 0;priority_queue<node> q;q.push (node (1,0)); int cnt = 0;while (!q.empty ()) {Node p = q.top (); Q.pop (); if (Visited[p.to]) {continue;} Ans + = w[p.to] * D[p.to];cnt++;visited[p.to] = true;for (int i = 0;i < Vec[p.to].size (); i++) {int x = vec[p.to][i].to;if ( !VISITED[X] && d[x] > d[p.to] + vec[p.to][i].cost) {d[x] = d[p.to] + vec[p.to][i].cost;q.push(Node (x,d[x]));}}} if (cnt! = N) {ans =-1;}} int main () {int Ncase;cin>>ncase;while (ncase--) {int n,m;scanf ("%d%d", &n,&m); for (int i = 1;i <= n;i++) { scanf ("%d", w + i);} for (int i = 0;i < m;i++) {int u,v,c;scanf ("%d%d%d", &u,&v,&c); Vec[u].push_back (Node (v,c)); Vec[v].push_ Back (Node (u,c));} if (n = = 0 | | m = = 0) {printf ("0\n"); continue;} Djistra (n); if (ans! =-1) {printf ("%i64d\n", ans);} else{printf ("No answer\n");} for (int i = 1;i <= n;i++) {vec[i].clear ();}} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Big Christmas Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.