POJ 3013 Big Christmas Tree(dijkstra 堆优化)

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、这道题大概花了我一天时间= =上午通过看别人的优化大概搞清楚了思想,然后便参考边敲出了第一遍代码,果然连测试样例都没过,然后看看书发现书上的格式更好理解一点,便稍微结合了一下,记录边的编号是用的vector,edge就直接没变。这样测试样例终于过了,开始了下午的漫漫debug路,其中真的是各种怀疑人生,写的差不多的套路为啥我就是wa,后来发现我把初始化的函数写在读入n,m之前,这样的话在初始化时用到n结果就是错的了。之后还是错,原来是我inf开小了,结果是longlong的时候inf要开的更大。
2、这道题其实我是一开始就看了答案,因为没有一点思路。其实我觉得要想到用最短路径做就挺难的了,因为它计算的条件很多,经过分析可以发现

1、在树中,两点的路径是唯一的
2、对点u,只有从点u到根结点之间的边会乘以点u的重量
3、点u的重量不变
结论:最小总花费=每条边(u,v)*v的子树中各结点的重量
=每个点的重量*从该点到根结点的每条边的单位价值
=每个点*该点到根结点的最短路

3、dijkstra的优化就是用优先队列,这样的话将条件设为边长小的优先出队列,就不需要经过循环找最小的边了。在Node中就是重新定义了<,然后edge就是用来保存边的起始点、终止点、长度,G就是用来通过Node中的u来确定edge。
4、下面就是最后的代码,应该可以当个模板了

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值