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. 二分 ...
随机推荐
- Web前端开发工具总结
前端开发工具: web前端开发乃及其它的相关开发, 推荐sublime text, webstorm(jetbrains公司系列产品)这两个的原因在于,有个技术叫emmet, http://docs. ...
- Autodesk 招人了,开发顾问,感兴趣的或者有推荐的人扔简历过来啊
Autodesk ADN招人了,在上海,开发顾问, JD如下. 如果你感兴趣或者有人推荐,扔简历过来啊, daniel.du@autodesk.com,赶早不赶晚啊. Job Title: Dev ...
- Spring Session
开工开工, 准备条件: 1. 本地Redis,官网:http://redis.io/,windows下 https://github.com/ServiceStack/redis-windows ht ...
- [转]MOSS通过此命令注册模板,web应用程序可以根据stp模块生成网站集
注:C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin stsadm –o add ...
- 详细对比IB开发与纯手码开发的优劣。
1.IB是什么? Interface Builder 是一种通过图形化界面搭建UI的方式,并把窗口.菜单栏以及窗口上的各种控件的对象都“冻结”在了一个 NIB文档里:程序运行时,这些对象将会“苏醒”. ...
- Android 手机卫士--事件传递&响应规则
问题的提出: 本文地址:http://www.cnblogs.com/wuyudong/p/5911187.html ,转载请注明源地址. 前面的文章实现了点击SettingItemView条目的时候 ...
- Android 图片的平移和镜面和倒影效果
在前面的文章中陆续介绍了图片的旋转与缩放,本文继续介绍关于图片的操作 图片的平移 使用下面的代码将图水平竖直方向平移10个像素 matrix.setTranslate(10, 10); 可以看到图片不 ...
- 【代码笔记】iOS-提醒时间的选择
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- IOS 音效
IOS 音效 音效我们也可以成为短音频通常在程序中播放时间为1~2秒. 在应用程序中起到点缀效果,提升整体用户体验 音效文件只需要加载一次 示例代码: // // ViewController.m / ...
- C# 依赖缓存
使用轮询的方式 数据库: 在VS的命令里面输入 aspnet_regsql.exe -S (local) -U sa -P 123456 -d ERP_SQL -ed 上面这句是用来设置哪个服务器上的 ...