poj 3013 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 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
Source
/*
* 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的更多相关文章
- POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)
POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n. ...
- poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra
http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total S ...
- poj 3013 Big Christmas Tree Djistra
Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...
- poj 3013 Big Christmas Tree (dij+优先级队列优化 求最短)
模板 意甲冠军:给你一个图,1始终根,每一方都有单价值,每个点都有权重新. 每个边缘的价格值 = sum(后继结点重)*单价方值. 最低价格要求树值,它构成了一棵树n-1条边的最小价值. 算法: 1. ...
- SPFA/Dijkstra POJ 3013 Big Christmas Tree
题目传送门 题意:找一棵树使得造价最少,造价为每个点的子节点造价和*边的造价和 分析:最短路跑出1根节点到每个点的最短边权值,然后每个点的权值*最短边距和就是答案,注意INF开足够大,n<=1特 ...
- POJ Big Christmas Tree(最短的基础)
Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the ...
- POJ3013 Big Christmas Tree[转换 最短路]
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23387 Accepted: 5 ...
- Big Christmas Tree(poj-3013)最短路
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 25823 Accepted: 5 ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
随机推荐
- Vue.js之深入浅出
介绍引言 Vue.js(读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,它不仅易于上 ...
- 了解Java并学会创建Java项目(一个菜鸟的成长历程)
计算机语言分类:了解 1)低级语言:更接近于计算机的语言 1.1)机器语言:由0和1组成的 1.2)汇编语言:有一些助记符号2)高级语言:更接近于人的语言 2.1)面向过程的:C... 2.2)面向对 ...
- HDOJ2008-数值统计
Problem Description 统计给定的n个数中,负数.零和正数的个数. Input 输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然 ...
- javascript中this的用法
this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ this.x = 1; } 随着函数使用场合的 ...
- bzoj3728: PA2014Final Zarowki
Description 有n个房间和n盏灯,你需要在每个房间里放入一盏灯.每盏灯都有一定功率,每间房间都需要不少于一定功率的灯泡才可以完全照亮. 你可以去附近的商店换新灯泡,商店里所有正整数功率的 ...
- 使用CHCA搭建静态博客
[toc] chca是一个使用golang开发的静态博客生成器,简单.方便.快捷,抛弃每次都需要使用命令编译文件,采用文件监听方式编译,作者只需把markdown文件放到配置中的markdown文件夹 ...
- ubuntu 手动安装openssh-server
先用能上网的机器下载:zlib-1.2.5.tar.openssh-5.6p1.tar.gz.openssl-0.9.8o.tar.tar,接下来,准备安装. 步骤如下: 1.首先解压安装zlib:t ...
- acdsee-pro-8 英文版
目前中文版acdsee pro 8不支持破解,只能选择英文版,然后汉化. 但是英文版并不好找,我在这里分享一下. 下载地址:http://pan.baidu.com/s/1boNZxQf 同时附上注册 ...
- NYOJ--541--最强DE 战斗力(递推)
最强DE 战斗力 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 春秋战国时期,赵国地大物博,资源非常丰富,人民安居乐业.但许多国家对它虎视眈眈,准备联合起来对赵国发 ...
- Mysql 基于日志点的主从复制(实操)
实现环境: Master 主:192.168.100.165 (Mysql 5.6.36) Slave 从 :192.168.100.156 (Mysql 5.6.36) 步骤1.在主DB服务器上建立 ...