POJ-3013: Big Christmas Tree(dij,spfa)

POJ-3013: Big Christmas Tree

题目链接:POJ:3013


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

题意:哈理工1419是本题的中文题意。

分析: 第二个样例
=404+602+503+40+60+50+203+302+10+20+30+40+50+601
=404+3+1+602+3+1+503+3+1+203+1+302+1101
得到:最小的答案=每个点*该点到根结点的最短路

注意: INF开到0x3f3f3f3f3f3f3f3f


  • dij
#include<queue>
#include<stdio.h>
#include<vector>
#include<string.h>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn=50005;
int vv[maxn];
int pos[maxn*2],vis[maxn],num;
LL dis[maxn],ans;
struct node1
{
    int en,val,next;
}a[maxn*2];
struct node
{
    int id,val;
    node(int idd=0,int vall=0)
    {
        id=idd;
        val=vall;
    }
    friend bool operator <(node x,node y)
    {
        return x.val>y.val;
    }
};
void add(int st,int en,int val)
{
    a[num].en=en;
    a[num].val=val;
    a[num].next=pos[st];
    pos[st]=num++;
}
void dij()
{
    dis[1]=0;
    priority_queue<node>q;
    q.push(node(1,dis[1]));
    while(!q.empty())
    {
        node now=q.top();
        q.pop();
        if(vis[now.id]) continue;
        vis[now.id]=1;
        for(int i=pos[now.id];i!=-1;i=a[i].next)
        {
            if(!vis[a[i].en]&&dis[a[i].en]>dis[now.id]+a[i].val)
            {
                dis[a[i].en]=dis[now.id]+a[i].val;
                q.push(node(a[i].en,dis[a[i].en]));
            }
        }
    }
}
void init()
{
    ans=0;
    num=0;
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    memset(pos,-1,sizeof(pos));
}
int main()
{
    int t;
    int v,e;
    int x,y,z;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&v,&e);
        for(int i=1;i<=v;i++)
            scanf("%d",&vv[i]);
        while(e--)
        {
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        dij();
        int flag=1;
        for(int i=1;i<=v;i++)
        {
            if(dis[i]==INF)
            {
                flag=0;
                break;
            }
            ans+=dis[i]*vv[i];
        }
        if(flag)
            printf("%lld\n",ans);
        else
            printf("No Answer\n");
    }
    return 0;
}
  • spfa
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn=50005;
int v,e;
int vv[maxn];
int cnt[maxn],vis[maxn];
struct node
{
    int en,val,next;
}a[maxn*2];
int pos[maxn],num;
LL dis[maxn],ans;
void add(int st,int en,int val)
{
    a[num].en=en;
    a[num].val=val;
    a[num].next=pos[st];
    pos[st]=num++;
}
void init()
{
    num=0;
    ans=0;
    memset(pos,-1,sizeof(pos));
}
void spfa()
{
    queue<int>q;
    for(int i=1;i<=v;i++)
        dis[i]=INF,vis[i]=0,cnt[i]=0;
    dis[1]=0;
    cnt[1]++;
    q.push(1);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=pos[u];i!=-1;i=a[i].next)
        {
            int w=a[i].en;
            int val=a[i].val;
            if(dis[w]>dis[u]+val)
            {
                dis[w]=dis[u]+val;
                if(!vis[w])
                {
                    vis[w]=1;
                    cnt[w]++;
                    if(cnt[w]>=v)
                        return;
                    q.push(w);
                }
            }
        }
    }
}
int main()
{
    int t;
    int x,y,z;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&v,&e);
        for(int i=1;i<=v;i++)
            scanf("%d",&vv[i]);
        while(e--)
        {
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        spfa();
        int flag=1;
        for(int i=1;i<=v;i++)
        {
            if(dis[i]==INF)
            {
                flag=0;
                break;
            }
            ans+=dis[i]*vv[i];
        }
        if(flag)
            printf("%lld\n",ans);
        else
            printf("No Answer\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值