POJ 3013

Big Christmas Tree
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 19379 Accepted: 4139

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 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)


给你一个图,求一个以1节点为根节点的生成树,使得这个“(sum of weights of all descendant nodes) × (unit price of the edge)”最小。

一开始以为是小生成树,后来发现是最短路树问题。

下面我们来简要的分析一下,考虑如下的三元环:




                                                                                        

因为树是没有环的对于所有的环我们都要进行破环操作,而三元环是最简单的情况。如图,1为根节点,a,b为两个权重为a,b的节点,三条边权重为x,y,z。

现在有如下三种情况

1、断x,那么最后的代价是:

wx = z ( a + b ) + ya

2、断y,那么最后的代价是:

wy = xa+bz

3、断z,那么最后的代价是:

wz = x (a + b ) + yb


现在我们只分析如果最后取断x要满足的条件,其他的同理


wx<=wy && wx<=wz

wx<=wy 等价于 y + z <=  x

wx<=wz 等价于 za + zb + ya <= xa + xb + yb

如果 z + y <= x成立,则有如下两个式子成立:

1、 za + ya <= xa

2、 z + y <= x   →   z <= x - y   →   z <= x + y   →    zb <= xb + yb

如果上述两个式子成立则 wx <= wz成立。也就是说断x(取y,z)的条件就是 y + z  <= x,也就是 y,z是上图最短路树上的点。


那么算法就是先从1节点求一次单源最短路,然后在最短树上算出结果。


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define maxn 50009
#define maxm 100009
#define INF 999999999999999LL
using namespace std;
int first[maxn];
int u[maxm],v[maxm],next[maxm];
__int64 w[maxm];
int n,m,tot;
int wi[maxn],fa[maxn];
bool inq[maxn];
__int64 d[maxn],dp[maxn],ans;
void add(int x,int y,__int64 z)
{
    u[tot]=x,v[tot]=y,w[tot]=z;
    next[tot]=first[x];
    first[x]=tot++;
}
void spfa()
{
    queue<int>q;
    for(int i=1;i<=n;i++)d[i]=INF;
    d[1]=0;
    q.push(1);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        inq[x]=0;
        for(int e=first[x];e!=-1;e=next[e])
        {
            if(d[v[e]]>d[x]+w[e])
            {
                fa[v[e]]=x;
                d[v[e]]=d[x]+w[e];
                if(!inq[v[e]])
                {
                    inq[v[e]]=1;
                    q.push(v[e]);
                }
            }
        }
    }
}
void dfs(int cur)
{
    dp[cur]=wi[cur];
    for(int e=first[cur];e!=-1;e=next[e])
    {
        dfs(v[e]);
        dp[cur]+=dp[v[e]];
        ans+=w[e]*dp[v[e]];
    }
}
int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&wi[i]);
        memset(first,-1,sizeof(first));
        tot=0;
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        spfa();
        memset(first,-1,sizeof(first));
        tot=0;
        bool ok=1;
        for(int i=2;i<=n;i++)
        {
            if(d[i]==INF)
            {
                ok=0;break;
            }
            else
            {
                add(fa[i],i,d[i]-d[fa[i]]);
            }
        }
        if(!ok)
        {
            printf("No Answer\n");
        }
        else
        {
            ans=0;
            dfs(1);
            printf("%I64d\n",ans);
        }

    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值