POJ---3013 Big Christmas Tree[数据超大,谨慎小心]

 

Big Christmas Tree
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 17317   Accepted: 3644

Description

POJ---3013 Big Christmas Tree[数据超大,谨慎小心]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

Source

POJ Monthly--2006.09.29, Kim, Chan Min (kcm1700@POJ)

 

 

 

题意:从各点到1的最短路径乘以权值就是结果了。

 

 

 

code:

  1 #include<iostream>

  2 #include<queue>

  3 using namespace std;

  4 

  5 #define MAXN 50010

  6 #define inf 1LL<<61            //这个inf必须用这个,不然会WA

  7 

  8 typedef struct edge

  9 {

 10     long long a,b;

 11     long long dis;

 12     int next;

 13 }Edge;

 14 Edge e[MAXN*6];

 15 

 16 long long head[MAXN];

 17 long long w[MAXN];

 18 long long v,ei;

 19 long long edgenum;

 20 

 21 void addedge(long long s,long long t,long long k)

 22 {

 23     e[edgenum].a=s;e[edgenum].b=t;e[edgenum].dis=k;e[edgenum].next=head[s];head[s]=edgenum++;

 24     e[edgenum].a=t;e[edgenum].b=s;e[edgenum].dis=k;e[edgenum].next=head[t];head[t]=edgenum++;

 25 }

 26 

 27 long long dis[MAXN];

 28 bool vst[MAXN];

 29 void spfa()

 30 {

 31     for(int i=1;i<=MAXN;i++)

 32     {

 33         dis[i]=inf;

 34         vst[i]=false;

 35     }

 36     dis[1]=0;

 37     vst[1]=true;

 38     queue<long long>Q;

 39     Q.push(1);

 40     while(!Q.empty())

 41     {

 42         long long temp=Q.front();

 43         Q.pop();

 44         vst[temp]=true;

 45         for(int i=head[temp];i!=-1;i=e[i].next)

 46         {

 47             if(dis[e[i].b]>dis[temp]+e[i].dis)

 48             {

 49                 dis[e[i].b]=dis[temp]+e[i].dis;

 50                 if(!vst[e[i].b])

 51                 {

 52                     vst[e[i].b]=1;

 53                     Q.push(e[i].b);

 54                 }

 55             }

 56         }

 57         vst[temp]=false;

 58     }

 59     bool flag=true;

 60     long long sum=0;

 61     for(int i=2;i<=v;i++)

 62     {

 63         if(dis[i]<inf)

 64         {

 65             sum+=dis[i]*w[i];

 66         }

 67         else

 68         {

 69             flag=false;

 70             break;

 71         }

 72     }

 73     if(flag)

 74         printf("%I64d\n",sum);

 75     else

 76         printf("No Answer\n");

 77 }

 78 

 79 int main()

 80 {

 81     int t;

 82     scanf("%d",&t);

 83     while(t--)

 84     {

 85         memset(head,-1,sizeof(head));

 86         scanf("%I64d%I64d",&v,&ei);

 87         int i;

 88         edgenum=0;

 89         for(i=1;i<=v;i++)

 90             scanf("%I64d",&w[i]);

 91         for(i=0;i<ei;i++)

 92         {

 93             long long s,t,k;

 94             scanf("%I64d%I64d%I64d",&s,&t,&k);

 95             addedge(s,t,k);

 96         }

 97         spfa();

 98     }

 99     return 0;

100 }

 

你可能感兴趣的:(tree)