zoukankan      html  css  js  c++  java
  • POJ 3013 Big Christmas Tree

                                    Big Christmas Tree
    Time Limit: 3000MS   Memory Limit: 131072K
    Total Submissions:26471   Accepted: 5745

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

    思路
    求出各个点的最短路,分别乘上每个点的权值在累和就行了
    注意n==1与n==0的情况,要输出0
    这题是我第一次套板子,以前我做题全是手撸,可是我还是差不多wa了一页,我还以为是我套板子的报应。。。。。

    代码
    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn = 50086*4;
    const long long inf = 2100000000000000;
    
    int u[maxn],v[maxn];
    long long w[maxn];
    int first[maxn],Next[maxn];
    long long dis[maxn];
    long long pri[maxn];
    bool book[maxn];
    int n,m;
    struct node
    {
        int num;
        long long dis;
    
        bool operator<(const node x)const
        {
            return dis>x.dis;
        }
    };
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&n,&m);
            memset(pri,0,sizeof(pri));
            for(int i=1;i<=n;i++){
                scanf("%lld",&pri[i]);
            }
            memset(book,0,sizeof(book));
            memset(first,-1,sizeof(first));
            for(int i=1;i<=m;i++){
                scanf("%d%d%lld",&u[i],&v[i],&w[i]);
            }if(n==0||n==1){printf("0
    ");continue;}
            for(int i=1;i<=m;i++){
                u[i+m]=v[i];
                v[i+m]=u[i];
                w[i+m]=w[i];
            }
            for(int i=1;i<=n;i++){
                first[i]=-1;
            }
    
            fill(dis,dis+50086,inf);
            for(int i=1;i<=2*m;i++){
                Next[i]=first[u[i]];
                first[u[i]]=i;
            }
            priority_queue<node>q;
            node exa;
            exa.dis=0;
            exa.num=1;
            dis[1]=0;
            q.push(exa);
            int k;
            while(!q.empty()){
                exa=q.top();q.pop();
                if(book[exa.num]){continue;}
                k=first[exa.num];
                book[exa.num]=true;
                while(k!=-1){
                    if(dis[v[k]]>dis[u[k]]+w[k]&&book[v[k]]==false){
                        dis[v[k]]=dis[u[k]]+w[k];
                        exa.dis=dis[v[k]];
                        exa.num=v[k];
                        q.push(exa);
                    }
                    k=Next[k];
                }
            }
            long long ans=0;
            for(int i=2;i<=n;i++){
                ans+=dis[i]*pri[i];
                if(dis[i]==inf){
                    ans=-1;break;
                }
            }
            if(ans==-1||n==0){printf("No Answer
    ");}
            else printf("%lld
    ",ans);
        }
    }
    

      

  • 相关阅读:
    从0开始学习C#第三天
    金盾视频加密器V2014视频加密原理分析
    从0开始学习C#第二天
    从0开始学习C#第一天
    hook NtTerminateProcess进行应用的保护
    Wireshark简单使用教程3——附视频
    Wireshark简单使用教程2——附视频
    Wireshark简单使用教程1——附视频
    一个bat病毒分析(part1)
    社团的CTF逆向题WriteUp
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/9377537.html
Copyright © 2011-2022 走看看