poj 3013 Big Christmas Tree (最短路径树)

来源:互联网 发布:科技部办公厅关于优化 编辑:程序博客网 时间:2024/05/20 02:54

Big Christmas Tree

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>#include<queue>#include<algorithm>using namespace std;typedef long long LL;const LL inf=0x3f3f3f3f3f3f3f3f;const LL maxn=50000+10;struct edge{    LL v,w,next;} E[maxn<<1];LL first[maxn],d[maxn],inq[maxn],val[maxn];LL n,m,len;void spfa(){    memset(inq,0,sizeof(inq));    memset(d,inf,sizeof(d));    queue<LL>q;    LL u=1;    q.push(u),inq[u]=1,d[u]=0;    while(!q.empty())    {        u=q.front();        q.pop(),inq[u]=0;        for(LL i=first[u]; ~i; i=E[i].next)        {            LL v=E[i].v,w=E[i].w;            if(d[v]>d[u]+w)            {                d[v]=d[u]+w;                if(!inq[v])                {                    inq[v]=1;                    q.push(v);                }            }        }    }}void add_edge(LL u,LL v,LL w){    E[len].v=v,E[len].w=w,E[len].next=first[u],first[u]=len++;}int main(){    LL t;    scanf("%lld",&t);    while(t--)    {        memset(first,-1,sizeof(first));        scanf("%lld%lld",&n,&m);        for(LL i=1; i<=n; ++i)            scanf("%lld",&val[i]);        LL u,v,w;        len=0;        for(LL i=1; i<=m; ++i)        {            scanf("%lld%lld%lld",&u,&v,&w);            add_edge(u,v,w);            add_edge(v,u,w);        }        spfa();        LL ans=0;        for(LL i=2; i<=n; ++i)        {            if(d[i]==inf)            {                ans=inf;                break;            }            else ans+=d[i]*val[i];        }        if(ans==inf)            printf("No Answer\n");        else            printf("%lld\n",ans);    }    return 0;}
原创粉丝点击