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

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)

点有w,路径有也有权值,price[i]定义为i和所有后代sigma{w}*i与父的路径权值乘积;构造一棵生成树使代价最小

最小生成树?没法建图,不知道后代有谁
把式子转换一下,不就是sigma{w[i]*d[i]}...............
dijk 94ms
 
//
// main.cpp
// poj3013
//
// Created by Candy on 9/12/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=,M=;
const ll INF=1e19;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int T,n,m,w[N],a,b,c;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int a,int b,int c){
cnt++;
e[cnt].v=b;e[cnt].w=c;e[cnt].ne=h[a];h[a]=cnt;
cnt++;
e[cnt].v=a;e[cnt].w=c;e[cnt].ne=h[b];h[b]=cnt;
} struct hn{
int u;
ll d;
bool operator <(const hn &rhs)const{return d>rhs.d;}
};
ll d[N];
priority_queue<hn> q;
bool vis[N];
void dijkstra(int s){
for(int i=;i<=n;i++) d[i]=INF;
memset(vis,,sizeof(vis));
q.push((hn){s,});d[s]=;
while(!q.empty()){
hn x=q.top();q.pop();
int u=x.u;
if(vis[u]) continue; vis[u]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
q.push((hn){v,d[v]});
}
}
}
}
int main(int argc, const char * argv[]) {
T=read();
while(T--){ memset(h,,sizeof(h));cnt=;
n=read();m=read();
for(int i=;i<=n;i++) w[i]=read();
for(int i=;i<=m;i++){
a=read();b=read();c=read();
ins(a,b,c);
}
dijkstra();
ll ans=,flag=;
for(int i=;i<=n;i++){
if(d[i]==INF) {printf("No Answer\n");flag=;break;}
ans+=w[i]*d[i];
}
if(!flag) printf("%lld\n",ans); }
return ;
}

POJ3013 Big Christmas Tree[转换 最短路]的更多相关文章

  1. POJ3013 Big Christmas Tree(最短路径树)

    题目大概说给一张点和边都有权的图,现在要求其一棵以1结点为根的生成树使树的边权和最小,树边权 = 对应的图边权 * 树边末端点为根的子树所有结点对于图顶点的点权和. 要求∑(边权*子树点权和),等价于 ...

  2. POJ3013 Big Christmas Tree

    题目:http://poj.org/problem?id=3013 求每个点到1的最短路.不是最小生成树. 总是WA.看讨论里说INF至少2e10,于是真的A了! 算一下,dis最大可能3276800 ...

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

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

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

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

  5. poj 3013 Big Christmas Tree

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

  6. poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra

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

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

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

  8. poj 3013 Big Christmas Tree Djistra

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

  9. Convert Sorted Array to Binary Search Tree转换成平衡二查搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 二分 ...

随机推荐

  1. sql server 2014预览版发布

    MSDN发布sql server2014预览版,如下图: SQL Server 2014新特性: 微软SQL Server部门主管Eron Kelly介绍,通过将交易处理放到内存中进行,新的SQL S ...

  2. [Java] Tomcat环境变量设置

    @echo off title Tomcat环境变量设置 color 0a set /p inputTH=D:\Work\024_Tomcat if /i "%inputTH%"= ...

  3. Hosting socket.io WebSocket apps in IIS using iisnode

    In this post I explain how to configure a socket.io node.js application to use of WebSockets when ho ...

  4. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q13-Q15)

    Question 13 You are designing a SharePoint 2010 site. You need to design the site to meet all the fo ...

  5. Android 杀死进程

    当应用不再使用时,通常需要关闭应用,可以使用以下三种方法关闭android应用: 第一种方法:首先获取当前进程的id,然后杀死该进程.android.os.Process.killProcess(an ...

  6. kubernetes听云实战发布版

    ➠更多技术干货请戳:听云博客 听云线上使用k8s已经有一段时间了,下面对一些听云使用过程中的问题进行一些梳理,包括架构设计,安装部署和后期维护. 目录结构如下: 下面进入正题: 我们以听云系统的一个报 ...

  7. django tmeplate 循环基数

    {% for item in item_list %} {{ forloop.counter }} {# starting index 1 #} {{ forloop.counter0 }} {# s ...

  8. Windows下 maven3.0.4的安装步骤+maven配置本地仓库

    简单讲下maven的安装步骤: 1.在安装maven之前,先确保已经安装JDK1.6及以上版本,并且配置好环境变量. 2.下载maven3,最新版本是Maven3.0.4 ,下载地址:http://m ...

  9. #VSTS日志# 2015/12/10 – 终于可以删除工作项了

    最近的更新不少,废话少说,直接上干货 定制工作项字段 本周的更新后,所有的用户都可以在vsts上直接给工作项添加字段了,具体内容包括– 添加新字段(日期,字符串,整形,数字)– 字段显示位置配置– 过 ...

  10. Java基础知识学习(九)

    GUI开发 先前用Java编写GUI程序,是使用抽象窗口工具包AWT(Abstract Window Toolkit).现在多用Swing.Swing可以看作是AWT的改良版,而不是代替AWT,是对A ...