POJ 3013 Big Christmas Tree (spfa)

来源:互联网 发布:发货打印软件 编辑:程序博客网 时间:2024/05/20 04:15
Time Limit: 3000MS Memory Limit: 131072KTotal Submissions: 21891 Accepted: 4737

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 throughn. 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 numbersv,e (0 ≤ v, e ≤ 50000) are given in the first line of each test case. On the next line,v positive integerswi indicating the weights ofv nodes are given in one line. On the followinge lines, each line contain three positive integersa, b,c indicating the edge which is able to connect two nodesa 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

22 11 11 2 157 7200 10 20 30 40 50 601 2 12 3 32 4 23 5 43 7 23 6 31 5 9

Sample Output

151210

Source

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


构建一颗圣诞树,然后叫你求各个点的最短路*权值的和。
记录权值的数组开long long 就行。
#include<iostream>#include<algorithm>#include<map>#include<cstring>#include<cstdio>#include <queue>using namespace std;typedef long long ll;const int maxn=50005;int tot;int head[maxn*2+100];int vis[maxn*2+100];ll dis[maxn*2+100];struct node{    int u,v,next;    ll w;} edge[maxn*10+100];void add(int u,int v,ll w){    edge[tot].u=u;    edge[tot].v=v;    edge[tot].w=w;    edge[tot].next=head[u];    head[u]=tot++;    edge[tot].u=v;    edge[tot].v=u;    edge[tot].w=w;    edge[tot].next=head[v];    head[v]=tot++;}void spfa(int s){    memset(vis,0,sizeof(vis));    queue<int>que;    que.push(s);    vis[s]=1;    while(!que.empty())    {        int u=que.front();        que.pop();        vis[u]=0;        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v;            ll cost=edge[i].w;            if(dis[v]>dis[u]+cost)            {                dis[v]=dis[u]+cost;                if(!vis[v])                {                    vis[v]=1;                    que.push(v);                }            }        }    }}int main(){    int t,i,j;    ll w1[maxn];    scanf("%d",&t);    while(t--)    {        int n,m;        scanf("%d%d",&n,&m);        int a,b;        ll c;        for(i=1; i<=n; i++)        {            scanf("%lld",&w1[i]);            dis[i]=((ll)1<<63)-1;        }        tot=0;        dis[1]=0;        memset(head,-1,sizeof(head));        for(i=0; i<=2*m; i++)            edge[i].next=-1;        for(i=1; i<=m; i++)        {            scanf("%d%d%lld",&a,&b,&c);            add(a,b,c);        }        spfa(1);        int flag=0;        ll res=0;        for(i=1; i<=n; i++)        {            if(dis[i]==((ll)1<<63)-1)            {                flag=1;                break;            }            res+=dis[i]*w1[i];        }        if(flag)            printf("No Answer\n");        else            printf("%I64d\n",res);    }    return 0;}

0 0