POJ 3013 Big Christmas Tree "Shortest path distortion, Dijkstra heap optimization +SPFA algorithm"

Source: Internet
Author: User



Big Christmas Tree
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 23064 Accepted: 4987

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 through n. 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 numbers v, 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 Lin E. On the following e lines, each line contain three positive integers a, b,C in Dicating the edge which is able to connect with the nodes a 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

Source

POJ monthly--2006.09.29, Kim, Chan Min ([email protected])
original title link: http://poj.org/problem?id=3013the problem is difficult to understand, I heard that there is a Chinese version of the topic, Harbin University of Science and Technology OJ1419http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1419
later in http://blog.sina.com.cn/s/blog_6760701101010ubf.html this blog to see a more detailed explanation, also learned TA code, Dijkstra heap optimization algorithm +SPFA algorithm.
Test Instructions : To build a Christmas tree, so that the total cost is minimal. The rule is: the Christmas tree is a non-tree, wherein the node numbered 1 is the root node, each edge of the original diagram has Benquan (unit): The value of the units of the material; each point also has a weight (weight): The amount of the point. In the spanning tree, the cost of each edge is the amount of weight (weight) in the Benquan of that edge, and the total cost is the sum of the cost of all the edges in the spanning tree.
Analysis: 1, in the tree, the path of two points is unique
2, to point U, only the edge from the point u to the root node will be multiplied by the weight of the point U
3, the weight of the point U is unchanged
Conclusion : Minimum Total Cost = weight of each node in the subtree of each edge (U,V) *v
= Weight per point * unit value of each edge from this point to the root node
= Each point * The shortest point to the root node

Attached: Calculation method in the sample
4*40+3*50+2*60+3* (20+40+50+60) +2*30+1* (10+20+30+40+50+60)
=10*1+20* (1+3) +30* (2+1) +40* (4+1+3) +50* (3+1+3) +60* (1+2+3)
=10+80+90+320+350+360
=1210
Dijkstra algorithm heap optimized AC code:

#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; const int Maxn=50000+5;const Long Long inf=0x3f3f3f3f3f;int weight[maxn];int cnt;int Head[maxn];long Long Dis[maxn];bool    Vis[maxn];int n,m;struct edge{int to,w,next;} edge[maxn<<1];struct node{int u,dis;        The dis here is only for use with Priority queue: Define priority BOOL Operator < (const Node &a) Const {return dis>a.dis;    The heap is updated when True is returned, so when the dis of the new element A is less than the heap top element dis//, it returns true while the heap is updated, so the heap is a small top heap}};void Init () {cnt=0;    memset (head,-1,sizeof (head));    memset (vis,false,sizeof (VIS)); for (int i=0; i<=n; i++) Dis[i]=inf;}    void Addedge (int u,int v,int W) {edge[cnt].to=v;    Edge[cnt].w=w;    Edge[cnt].next=head[u]; head[u]=cnt++;}    void Dijkstra (int s) {Node now,next;    priority_queue<node>q;    now.dis=0;    Now.u=s;    dis[s]=0;    Q.push (now);        while (!q.empty ()) {now=q.top ();        Q.pop ();        if (vis[now.u])    Continue        int u=now.u;        Vis[u]=true;            for (int i=head[u]; i!=-1; i=edge[i].next) {int to=edge[i].to;            For Point V, there will be multiple dis exists, but we only take the top element,//That is, the dis smallest point v if (!vis[to]&&dis[u]+edge[i].w<dis[to])                {DIS[TO]=DIS[U]+EDGE[I].W;                Next.dis=dis[to];                Next.u=to;            Q.push (next);    }}}}int Main () {int T;    cin>>t;        while (t--) {cin>>n>>m;        Init ();        for (int i=1; i<=n; i++) scanf ("%d", &weight[i]);        int u,v,w;            for (int i=1; i<=m; i++) {scanf ("%d%d%d", &u,&v,&w);            Addedge (U,V,W);        Addedge (V,U,W);        } Dijkstra (1);        Long Long sum=0;        BOOL Flag=true;                for (int i=2; i<=n; i++) {if (dis[i]==inf) {flag=false;            Break      }      Sum+=dis[i]*weight[i];        } if (flag) cout<<sum<<endl;    else cout<< "No Answer" <<endl; } return 0;}


SPFA algorithm AC code
#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; const int Maxn=50000+5;const __int64 inf=0x3f3f3f3f3f;int weight[maxn];int cnt;int head[maxn];__int64 dis[maxn];bool Vis[maxn];int n,m;struct edge{int to,next,w;}    Edge[maxn<<1];void Init (void) {cnt=0;    memset (head,-1,sizeof (head));    memset (vis,false,sizeof (VIS)); for (int i=0;i<=n;i++) Dis[i]=inf;}    void Addedge (int u,int v,int W) {edge[cnt].to=v;    Edge[cnt].w=w;    Edge[cnt].next=head[u]; head[u]=cnt++;}    void SPFA (int s) {queue<int>q;    Q.push (s);    Vis[s]=true;    dis[s]=0;        while (!q.empty ()) {int P=q.front ();        Q.pop ();        Vis[p]=false;            for (int i=head[p];i!=-1;i=edge[i].next) {int to=edge[i].to;            int W=EDGE[I].W;                if (Dis[p]+w<dis[to]) {dis[to]=dis[p]+w; if (!vis[to]) {Q.push (to);                Vis[to]=true;    }}}}}int main () {int T;    cin>>t;        while (t--) {cin>>n>>m;        Init ();        for (int i=1; i<=n; i++) scanf ("%d", &weight[i]);        int u,v,w;            for (int i=1; i<=m; i++) {scanf ("%d%d%d", &u,&v,&w);            Addedge (U,V,W);        Addedge (V,U,W);        }//dijkstra (1);        SPFA (1);        __int64 sum=0;        BOOL Flag=true;                for (int i=2; i<=n; i++) {if (dis[i]==inf) {flag=false;            Break        } Sum+=dis[i]*weight[i];        } if (flag) cout<<sum<<endl;    else cout<< "No Answer" <<endl; } return 0;}



POJ 3013 Big Christmas Tree "Shortest path distortion, Dijkstra heap optimization +SPFA algorithm"

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.