poj 3013 Big Christmas Tree

Big Christmas Tree
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 20974   Accepted: 4535

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 ve (0 ≤ ve ≤ 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 abc 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

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

Source

POJ Monthly--2006.09.29, Kim, Chan Min (kcm1700@POJ)
 
题意&思路:无聊的题目,纯最短路。dijsktra+heap优化。存下来当模板
  1 /*
  2  * Author:  Joshua
  3  * Created Time:  2014年10月06日 星期一 20时39分47秒
  4  * File Name: poj3013.cpp
  5  */
  6 #include<cstdio>
  7 #include<queue>
  8 #include<cstring>
  9 #include<iostream>
 10 #include<algorithm>
 11 using namespace std;
 12 #define maxn 50005
 13 #define LLinf 0x7f7f7f7f7f7f7f7f
 14 typedef long long LL;
 15 struct edge
 16 {
 17     int v,w,next;
 18 } e[maxn<<1];
 19 struct node
 20 {
 21     LL d;
 22     int num;
 23     bool operator < (const node& pp) const
 24     { 
 25         return d<pp.d;
 26     }
 27 } heap[maxn];
 28 int T,n,m,tot,heapSize;
 29 int c[maxn],head[maxn],map[maxn];
 30 bool vis[maxn];
 31 void Read(int &ret){
 32     ret = 0;
 33     bool ok = 0;
 34     for( ; ;){
 35         int c = getchar();
 36         if (c >= '0' && c <= '9') ret = (ret << 3) + (ret << 1) + c - '0', ok = 1;
 37         else if (ok) return;
 38     }
 39 }
 40 
 41 void createEdge(int u,int v,int w)
 42 {
 43     edge& temp=e[++tot];
 44     temp.v=v;
 45     temp.w=w;
 46     temp.next=head[u];
 47     head[u]=tot;
 48 }
 49 
 50 void heap_swap(int x,int y)
 51 {
 52     swap(heap[x],heap[y]);
 53     map[heap[x].num]=x;
 54     map[heap[y].num]=y;
 55 }
 56 
 57 void heap_up(int x)
 58 {
 59     while (x!=1 && heap[x]<heap[x>>1])
 60     {
 61         heap_swap(x,x>>1);
 62         x>>=1;
 63     }
 64 }
 65 
 66 void heap_down(int x)
 67 {
 68     while ((x<<1)<=heapSize)
 69     {
 70         x<<=1;
 71         if (x+1<=heapSize && heap[x+1]<heap[x]) x++;
 72         if (heap[x]<heap[x>>1])
 73             heap_swap(x,x>>1);
 74         else break;
 75     }
 76 }
 77 
 78 void heap_del()
 79 {
 80     heap_swap(1,heapSize);
 81     heapSize--;
 82     heap_down(1);
 83 }
 84 
 85 void init()
 86 {
 87     int u,v,w;
 88     Read(n);Read(m);
 89     for (int i=1;i<=n;++i)
 90         Read(c[i]);
 91     memset(head,-1,(n+1)<<2);
 92     tot=0;
 93     for (int i=1;i<=m;++i)
 94     {
 95         Read(u);Read(v);Read(w);
 96         createEdge(u,v,w);
 97         createEdge(v,u,w);
 98     }
 99 }
100 
101 void updata(int x,LL y)
102 {
103     if (heap[x].d<=y) return;
104     heap[x].d=y;
105     heap_up(x);
106 }
107 
108 void solve()
109 {
110     bool flag=false;
111     LL td,ans=0;
112     int tn;
113     for (int i=2;i<=n;++i)
114     {
115         heap[i-1].d=LLinf;
116         heap[i-1].num=i;
117         map[i]=i-1;
118     }
119     heap[n].d=0;
120     heap[n].num=1;
121     map[1]=n;
122     heapSize=n;
123     heap_up(n);
124     memset(vis,1,n+1);
125     for (int i=1;i<=n;++i)
126     {
127         td=heap[1].d;
128         tn=heap[1].num;
129         vis[tn]=false;
130         if (td==LLinf) 
131         {
132             flag=true;
133             break;
134         }
135         heap_del();
136         ans+=td*c[tn];
137         for (int i=head[tn];~i;i=e[i].next)
138             if (vis[e[i].v])
139             updata(map[e[i].v],td+e[i].w);
140     }
141     if (flag) printf("No Answer\n");
142     else cout<<ans<<endl;
143 }            
144 
145 int main()
146 {
147     Read(T);
148     for (int i=1;i<=T;++i)
149     {
150         init();
151         solve();
152     }
153     return 0;
154 }

 

 

posted @ 2014-10-07 17:20  一个大叔  阅读(169)  评论(0编辑  收藏  举报