poj 3013 Big Christmas Tree 最短路

本文深入探讨了大数据开发领域的关键技术,包括Hadoop、Spark、Flink等,并结合实际案例阐述了这些技术在数据处理、分析和存储方面的应用。通过详细解析数据流处理流程和系统架构,读者可以全面了解大数据技术的最新发展和最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Big Christmas Tree
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 18039 Accepted: 3811

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


W12*(V2+V4)+W13*(V3)+W24*(V4)化简得V2*W12+V3*W13+V4*(W12+W24)

因此最小的成本就是点1到点k(2<=k<=n)的最短距离乘上k的权值之和。

注意,n=0的树仍然是棵树,成本为0。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int maxn=155555;
const int INF=-1;

struct NODE
{
    int to;
    long long w;
    int next;
}link[maxn];

int edge,node,src,dest;
long long dist[maxn];
int head[maxn];
bool visit[maxn];
int x[maxn];
int outque[maxn];

queue<int>que;

void prepare(int _node,int _src)
{
    node=_node;
    src=_src;
    for (int i=0;i<=node;i++) head[i]=-1;
    edge=0;
}

void addedge(int u,int v,int c)
{
    link[edge].w=c;link[edge].to=v;link[edge].next=head[u];head[u]=edge++;
    link[edge].w=c;link[edge].to=u;link[edge].next=head[v];head[v]=edge++;
}

bool SPFA()
{
    int top;
    for (int i=0;i<=node;i++)
    {
        dist[i]=INF;
    }
    memset(visit,0,sizeof(visit));
    memset(outque,0,sizeof(outque));
    while (!que.empty()) que.pop();
    que.push(src);
    visit[src]=true;
    dist[src]=0;
    while (!que.empty())
    {
        top=que.front();
        que.pop();
        visit[top]=false;
        outque[top]++;
        if (outque[top]>node) return false;
        int k=head[top];
        while (k!=-1)
        {
            if ( dist[link[k].to]==INF||dist[link[k].to]>dist[top]+link[k].w )
            {
                dist[link[k].to]=dist[top]+link[k].w;
                if (!visit[link[k].to])
                {
                    visit[link[k].to]=true;
                    que.push(link[k].to);
                }
            }
            k=link[k].next;
        }
    }
    return true;
}

int main()
{
    int T,v,e;
    long long ans;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&v,&e);
        prepare(v,1);
        for (int i=1;i<=v;i++)
        {
            scanf("%d",&x[i]);
        }
        for (int i=1;i<=e;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c);
        }
        bool ret=SPFA();
        /*
        for (int i=1;i<=node;i++)
        {
            cout<<dist[i]<<endl;
        }
        */
        ans=0;
        for (int i=2;i<=node;i++)
        {
            if (dist[i]==INF)
            {
                ret=false;
                break;
            }
            ans+=dist[i]*x[i];
        }
        if (node==0||node==1){ret=true;ans=0;}
        if (ret) printf("%I64d\n",ans);
        else printf("No Answer\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值