POJ3013 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 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
点有w,路径有也有权值,price[i]定义为i和所有后代sigma{w}*i与父的路径权值乘积;构造一棵生成树使代价最小
//
// 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[转换 最短路]的更多相关文章
- POJ3013 Big Christmas Tree(最短路径树)
题目大概说给一张点和边都有权的图,现在要求其一棵以1结点为根的生成树使树的边权和最小,树边权 = 对应的图边权 * 树边末端点为根的子树所有结点对于图顶点的点权和. 要求∑(边权*子树点权和),等价于 ...
- POJ3013 Big Christmas Tree
题目:http://poj.org/problem?id=3013 求每个点到1的最短路.不是最小生成树. 总是WA.看讨论里说INF至少2e10,于是真的A了! 算一下,dis最大可能3276800 ...
- Big Christmas Tree(poj-3013)最短路
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 25823 Accepted: 5 ...
- POJ Big Christmas Tree(最短的基础)
Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the ...
- poj 3013 Big Christmas Tree
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20974 Accepted: 4 ...
- 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(最短Dijkstra+优先级队列优化,SPFA)
POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n. ...
- poj 3013 Big Christmas Tree Djistra
Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...
- Convert Sorted Array to Binary Search Tree转换成平衡二查搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 二分 ...
随机推荐
- #8.11.16总结#CSS常用样式总结(二)
border 边框 简写:border:1px solid #000; 等效于:border-width:1px;border-style:solid;border-color:#000; 顺序:b ...
- JavaScript 数据类型判断
JavaScript 的数据类型分为两类:原始类型(基本类型)和对象类型(引用类型).原始类型包括数字.字符串和布尔值,另外有两个特殊的原始值:null 和 undefined,除此之外的都是对象.对 ...
- SPC2014 :“FOSL”不是替代InfoPath,只是另外一种创建表单的方式
今天在SPC2014微软宣布他们技术路线图.其实,没有足够证据替代InfoPath,只是另外的一种尝试 - FOSL(对SharePoint列表表单). FOSL使用相同的引擎,用于创建表单的访问服务 ...
- 解决Dialog 消失,输入法不消失的问题
前言:今天遇到一个奇怪的问题,Activity 里面弹出一个 dialog , 这个dialog里面有EditText . 问题:当 dialog 里面的输入法出现的时候,此时让diolog 消失,输 ...
- 三种经典iPhone上网络抓包方法详解
此文章来自:听云博客 很多时候需要网络抓包分析,在iPhone上抓包稍有不同,下面介绍三种常用的方式.分析工具以wireshark为例. 一.最简单的方式:用PC作为热点,在PC上抓包 优点:简单 缺 ...
- web service上传参数代码实例
web service上传参数代码实例 这次做的项目用到webservice比较多,最开始在网上看的参考dome,发现都不行,后来发现安卓4.0以后有很大的不同,在做传参时,有些东西需要注意: 第一, ...
- IOS中十六进制的颜色转换为UIColor
IOS中十六进制的颜色转换为UIColor #pragma mark - 颜色转换 IOS中十六进制的颜色转换为UIColor + (UIColor *) colorWithHexString: (N ...
- 网站错误记录:Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.
今天看公司项目的错误日志文件,发现日志文件都是记录的这个错误. 经过网站查找,发现英文翻译是: 译:超时,与连接池的连接时间已过.这种情况发生是因为连接池在使用和最大连接池数目已满 通过翻译,可以看出 ...
- wpf开发桌面软件记录
我的开发环境是win7,vs2013,sql2012,用wpf开发了一个很简单的桌面软件,用Installshield制作的安装包,安装包包含了.framework4.5,在自己电脑上测试正常,想着挺 ...
- WPF学习之路(二) XAML
在WPF中引入了XAML语言,主要用于界面设计,业务逻辑则使用C#实现后台代码,将界面设计与业务逻辑分离 XAML是一种声明式语言,类似XML\HTML 示例: <!--Start Tag--& ...