poj 3013 Big Christmas Tree(Dijktra或者spfa)

16 篇文章 1 订阅

Big Christmas Tree

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

这个题真是个神题。。。坑点还不是一两个。。。

题意:

要建一棵圣诞树,使得总的花费最小。具体规则是:圣诞树是一颗无向树形图,其中,编号为1的节点为根节点,原始图中每条边具有边权(unit):材料的单位价值;每个点也有一个权(weight):点的重量。生成树中,各条边的花费是该边权(unit)* 该边的子树中所有点的重量(weight)和,总的花费则是生成树中所有边的花费之和。


思路:首先,最下面的1为根节点,我一开始把他当做了最小的子节点。以为下面的是上面的根节点。题意读完之后,没有什么思路,一般都会以为是最小生成树,但仔细想想最不可能就是最小生成树,如果把第二个样例过程写出来的话,仔细观察下思路就很简单了。

样例中的计算方法
4*40+3*50+2*60+3*(20+40+50+60)+2*30+1*(10+20+30+40+50+60)
=10*1+20*(1+3)+30*(2+1)+40*(4+1+3)+50*(3+1+3)+60*(1+2+3)
=10+80+90+320+350+360
=1210

会发现,最上面的节点也就是最小的子节点,他要参与前面所有根节点的“构成”,也就是最上面的节点,分别要乘以从节点1到自己之间所有必须经过的边权和,为了使价值最小就是最短路问题了。。。


坑点:1.注意点的范围是0-50000,包括0,当v = 0 或者1的时候,不应该输出wa,而是0.。。

2.当v = 0或者v = 1时候,不能还没有把所有数据都输入完就continue;否则会tle。。。

3.inf如果只是int的最大值会wa,应该longlong一个更大的数,这里wa哭了。。。

4.注意这是个无向图,存的时候要存两边



这是我用Dijkstra写的代码,主要我看着也不用判断有没有负环,也没有负权,用Dijkstra应该可以过,而我的两个队友不约而同的用了spfa。。。确实时间比我快了1倍。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 5e6 + 5;
const long long inf = 2000000000000;
int head[maxn], t, v, e, k, via[maxn];
long long dis[maxn];
struct node
{
    int v, w, pre;
    node(int vv, int ww) : v(vv),w(ww){};
    node(){}
    bool operator < (const node &a)  const
    {
        return w > a.w;
    }
}edge[maxn];
void addedge(int u, int v, int w)
{
    edge[k].v = v;
    edge[k].w = w;
    edge[k].pre = head[u];
    head[u] = k++;
}
void Dijkstra(int u)
{
    for(int i = 1; i <= v; i++)
        dis[i] = inf;
    dis[u] = 0;
    priority_queue<node> pq;
    pq.push(node(u,0));
    while(!pq.empty())
    {
        u = pq.top().v;
        pq.pop();
        for(int i = head[u]; i != -1; i = edge[i].pre)
        {
            int v = edge[i].v, w = edge[i].w;
            if(dis[u] + w < dis[v])
            {
                dis[v] = dis[u] + w;
                pq.push(node(v,dis[v]));
            }
        }
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(head,-1,sizeof(head));
        scanf("%d%d",&v,&e);

        for(int i = 1; i <= v; i++)
            scanf("%d",&via[i]);
        int x, y, z;
        int k = 1;
        while(e--)
        {
            scanf("%d%d%d",&x, &y, &z);
            addedge(x,y,z);
            addedge(y,x,z);
        }
      if(v == 0 || v == 1)
        {printf("0\n");continue;}
        Dijkstra(1);
       // cout << 1 << endl;
        int flag = 0;
        long long sum = 0;
        for(int i = 2; i <= v; i++)
        {
            if(dis[i] == inf)
                {flag = 1; break;}
            sum += dis[i] * via[i];
        }
        if(flag) printf("No Answer\n");
        else
            printf("%lld\n",sum);
    }
    return 0;
}

这是一个队友chenbin1写的,也不错spfa


#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const long long INF = 1e12;
const int maxn = 50005;
int k, n, w[maxn], head[maxn];
long long dis[maxn];
bool book[maxn];
struct node
{
    int v, next;
    long long w;
}edge[maxn*2];

void addEdge(int u, int v, long long w)
{
    edge[k].v = v;
    edge[k].w = w;
    edge[k].next = head[u];
    head[u] = k;
}

void spfa(int u)
{
    memset(book, 0, sizeof(book));
    for(int i = 1; i <= maxn; i++) dis[i] = INF;
    dis[u] = 0;
    queue<int> q;
    q.push(u);
    while(!q.empty())

    {
        u = q.front(); q.pop(); book[u] = 0;
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].v, w = edge[i].w;
            if(dis[v] > dis[u]+w)
            {
                dis[v] = dis[u]+w;
                if(!book[v])
                {
                    book[v] = 1;
                    q.push(v);
                }
            }
        }
    }
}
int main(void)
{
    int t, m;
    cin >> t;
    while(t--)
    {
        k = 0;
        memset(head, -1, sizeof(head));
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
        while(m--)
        {
            int x, y, d;
            scanf("%d%d%d", &x, &y, &d);
            addEdge(x, y, d);
            k++;
            addEdge(y, x, d);
            k++;
        }
        spfa(1);
        long long ans = 0;
        bool flag = 0;
        for(int i = 1; i <= n; i++)
        {
            if(dis[i] == INF) { flag = 1; break; }
            ans += dis[i]*w[i];
        }
        if(flag) printf("No Answer\n");
        else printf("%lld\n", ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值