zoukankan      html  css  js  c++  java
  • POJ 3013 Big Christmas Tree (最短路)

    Big Christmas Tree
    Time Limit: 3000MS   Memory Limit: 131072K
    Total Submissions: 22903   Accepted: 4962

    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

    这题以前用Dijkstra+stl做过,今天学习下手写heap,重做了一遍
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    
    const int MAXN = 50000+100;
    const long long INF = 10000000000;
    typedef pair<long long,int>plli;
    struct Edge
    {
        int to,next;
        long long w;
    }edge[MAXN*2];
    int head[MAXN],tol;
    long long dis[MAXN];
    long long val[MAXN];
    bool vis[MAXN];
    plli heap[MAXN*2];
    int num;
    int n,m;
    
    void init()
    {
        tol=0;
        num=0;
        memset(head,-1,sizeof(head));
    }
    void addedge(int u,int v,long long w)
    {
        edge[tol].to=v;
        edge[tol].w=w;
        edge[tol].next=head[u];
        head[u]=tol++;
    }
    
    int cmp(plli x,plli y)
    {
        return x.first<y.first;
    }
    plli top()
    {
        return heap[1];
    }
    void push(plli x)
    {
        int par,son;
        plli temp=x;
        heap[++num]=x;
        for(son=num,par=son/2;son>1;son=par,par=son/2)
        {
            if(cmp(temp,heap[par])) //儿子更小,向上移动
                heap[son] = heap[par]; //父亲滑下来覆盖儿子
            else break; //终于儿子已经比父亲大了,那么不用再比较了
        }
        heap[son]=temp;
    }
    void pop()
    {
        int son,par;
        plli temp;
        heap[1]=heap[num--];
        temp=heap[1];
        for(par=1,son=par*2;son<=num;par=son,son=par*2)
        {
            if(son<num&&cmp(heap[son+1],heap[son])) son++;
            if(cmp(temp,heap[son])) break;
            heap[par]=heap[son];
        }
        heap[par]=temp;
    }
    
    long long Dijkstra()
    {
        int cnt=0;
        long long res=0;
        for(int i=1;i<=n;i++) dis[i]=INF,vis[i]=false;
        dis[1]=0;
        push(make_pair(dis[1],1));
        while(num>0)
        {
            int u,v;
            long long w;
            plli x=top();
            pop();
            u=x.second;
            if(vis[u]) continue;
            vis[u]=true;
            cnt++;
            res+=dis[u]*val[u];
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                v=edge[i].to;
                w=edge[i].w;
                if(dis[u]+w<dis[v])
                {
                    dis[v]=dis[u]+w;
                    push(make_pair(dis[v],v));
                }
            }
        }
        if(cnt<n) return -1;
        return res;
    }
    
    int main()
    {
        int T;
        int u,v;
        long long w;
        scanf("%d",&T);
        while(T--)
        {
            init();
            scanf("%d%d",&n,&m);
            for(int i=1; i<=n; i++) scanf("%lld",&val[i]);
            if(n==0||n==1)
            {
                printf("0
    ");
                continue;
            }
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%lld",&u,&v,&w);
                addedge(u,v,w);
                addedge(v,u,w);
            }
            long long res=Dijkstra();
            if(res!=-1) printf("%lld
    ",res);
            else   printf("No Answer
    ");
        }
        return 0;
    }
     
  • 相关阅读:
    Vue 4.0及以上修改默认8080端口号
    EasyRTC进入会议室视频父组件传递数据给子组件显示为null的问题修复
    EasyRTC通过Golang缓存库fastcache实现在线用户存储在内存中加快速度
    视频远程通话系统EasyRTC 日志显示调用位置不正确如何优化?
    通过浏览器使用WebRTC时会话终止或断开都是什么原因?
    为什么我们要用BS架构来开发流媒体平台?
    TSINGSEE青犀视频流媒体平台为什么会存在跨域问题?
    EasyDSS/EasyNVR传输高清视频如何优化及节省带宽消耗?
    【解决方案】TSINGSEE青犀视频AI+智慧工地,助力工地安全生产
    【解决方案】TSINGSEE青犀视频助力危化企业安全生产,实现AI全流程监管
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5716523.html
Copyright © 2011-2022 走看看