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 ...
随机推荐
- c# 多线程 创建对象实例
本次的标题是我在写单例模式的博客时遇到的问题,所以今天专门写了的demo让自己记住怎么简单的使用多线程. 一直纠结的是怎么在for循环中多次实例化对象,好复现单例模式在没有加锁的情况下出现多个实例对象 ...
- Windows查看端口使用状况
使用端口是我们在进行远程或者打印机等都会遇到的,但是有很多用户会遇到端口被占用的情况,遇到这样的问题首先就要找出电脑中的所以端口然后进行查看,还是有很多人不知道该如何查看电脑端口. 1 查看windo ...
- (转)搬瓦工(bandwagonhost)后台管理VPS
1. Bandwagonghost使用建议 购买了搬瓦工(bandwagonhost)的VPS,如何使用呢? 首先插几句使用建议,老高认为十分重要,为什么呢?搬瓦工如果监控到有大量的垃圾信息从我们的主 ...
- JPA Advanced Mappings(映射)
JPA Advanced Mappings(映射) JPA是一个使用java规范发布的库.因此,它支持所有面向对象的实体持久性概念. 原文链接:http://blogxinxiucan.sh1.new ...
- centos7下安装PHP swoole扩展
PHP的异步.并行.高性能网络通信引擎,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据库连接池,AsyncTask,消息队列, ...
- ES6中的模块
前面的话 JS用"共享一切"的方法加载代码,这是该语言中最容出错且容易令人感到困惑的地方.其他语言使用诸如包这样的概念来定义代码作用域,但在ES6以前,在应用程序的每一个JS中定义 ...
- 用Eclipse生成JPA元模型
在JPA criteria 动态查询中,有个"元模型"的东西,它是根据"实体"类动态生成的一个类,它的主要作用是实现JPA criteria查询的"类 ...
- Java 字符排序问题
Java 字符排序问题 未专注于排序算法,而是写了一个MyString类,实现了comparable的接口,然后用Arrays的sort方法来实现排序.我觉得这道题的难度在于如果比较两个.因为大小写的 ...
- javascript基础知识3#引用类
引用类 引用类型的只是引用类型的一个实例,在ecmascript当中,引用类型是一种数据结构用于将数据和功能组织在一起,也常被称做类. object类型 构造函数[var o = new object ...
- C# 将数据表导出到Excel通用方法
DataGrid dg = new DataGrid(); dg.DataSource = dt; dg.DataBind(); Response.Clear(); Response.Buffer = ...