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

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)
 
题意&思路:无聊的题目,纯最短路。dijsktra+heap优化。存下来当模板
 /*
* Author: Joshua
* Created Time: 2014年10月06日 星期一 20时39分47秒
* File Name: poj3013.cpp
*/
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 50005
#define LLinf 0x7f7f7f7f7f7f7f7f
typedef long long LL;
struct edge
{
int v,w,next;
} e[maxn<<];
struct node
{
LL d;
int num;
bool operator < (const node& pp) const
{
return d<pp.d;
}
} heap[maxn];
int T,n,m,tot,heapSize;
int c[maxn],head[maxn],map[maxn];
bool vis[maxn];
void Read(int &ret){
ret = ;
bool ok = ;
for( ; ;){
int c = getchar();
if (c >= '' && c <= '') ret = (ret << ) + (ret << ) + c - '', ok = ;
else if (ok) return;
}
} void createEdge(int u,int v,int w)
{
edge& temp=e[++tot];
temp.v=v;
temp.w=w;
temp.next=head[u];
head[u]=tot;
} void heap_swap(int x,int y)
{
swap(heap[x],heap[y]);
map[heap[x].num]=x;
map[heap[y].num]=y;
} void heap_up(int x)
{
while (x!= && heap[x]<heap[x>>])
{
heap_swap(x,x>>);
x>>=;
}
} void heap_down(int x)
{
while ((x<<)<=heapSize)
{
x<<=;
if (x+<=heapSize && heap[x+]<heap[x]) x++;
if (heap[x]<heap[x>>])
heap_swap(x,x>>);
else break;
}
} void heap_del()
{
heap_swap(,heapSize);
heapSize--;
heap_down();
} void init()
{
int u,v,w;
Read(n);Read(m);
for (int i=;i<=n;++i)
Read(c[i]);
memset(head,-,(n+)<<);
tot=;
for (int i=;i<=m;++i)
{
Read(u);Read(v);Read(w);
createEdge(u,v,w);
createEdge(v,u,w);
}
} void updata(int x,LL y)
{
if (heap[x].d<=y) return;
heap[x].d=y;
heap_up(x);
} void solve()
{
bool flag=false;
LL td,ans=;
int tn;
for (int i=;i<=n;++i)
{
heap[i-].d=LLinf;
heap[i-].num=i;
map[i]=i-;
}
heap[n].d=;
heap[n].num=;
map[]=n;
heapSize=n;
heap_up(n);
memset(vis,,n+);
for (int i=;i<=n;++i)
{
td=heap[].d;
tn=heap[].num;
vis[tn]=false;
if (td==LLinf)
{
flag=true;
break;
}
heap_del();
ans+=td*c[tn];
for (int i=head[tn];~i;i=e[i].next)
if (vis[e[i].v])
updata(map[e[i].v],td+e[i].w);
}
if (flag) printf("No Answer\n");
else cout<<ans<<endl;
} int main()
{
Read(T);
for (int i=;i<=T;++i)
{
init();
solve();
}
return ;
}

poj 3013 Big Christmas Tree的更多相关文章

  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 (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra

    http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total S ...

  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. Big Christmas Tree(poj-3013)最短路

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

  9. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

随机推荐

  1. Struts2从头到脚--学习笔记(自认为比较重要的)

    一. Struts2框架介绍 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与 ...

  2. 安装cocoaPods第三方类库

    *1 检测gem 镜像文件     输入指令: gem sources -l     回车后得到镜像地址.可能是一个,也可能有好几个,常见两个如下 https://rubygems.org/     ...

  3. 【HTML】html5新属性-datalist

    摘要: 为实现输入域自动填充的效果 方法一: jquery-ui的autocomplete方法, 不是jquery 自带的方法. 方法二: 采用html5新特性,datalist,例子如下:坏处很明显 ...

  4. year:2017 month:7 day:19

    2017-07-19 JavaScript 一:javascirpt的对象 1:数组对象 声明方式:(1)var arr=new Array(): (2)var arr=new Array(12): ...

  5. 用SSH解决大局域网反向端口转发问题

    本文作者Tony Lee,转载自FreeBuf.COM ​​自从家里换了联通光纤后,联通就在我家宽带出口前搭了一个路由器,我家也彻底沦为192.168.1.0/24段的局域网了,带来的问题就是在外网无 ...

  6. python扫描proxy并获取可用代理ip

    今天咱写一个挺实用的工具,就是扫描并获取可用的proxy 首先呢,我先百度找了一个网站:http://www.xicidaili.com 作为例子 这个网站里公布了许多的国内外可用的代理的ip和端口 ...

  7. 各种排序算法及其java程序实现

    各种排序算法:冒择路(入)兮(稀)快归堆,桶式排序,基数排序 冒泡排序,选择排序,插入排序,稀尔排序,快速排序,归并排序,堆排序,桶式排序,基数排序 一.冒泡排序(BubbleSort)1. 基本思想 ...

  8. 编写一个矩形类,私有数据成员为矩形的长( len)和宽(wid),wid设置为0,有参构造函数设置和的值,另外,类还包括矩形的周长、求面积、取矩形的长度、取矩形的长度、取矩形的宽度、修改矩形的长度和宽度为对应的形参值等公用方法。

    class Rectangle { private double len, wid; public Rectangle()//求矩形周长 { len = 0; wid = 0; } public Re ...

  9. java配置mongo最大连接数

    最近做压测,其中有个交易涉及到对mongo的操作. 单机压到1500UV的时候出现如下错误: 一看,原来是mongo配置的最大连接数不够: 我们来看看mongo的官方文档: connectionPer ...

  10. CSS中2d转换:transition过渡放在:hover伪类中与应用在整个元素中区别

    css的2d转换十分强大,能够在不使用js的情况下,实现页面的元素与用户之间更多动态的交互,增强用户体验.其中使用最多的就是hover伪类. 1.创建一个页面的div元素: <!DOCTYPE ...