poj 3013 Big Christmas Tree

时间:2022-01-22 06:44:08
Big Christmas Tree
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 20974   Accepted: 4535

Description

poj 3013 Big Christmas TreeChristmas 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优化。存下来当模板
 /*
* Author: Joshua
* Created Time: 2014年10月06日 星期一 20时39分47秒
* File Name: poj3013.cpp
*/
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 50005
#define LLinf 0x7f7f7f7f7f7f7f7f
typedef long long LL;
struct edge
{
int v,w,next;
} e[maxn<<];
struct node
{
LL d;
int num;
bool operator < (const node& pp) const
{
return d<pp.d;
}
} heap[maxn];
int T,n,m,tot,heapSize;
int c[maxn],head[maxn],map[maxn];
bool vis[maxn];
void Read(int &ret){
ret = ;
bool ok = ;
for( ; ;){
int c = getchar();
if (c >= '' && c <= '') ret = (ret << ) + (ret << ) + c - '', ok = ;
else if (ok) return;
}
} void createEdge(int u,int v,int w)
{
edge& temp=e[++tot];
temp.v=v;
temp.w=w;
temp.next=head[u];
head[u]=tot;
} void heap_swap(int x,int y)
{
swap(heap[x],heap[y]);
map[heap[x].num]=x;
map[heap[y].num]=y;
} void heap_up(int x)
{
while (x!= && heap[x]<heap[x>>])
{
heap_swap(x,x>>);
x>>=;
}
} void heap_down(int x)
{
while ((x<<)<=heapSize)
{
x<<=;
if (x+<=heapSize && heap[x+]<heap[x]) x++;
if (heap[x]<heap[x>>])
heap_swap(x,x>>);
else break;
}
} void heap_del()
{
heap_swap(,heapSize);
heapSize--;
heap_down();
} void init()
{
int u,v,w;
Read(n);Read(m);
for (int i=;i<=n;++i)
Read(c[i]);
memset(head,-,(n+)<<);
tot=;
for (int i=;i<=m;++i)
{
Read(u);Read(v);Read(w);
createEdge(u,v,w);
createEdge(v,u,w);
}
} void updata(int x,LL y)
{
if (heap[x].d<=y) return;
heap[x].d=y;
heap_up(x);
} void solve()
{
bool flag=false;
LL td,ans=;
int tn;
for (int i=;i<=n;++i)
{
heap[i-].d=LLinf;
heap[i-].num=i;
map[i]=i-;
}
heap[n].d=;
heap[n].num=;
map[]=n;
heapSize=n;
heap_up(n);
memset(vis,,n+);
for (int i=;i<=n;++i)
{
td=heap[].d;
tn=heap[].num;
vis[tn]=false;
if (td==LLinf)
{
flag=true;
break;
}
heap_del();
ans+=td*c[tn];
for (int i=head[tn];~i;i=e[i].next)
if (vis[e[i].v])
updata(map[e[i].v],td+e[i].w);
}
if (flag) printf("No Answer\n");
else cout<<ans<<endl;
} int main()
{
Read(T);
for (int i=;i<=T;++i)
{
init();
solve();
}
return ;
}