Big Christmas Tree
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 17532   Accepted: 3702

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 v, 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 integers a, b, c 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

代码:

#include<stdio.h>
#include<string.h>
#define MAXN 50005
typedef struct
{
   int to;
   int next;
   int w;   
}Edge;
Edge e[MAXN*2];
int T,n,m,cnt;
__int64 sum;
int visit[MAXN];
int pn[MAXN];
int weight[MAXN]; //顶点的权值
int flag;
__int64 dis[MAXN];
int que[MAXN];

//---------------------------构图
void addedge(int from,int to,int w)
{
  e[cnt].to=to;
  e[cnt].w=w;
  e[cnt].next=pn[from];
  pn[from]=cnt++;
}


//---------------------------SPFA算法
void SPFA() //返回1到n的最短路径  dist[] 存储1到各顶点的距离
{
    int head=0,tail=0;
 que[tail++]=1;
 visit[1]=1;
 dis[1]=0;
 while(head!=tail)
 {
    int u=que[head];
    visit[u]=0;
    head++;
    for(int i=pn[u];i!=-1;i=e[i].next)
    {
       int v=e[i].to;
    if(dis[v]==-1||dis[v]>dis[u]+e[i].w)
    {
       dis[v]=dis[u]+e[i].w;
       if(!visit[v])
       {
       que[tail++]=v;
    visit[v]=1;
    }           
    }   
       }
     
    } 
}

int main()
{
   scanf("%d",&T);
   while(T--)
   {
      //初始化
      memset(dis,-1,sizeof(dis));//设为-1,不能设为inf
   memset(visit,0,sizeof(visit));
   memset(pn,-1,sizeof(pn));
   scanf("%d%d",&n,&m);
   int a,b,c;
   for(int i=1;i<=n;i++)
   scanf("%d",&weight[i]);
   cnt=1;
   while(m--)
   {
      scanf("%d%d%d",&a,&b,&c);
   addedge(a,b,c);
   addedge(b,a,c);    
      }
   SPFA();
   flag=1;
   sum=0;
   for(int i=1;i<=n;i++)
   {
    if(dis[i]==-1)
    {
   flag=0;
     break;
    }
    else
    sum+=weight[i]*dis[i];   
      } 
   if(flag==1)
   printf("%I64d\n",sum);
   else
   printf("No Answer\n");  
   }  
   return 0;
}

链接:http://poj.org/problem?id=3013