• poj 3013 Big Christmas Tree 最短路


    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;
    }
    


  • 相关阅读:
    Tensorflow学习笔记1
    强化学习——从最简单的开始入手
    MATLAB R2017a 安装与破解
    C# 理解lock
    Bayer Pattern
    OpenCV参考手册之Mat类详解
    opencv学习之颜色空间转换cvtColor()
    UNICODE下CString转string
    解决VS2013报错fopen、sprintf等函数安全的问题
    Convert between cv::Mat and QImage 两种图片类转换
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038385.html
Copyright © 2020-2023  润新知