POJ 3013 Big Christmas Tree (dijkstra heap optimization)

Keywords: less

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 v, e (0 ≤ v, e ≤ 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 a, b, c 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

1. It took me about a day to figure out the idea by looking at other people's optimizations in the morning. Then I knocked out the first code by referring to it. Sure enough, I didn't even have a test sample. Then I read the book and found that the format on the book was better understood. I combined it a little. The number on the record side was vector, and the edge did not change directly. So the test sample finally passed, and began the long debug road in the afternoon. There are really all kinds of doubts about life. Why am I wa? Later, I found that I wrote the initialization function before reading in n and m, so I used the n result in initialization wrong. After that, it's still wrong. It turned out that I started inf smaller, but it turned out that inf would be bigger when I was long.
2. Actually, I read the answer from the beginning, because I have no idea. In fact, I think it's very difficult to think about using the shortest path, because it has a lot of conditions to calculate, which can be found through analysis.

1. In a tree, the path of two points is unique.
2. For point u, only the edge from point u to root node multiplies the weight of point U.
3. The weight of point u remains unchanged
CONCLUSION: The minimum total cost = the weight of each node in the subtree of each edge (u,v)*v
= the weight of each point * the unit value of each side from that point to the root node
= each point * the shortest path from that point to the root node

3. The optimization of dijkstra is to use priority queue. In this way, if the condition is set as the priority queue with small side length, there is no need to find the smallest side through a loop. In Node, we redefine <, then edge is used to save the starting point, ending point and length of edge, and G is used to determine edge by u in Node.
4. Here's the final code. It should be a template.

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#define maxn 50010
using namespace std;
const long long int inf=0x3f3f3f3f3f3f3f3f;
struct Edge
{
    int from, to, w;
};
Edge edge[maxn*2];
struct Node
{
    int u, dis;
    bool operator < (const Node &a) const
    {
        return a.dis<dis;
    }
};
int n, m, cnt;
int weight[maxn], vis[maxn];
long long int cst[maxn];
vector <int> G[maxn];
void Init()
{
    cnt=0;
    memset(vis, 0, sizeof(vis));
    for(int i=0; i<=n; i++)cst[i]=inf;
    for(int i=0; i<=n; i++)G[i].clear();
}
void addedge(int u, int v, int w)
{
    edge[cnt].from=u;
    edge[cnt].to=v;
    edge[cnt].w=w;
    G[u].push_back(cnt);
    cnt++;
}
void dij()
{
    Node now, next;
    priority_queue<Node>Q;
    now.dis=0;
    now.u=1;
    cst[1]=0;
    Q.push(now);
    while(!Q.empty()){
        now=Q.top();
        Q.pop();
        int u=now.u;
        if(vis[u])continue;
        vis[u]=1;
        for(int i=0; i<G[u].size(); i++)
        {
            Edge e=edge[G[u][i]];
            if(cst[u]+e.w<cst[e.to] && !vis[e.to])
            {
                cst[e.to]=cst[u]+e.w;
                next.dis=cst[e.to];
                next.u=e.to;
                Q.push(next);
            }
        }

    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d %d", &n, &m);
        Init();
        for(int i=1; i<=n; i++)
            scanf("%d", &weight[i]);
        for(int i=0; i<m; i++){
            int a, b, c;
            scanf("%d %d %d", &a, &b, &c);
            addedge(a, b, c);
            addedge(b, a, c);
        }
        if(n==0 || n==1) {printf("0\n"); continue;}
        dij();
        long long sum=0;
        int flag=0;
        for(int i=2; i<=n; i++){
            if(cst[i]==inf){flag=1; break;}
            sum+=cst[i]*weight[i];
            //printf("%lld %d\n", cst[i], weight[i]);
        }
        if(flag)printf("No Answer\n");
        else printf("%lld\n", sum);

    }
    return 0;
}

Posted by Sinister747 on Sun, 23 Dec 2018 23:03:05 -0800