Big Christmas Tree
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 23630   Accepted: 5125

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结点为根的生成树使树的边权和最小,树边权 = 对应的图边权 * 树边末端点为根的子树所有结点对于图顶点的点权和。

思路:要求∑(边权*子树点权和),等价于求∑(点权*点到根路径上的边权和)。

第一道SPFA。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define N 50005
#define LL long long
#define INF (1LL<<60) struct Edge
{
int v,next;
LL w;
} edge[N]; int head[N]; int cnte=;
void addEdge(int a,int b,int w)
{
edge[cnte].v=b;
edge[cnte].w=w;
edge[cnte].next=head[a];
head[a]=cnte++;
} LL dist[N];
bool vis[N];
bool Spfa(int n)
{
for(int i=; i<=n; i++)
{
dist[i]=INF;
vis[i]=;
}
dist[]=;
vis[]=;
queue<int>q;
q.push();
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(dist[v]>dist[u]+edge[i].w)
{
dist[v]=dist[u]+edge[i].w;
if(vis[v]==)
{
vis[v]=;
q.push(v);
}
}
}
vis[u]=;
}
for(int i=; i<=n; i++)
{
if(dist[i]==INF)
return ;
}
return ;
} int verw[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cnte=;
int vn,en;
scanf("%d%d",&vn,&en);
for(int i=; i<=vn; i++)
scanf("%d",verw+i);
memset(head,-,sizeof(head));
for(int i=; i<en; i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
addEdge(a,b,w);
addEdge(b,a,w);
}
if(!Spfa(vn))
{
puts("No Answer");
continue;
}
else
{
LL res=;
for(int i=; i<=vn; i++)
res+=verw[i]*dist[i];
printf("%lld\n",res);
}
}
return ;
}
												

POJ_3013_最短路的更多相关文章

  1. poj 3013 最短路SPFA算法

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

  2. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  3. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  4. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  5. 最短路(Floyd)

    关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...

  6. bzoj1266最短路+最小割

    本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...

  7. HDU2433 BFS最短路

    Travel Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. 最短路(代码来源于kuangbin和百度)

    最短路 最短路有多种算法,常见的有一下几种:Dijstra.Floyd.Bellman-Ford,其中Dijstra和Bellman-Ford还有优化:Dijstra可以用优先队列(或者堆)优化,Be ...

  9. Javascript优化细节:短路表达式

    什么是短路表达式? 短路表达式:作为"&&"和"||"操作符的操作数表达式,这些表达式在进行求值时,只要最终的结果已经可以确定是真或假,求值过程 ...

随机推荐

  1. 1. PermCheck 桃花顺检验 Check whether array A is a permutation.

    package com.code; import java.util.Arrays; public class Test04_2 { public static int solution(int[] ...

  2. 性能测试实战-XYB项目-外网访问

    压测业务选择 跟产品.开发负责人评估系统中需要压测的重要业务接口 考虑到考勤业务是每天老师都需要做的且可多次考勤,列入压测重要业务中 值日检查也是每天老师都需要操作的业务,最终选择了考勤业务及值日检查 ...

  3. linux查找nginx所在目录

    ps -ef |grep nginx

  4. AUTOTRACE

    .sqlplus 设置参数 set atuotrace on SET AUTOTRACE OFF --No AUTOTRACE report is generated. This is the def ...

  5. 浅析C++多重继承

    继承是面向对象的三大特征之中的一个. 可是对于继承的实现和使用方式,各种不同的面向对象语言有各自的观点.有些语言支持多重继承.而有些语言则仅仅支持单一继承. 多重继承的确引入了较大的复杂度.那么.在不 ...

  6. 改善java程序的151个建议--数组和集合

    60.性能考虑,数组是首选,在基本类型处理方面.数组还是占优势的,并且集合类的底层也都是通过数组实现.建议在性能要求较高的场景中使用数组替代集合. 61.假设有必要.使用变长数组:我们能够通过对数组扩 ...

  7. 第二课 MongoDB 数据模型

    1.课程大纲 本课程主要介绍MongoDB数据模型相关知识.包含文档.集合与数据库的基本概念.用法及命名规则:MongoDB主要的数据类型介绍以及MongoDB Shell的简单介绍与使用. 文档 ( ...

  8. Android 驱动 (一) GPIO

    前面的博文对Lichee做了系列分析,事实上就是对在<七年之痒>中所说的,Android BSP具备的一项基本素养-SHELL脚本,所以我们Lichee系列的文章着重分析了SHELL脚本和 ...

  9. 解决无线网卡 RTL8723BE ubuntu环境下不稳定情况

    jiqing@ThinkPad:~$ lspci | grep -i net 00:19.0 Ethernet controller: Intel Corporation Ethernet Conne ...

  10. 自然常数 e 的理解与应用

    某彩票中奖率是百万分之一,则一个人买一百万张彩票仍不中奖的概率是: (1−1106)106≈1e e 往往出现在: 许多微小事件带来的总体变化 随机性和无穷多: