http://poj.org/problem?id=3013

Big Christmas Tree
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 19009   Accepted: 4048

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

Source

POJ Monthly--2006.09.29, Kim, Chan Min (kcm1700@POJ)
 
【题解】:
  变向的最短路劲问题:
      题目意思其实就是求各点到1节点的 最短路径*节点权值 之和,根节点1的权值没有用的
【code】:
 /**
Judge Status:Accepted Memory:2880K
Time:610MS Language:G++
Code Length:2062B Author:cj
*/ #include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define N 50005
#define INF 1000000000000
using namespace std; struct Edge //保存边的结构体
{
int to; //边连接的另外个点
int next; //下一个搜索的节点
int w; //节点的权值
}edge[N<<]; struct Nod
{
int u; //进入队列中的点
__int64 dis; //到该点的距离
}now,temp; bool operator< (Nod a,Nod b) //优先队列重载'<'运算符
{
return a.dis>b.dis; //小到大
} int weight[N],head[N],visit[N];
__int64 dis[N]; //第一点到各点的最小距离 void init(int n) //初始化
{
int i;
for(i=;i<=n;i++)
{
visit[i] = ;
dis[i] = INF;
head[i] = -;
}
} void Dijkstra(int s)
{
int i,v;
dis[s] = ;
priority_queue<Nod> p_q; //优先队列 你懂的
temp.dis = ;
temp.u = s;
p_q.push(temp);
while(!p_q.empty())
{
temp = p_q.top(); //每次去的都是距离起点最小的点(优先队列的性质)
p_q.pop();
if(visit[temp.u]) continue;
visit[temp.u] = ;
for(i=head[temp.u];i!=-;i=edge[i].next) //每次遍历跟这个点有连接的所有点
{
v = edge[i].to;
if(!visit[v]&&dis[v]>dis[temp.u]+edge[i].w)
{
dis[v] = dis[temp.u]+edge[i].w; //距离更新
now.u = v;
now.dis = dis[v];
p_q.push(now); //压入队列
}
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m,n;
scanf("%d%d",&n,&m);
int i;
for(i=;i<n;i++) scanf("%d",weight+i);
int id = ;
init(n);
int a,b,c;
for(i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
a--,b--;
edge[id].to = b;
edge[id].w = c;
edge[id].next = head[a];
head[a] = id++; //将边 a --> b保存 edge[id].to = a;
edge[id].w = c;
edge[id].next = head[b];
head[b] = id++; //将边 b --> a保存
}
Dijkstra();
__int64 res = ;
for(i=;i<n;i++)
{
if(dis[i]==INF) break;
res += dis[i]*weight[i];
}
if(i<n) puts("No Answer");
else printf("%I64d\n",res);
}
return ;
}

poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra的更多相关文章

  1. POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)

    POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意:  圣诞树是由n个节点和e个边构成的,点编号1-n. ...

  2. poj 3013 Big Christmas Tree

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20974   Accepted: 4 ...

  3. poj 3013 Big Christmas Tree Djistra

    Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...

  4. poj 3013 Big Christmas Tree (dij+优先级队列优化 求最短)

    模板 意甲冠军:给你一个图,1始终根,每一方都有单价值,每个点都有权重新. 每个边缘的价格值 = sum(后继结点重)*单价方值. 最低价格要求树值,它构成了一棵树n-1条边的最小价值. 算法: 1. ...

  5. SPFA/Dijkstra POJ 3013 Big Christmas Tree

    题目传送门 题意:找一棵树使得造价最少,造价为每个点的子节点造价和*边的造价和 分析:最短路跑出1根节点到每个点的最短边权值,然后每个点的权值*最短边距和就是答案,注意INF开足够大,n<=1特 ...

  6. POJ Big Christmas Tree(最短的基础)

    Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the ...

  7. POJ3013 Big Christmas Tree[转换 最短路]

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23387   Accepted: 5 ...

  8. POJ 3013 SPFA算法,邻接表的使用

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 19029   Accepted: 4 ...

  9. Big Christmas Tree(poj-3013)最短路

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 25823   Accepted: 5 ...

随机推荐

  1. win7下的mstsc ubuntu下的rdesktop

    远程图形化登录, win7下: 开始->mstsc->10.108.103.93即可进行后续输入账号密码验证登录. 功能类似rdesktop. 如图:

  2. php对UTF8字体串进行单字分割返回数组

    在网上查了很多字符串分割方法,都无法正确对UTF8字符串进行正确分割返回单个字符的数组.经过对FTU8编码的分析写出了下面的方法对UTF8进行分割.本人测试可用.本方法只支持UTF8编码的,其它编码转 ...

  3. c++ 11 国标标准方面的异常处理与微软在Visual Studio 2012的异常处理的区别

    这段代码: __try { } __except(GetErrorCode()) { } 可以捕获空指针,但是包围在其中的代码不能有自带析构函数的对象.c++ 11 标准里面的auto_ptr关键字, ...

  4. Eclipse 调试maven test

    在eclipse中调试maven test 一般情况下,使用如下方式都不能使myeclipse检测到程序中的断点: 项目 -> Run As -> maven test 或 项目 -> ...

  5. OC3-父类指针指向子类对象

    // // Cat.h // OC3-父类指针指向子类对象 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng. ...

  6. java新手笔记31 集合实现类

    Person类: package com.yfs.javase; import java.util.Date; public class Person implements Comparable { ...

  7. 让 Putty 保存密码,自动登陆的四种方法

    Putty 基本是我在紧急时候用来登陆 Linux/Unix 终端的不二之先,因其小,开源,界面也非常实用.可是当你要在私有的机器上,经常性的要登陆很多机器的时候就觉得烦琐了,不光打开一堆的窗口,还要 ...

  8. PERL 脚本

    PERL: Practical Extraction and Report Language 参考文档 1.Perl 5 version 24.0 documentation

  9. 开启Windows的索引服务

    除开SearchEverything电脑内部的全硬盘搜索之外,如果要搜索文件内的内容的话,就无能为力的了.Window内置的索引服务就派上用场了,这么好的服务,默认设置居然是关闭的,想不通. 下面来介 ...

  10. makefile--编码修改-空格出现错误

    "makefile", line 40: make: 1254-055 Dependency line needs colon or double colon operator. ...