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 //INF开小会WA 开成0x3f3f3f3f 会WA
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=;
const int M=;
int n,m,head[N],tot;
const long long INF=;
unsigned long long val[N],d[N];
bool vis[N];
struct node{
    int next,v,w;
}e[M];
void add(int u,int v,int w){
    e[tot].v=v;
    e[tot].next=head[u];
    e[tot].w=w;
    head[u]=tot++;
}
void spfa(){
    for(int i=;i<=n;++i) d[i]=INF,vis[i]=;
    queue<int>Q;
    Q.push();
    d[]=,vis[]=;
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        vis[u]=;
        for(int i=head[u];i+;i=e[i].next){
            int v=e[i].v;
            if(d[v]>d[u]+e[i].w){
                d[v]=d[u]+e[i].w;
                if(!vis[v]) {
                    Q.push(v);vis[v]=;
                }
            }
        }
    }
    for(int i=;i<=n;++i) if(d[i]==INF) {puts("No Answer");return;}
    unsigned long long ans=;
    for(int i=;i<=n;++i)
        ans+=(long long)d[i]*val[i];
    printf("%llu\n",ans);
}
int main(){
    int T,u,v,x;
    for(scanf("%d",&T);T--;){
        scanf("%d%d",&n,&m);
        for(int i=;i<=n;++i) head[i]=-;
        tot=;
        for(int i=;i<=n;++i) scanf("%llu",&val[i]);
        while(m--){
        scanf("%d%d%d",&u,&v,&x);
        add(u,v,x);
        add(v,u,x);
        }
        if(n==||n==) {puts("");continue;}
        spfa();
    }
}

Poj 3013基础最短路的更多相关文章

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

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

  2. POJ 1161 Walls(最短路+枚举)

    POJ 1161 Walls(最短路+枚举) 题目背景 题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了 ...

  3. poj 3013 最短路变形

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

  4. poj 3013 最短路SPFA算法

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

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

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

  6. poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra

    http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total S ...

  7. [原]poj-2680-Choose the best route-dijkstra(基础最短路)

    题目大意: 已知n 个点,m条路线,s为终点:给出m条路线及其权值:给出w个起点,求最短路! 思路:基础的dijkstra,有向无环正权最短路,只要把终点和起点 reverse考虑便可. AC代码如下 ...

  8. poj - 3225 Roadblocks(次短路)

    http://poj.org/problem?id=3255 bessie 有时会去拜访她的朋友,但是她不想走最快回家的那条路,而是想走一条比最短的路长的次短路. 城镇由R条双向路组成,有N个路口.标 ...

  9. poj 3463 Sightseeing( 最短路与次短路)

    http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

随机推荐

  1. docker容器介绍

                       Docker容器 一.什么是Docker? Docker时Docker.Lnc公司开源的一个基于LXC技术之上搭建的Container容器引擎,源代码托管在Git ...

  2. 写给Java程序员的Java虚拟机学习指南

    大家好,我是极客时间<深入拆解Java虚拟机>作者.Oracle Labs高级研究员郑雨迪.有幸借这个专题的机会,能和大家分享为何Java工程师要学Java虚拟机?如何掌握Java虚拟机? ...

  3. 谷歌浏览器报错Unchecked runtime.lastError: The message port closed before a response was received.。

    浏览器版本 : 报错原因:扩展程序问题 解决建议:打开chrome://extensions/,逐一关闭排查

  4. android 动画学习总结

    本文内容是本人阅读诸多前辈的学习心得后整理的,若有雷同,请见谅 Android 动画 分类:帧动画,补间动画,属性动画  . 1.帧动画 将一张张单独的图片连贯的进行播放,从而在视觉上产生一种动画的效 ...

  5. Guzzle 一个PHP的HTTP客户端

    Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上. https://github.com/guzzle/guzzle 接口简单:构建查询语句.POST请求. ...

  6. Vue.js 条件渲染 v-if、v-show、v-else

    v-if  v-if 完全根据表达式的值在DOM中生成或移除一个元素.如果v-if表达式赋值为false,那么对应的元素就会从DOM中移除:否则,对应元素的一个克隆将被重新插入DOM中. 1 2 3 ...

  7. 在Jetson TX2上安装OpenCV(3.4.0)

    参考文章:How to Install OpenCV (3.4.0) on Jetson TX2 与参考文章大部分都是相似的,如果不习惯看英文,可以看看我下面的描述 在我们使用python3进行编程时 ...

  8. andorid jar/库源码解析之Butterknife

    目录:andorid jar/库源码解析 Butterknife: 作用: 用于初始化界面控件,控件方法,通过注释进行绑定控件和控件方法 栗子: public class MainActivity e ...

  9. 金钱货币用什么类型--(Java)

    0.前言 项目中,基本上都会涉及到金钱:那么金钱用什么数据类型存储呢? 不少新人都会认为用double,因为它是双精度类型啊,或者float, 其实,float和double都是不能用来表示精确的类型 ...

  10. win7 64位系统使用vs2010编译OSG3.2.1

    首先我想说的是,osg是有二进制安装包的:http://openscenegraph.alphapixel.com/osg/downloads/free-openscenegraph-binary-d ...