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

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 这题其实就是一个裸的最短路,根据题意,
边权乘以字数点权和最小,
自己转化一下思维就是就是一个点到1点所经历的最短路乘以当前点的点权
这题有一个超级坑点,当n==1 || n==0 的时候要特判一下
还有这个特判不能直接放在输入n,m后面
要等到所有数据输入完才 特判 continue
我特判放错了位置 ,wa了一页
还有这题不能用vector建图 ,
会超时的,以后还是要学会打邻接表,快一定没错。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector> using namespace std; const long long INF = 0x3ffffffffffff;
const int maxn = 5e4+;
struct node {
int v;
int w;
int next;
}edge[*maxn];
int vis[maxn],head[maxn],w[maxn];
long long d[maxn];
void spfa(int n)
{
memset(vis,,sizeof(vis));
for (int i= ;i<=n ;i++) d[i]=INF;
d[]=;
vis[]=;
queue<int>q;
q.push();
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=;
for (int i=head[u] ;i!=- ;i=edge[i].next ) {
int v=edge[i].v;
int w=edge[i].w;
if (d[v]>d[u]+w){
d[v]=d[u]+w;
if (!vis[v]) {
q.push(v);
vis[v]=;
}
}
}
}
}
int main() {
int t;
scanf("%d",&t);
while(t--){
int n,m,e=;
scanf("%d%d",&n,&m);
for (int i= ;i<=n ;i++) {
scanf("%d",&w[i]);
head[i]=-;
}
for (int i= ;i<m ;i++ ){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
edge[e].v=b;
edge[e].w=c;
edge[e].next=head[a];
head[a]=e++;
edge[e].v=a;
edge[e].w=c;
edge[e].next=head[b];
head[b]=e++;
}
if (n== || n==) {
printf("0\n");
continue;
}
long long ans=;
int flag=;
spfa(n);
for (int i= ;i<=n ;i++ ){
if (d[i]==INF) {
flag=;
break;
}
else ans+=d[i]*w[i];
}
if (flag ) printf("No Answer\n");
else printf("%lld\n",ans);
}
return ;
}
 

Big Christmas Tree(poj-3013)最短路的更多相关文章

  1. poj 3013 最短路SPFA算法

    POJ_3013_最短路 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23630 ...

  2. poj 3013 最短路变形

    http://poj.org/problem?id=3013 给出n个点,m个边.给出每个点的权值,每个边的权值.在m条边中选n-1条边使这n个点成为一棵树,root=1,求这棵树的最小费用,费用=树 ...

  3. POJ 3013最短路变形....

    DES:计算输的最小费用.如果不能构成树.输出-1.每条边的费用=所有的子节点权值*这条边的权值.计算第二组样例可以知道树的费用是所有的节点的权值*到根节点的最短路径的长度. 用dij的邻接矩阵形式直 ...

  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 ...

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

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

  6. poj 3013 Big Christmas Tree

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

  7. poj 3013 Big Christmas Tree Djistra

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

  8. POJ3013 Big Christmas Tree[转换 最短路]

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

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

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

随机推荐

  1. 解决SoapFault (looks like we got no XML document)问题

    今天在调试项目的时候出现下面的错误信息: SoapFault looks like we got no XML document (D:\phpStudy\WWW\self.shop.xunmall. ...

  2. MySQL一些中重要命令

    前言: 最近在面试的过程中,深感对MySQL一些重要命令的缺失.借着这个机会,补补这块的知识.不让自己只会增删查改,懂一些高级的东西 limit 用法 order by 用法 in 和 between ...

  3. pygame事件之——控制物体(飞机)的移动

    近来想用pygame做做游戏,在 xishui 大神的目光博客中学了学这东西,就上一段自己写的飞机大战的代码,主要是对键盘控制飞机的移动做了相关的优化 # -*- coding: utf-8 -*- ...

  4. Mego开发文档 - 复杂保存操作

    复杂保存操作 Mego框架还提供了更强大的数据更新API,以简化开发工作,同时也保证的性能. 指定属性添加数据 本列中指定插入一个数据对象,并且只会插入三列数据,最后两个属性是以表达式的形式插入. u ...

  5. nohup 与 & 的区别

    nohup -- invoke a utility immune to hangups : 运行命令忽略挂起信号 & 是指后台运行: nohup 的功能和& 之间的功能并不相同.其中, ...

  6. centos单机安装zookeeper+kafaka

    环境如下: CentOS-7-x86_64zookeeper-3.4.11kafka_2.12-1.1.0 一.zookeeper下载与安装1)下载zookeeper [root@localhost ...

  7. POJ-1751 Highways---确定部分边的MST

    题目链接: https://vjudge.net/problem/POJ-1751 题目大意: 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多 ...

  8. 编程基础学习JS的入门教程

    将JavaScript 插入网页的方法 使用<script>标签在网页中插入Javascript代码. 插入JavaScript 与在网页中插入CSS的方式相似.使用下面的代码可以在网页中 ...

  9. ASP.NET CORE系列【五】webapi整理以及RESTful风格化

    介绍 什么是RESTful?  这里不多做赘述,详情请百度! 哈哈,本来还想巴拉巴拉介绍一些webapi, RESTful的, 还是算了,咱们直接上干货!(原因是懒!哈哈) 使用 以前使用过mvc的人 ...

  10. HTML5新增的标签及使用

    HTML5和HTML其实是很相似的,但是有些内容有发生了改变,今天我学习了一下HTML5发现还是挺好学的,只要有html+css基础就可以,今天知识看了下新的标签. 一.定义文档类型 在文件的开头总是 ...