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();
    }
}

最新文章

  1. php加密类
  2. Android进程间通讯之messenger
  3. awk多模式匹配
  4. GridView多列排序
  5. Git基本操作(Windows下)
  6. Java编译原理
  7. 2 MD5加密 java实现
  8. 02.PHP7.x编译详解
  9. (转)java反射机制及简单工厂模式
  10. [ipsec][strongswan] 使用wireshark查看strongswan ipsec esp ikev1 ikev2的加密内容
  11. 简单谈谈$.merge()
  12. CentOS上安装 Docker-CE以及Docker 加速器配置
  13. js权威指南笔记
  14. centos 6.5环境利用iscsi搭建SAN网络存储服务及服务端target和客户端initiator配置详解
  15. DockOne技术分享(二十):Docker三剑客之Swarm介绍
  16. Kinect2.0点云数据获取
  17. 10条建议让你创建更好的jQuery插件(转载)
  18. 如何制作一款HTML5 RPG游戏引擎——第五篇,人物&amp;人物特效
  19. Angular.js进阶
  20. 部署和调优 2.3 tomcat中JDK安装

热门文章

  1. linux 之学习路线
  2. Shiro踩坑记(一):关于shiro-spring-boot-web-starter自动注解无法注入authorizer的问题
  3. Node.js中的express框架,修改内容后自动更新(免重启),express热更新
  4. 现代软件工程讲义 如何提出靠谱的项目建议 NABCD
  5. Hadoop学习笔记(二)——插件安装和使用(Hadoop Eclipse)
  6. Codeforces Round #590
  7. python(类多态)
  8. css的属性选择器
  9. Collection接口【集合】和Iterator迭代器类
  10. 真香!PySpark整合Apache Hudi实战