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. 安装 Docker Machine - 每天5分钟玩转 Docker 容器技术(45)

    前面我们的实验环境中只有一个 docker host,所有的容器都是运行在这一个 host 上的.但在真正的环境中会有多个 host,容器在这些 host 中启动.运行.停止和销毁,相关容器会通过网络 ...

  2. python函数(1):初始函数

    在学了前面很多python的基础类型后,我们终于可以进入下一阶段,今天我们将走进一个函数的新世界. 预习: 1.写函数,计算传入字符串中[数字].[字母].[空格] 以及 [其他]的个数 2.写函数, ...

  3. (转)Sublime Text2 快捷键汇总

    场景:最近在编写项目中越发的感觉到一个得心应手的编辑器是多么的重要,而sublime,无疑是让我用着最舒服,最有感觉的编辑器了! 1 快捷键总结 一个好的编辑器,能大大提高编程的效率.如果能熟知软件的 ...

  4. 浅谈lvs和nginx的一些优点和缺点

    借鉴一些网上资料整理了简单的比较: LVS的负载能力强,因为其工作方式逻辑非常简单,仅进行请求分发,而且工作在网络的第4层,没有流量,所以其效率不需要有过多的忧虑. LVS基本能支持所有应用,因为工作 ...

  5. QT QT creator QTsdk的区别

    Qt是一个跨平台的C++图形用户界面应用程序框架.它提供给应用程序开发者建立艺术级的图形用户界面所需的所用功能.Qt是完全面向对象的,很容易扩展,并且允许真正地组件编程. QT Creator 跨平台 ...

  6. .NET Core 成都线下面基会拉开序幕

    2017年07月29日下午,由 .NET China Foundation 成都小组组织的 .NET Core 成都地区线下技术交流会在成都成华区某茶楼成功举行,这也是成都地区 .NET Core 非 ...

  7. android rss阅读器开发一点小技巧

    这几天一直在学习开发Rss阅读器,遇到一个很坑的问题,InputSource这里总是出错.弄了好久,终于让我找到一个解决方法----看代码: new Thread(){ @Override publi ...

  8. 快学 Scala 入门 3 部曲

    1 基础 1.1 Scala 解释器 REPL - 交互式解释器环境 R(read).E(evaluate).P(print).L(loop) 输入值,交互式解释器会读取输入内容并对它求值,再返回结果 ...

  9. CI 结合 vue.js 的搜索功能模块

    CI 结合 vue.js 的搜索功能模块 最近在有优化公司后台的某个模块的搜索功能优化 原先的是这个样子的,很是单调: 老大给我找个图希望我能弄成这样子: 经过不断修改,最后成了这样子 是不是比以前好 ...

  10. Python初学基础

      初入坑Python,打算跟着沫凡小哥的学习视频打个基础,此篇文章做一些简单的学习记录,加油加油加油啦 沫凡小哥的学习网站:https://morvanzhou.github.io/tutorial ...