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. MFC之动态调用自己写的类库中的类的成员方法

    第一步:创建一个要调用的类库 如果是MFC程序使用,可以创建一个MFC的类库,不过依然可以创建一个win32类库.我所知道的,MFC的类库可以分为常规MFC DLL和MFC扩展DLL关于它们之间的区别 ...

  2. 面向对象(OO)第二阶段学习总结

    0.前言 此阶段总共进行三次大作业,其中第一次作业中的第一题,水文数据校验及处理中,遇到较大的难题,第一次接触正则表达式,编码过程中显得难度特别大.第二次作业同样也是对于一元多项式求导中对单项的正则校 ...

  3. 【集群实战】NFS网络文件共享服务3-相关知识补充(showmount,exports,rpc)

    1. showmount命令说明 showmount命令一般用于从NFS客户端检查NFS服务器端共享目录的情况. 参数说明: -e,--exports 显示NFS服务器输出的目录列表 [root@we ...

  4. unix域源码解析

    首先我们先要创建一个用于通信的结构unix_proto_data ,并初始化某些字段 static int unix_proto_create(struct socket *sock, int pro ...

  5. Linux / mac OS Shell常用命令

    一.文件.目录操作命令 1.ls命令 功能:显示文件和目录的信息 ls 以默认方式显示当前目录文件列表 ls -a 显示所有文件包括隐藏文件 ls -l 显示文件属性,包括大小,日期,符号连接,是否可 ...

  6. JS的String()、toString()、valueOf()的一些隐秘特性

    toString()方法 要把一个值转换为一个字符串,最常用的就是,使用几乎每个值都有的toString()方法,这个方法唯一要做的就是返回相应值的字符串表现. 数值.布尔值.对象和字符串值(没错,每 ...

  7. 初学dp心得

    从STL到贪心,再到现在的动态规划,可以说动态规划真的让我学的有点蒙,对于一些题目,会做,但是不会用DP,现在还不能熟练的写出状态转移方程,更重要的是,自己宛如一个哺乳期的小孩,做题需要套模板,没有模 ...

  8. poj1251 Jungle Roads Kruskal算法+并查集

    时限: 1000MS   内存限制: 10000K 提交总数: 37001   接受: 17398 描述 热带岛屿拉格里山的首长有个问题.几年前,大量的外援花在了村庄之间的额外道路上.但是丛林不断地超 ...

  9. 《Docker从入门到跑路》之Dockerfile基本操作

    一.简介 Dockerfile是一个文本文件,里面包含一条条指令,每一条指令就是一层镜像.一般情况下,Dockerfile分为4个部分: 基础镜像 维护者信息 镜像操作指令 容器启动时执行命令 例如: ...

  10. Java——多线程之对象及变量的并发访问

    Java多线系列文章是Java多线程的详解介绍,对多线程还不熟悉的同学可以先去看一下我的这篇博客Java基础系列3:多线程超详细总结,这篇博客从宏观层面介绍了多线程的整体概况,接下来的几篇文章是对多线 ...