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. spring-cloud-starter-ribbon提供客户端的软件负载均衡算法

    Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接超时 ...

  2. Spark之安装和使用

    Scala安装 Spark使用Scala开发,安装的Scala版本必须和Spark使用的版本一致,否则使用Scala编写的Spark任务会有兼容性问题 可以到Spark官网查看确定Scala版本,或者 ...

  3. python_swift_project_swift使用

    1. swift的存取用curl命令. 我们先把pub url 和token保存起来 root@A071103070098:~# export pubURL=http://10.194.148.102 ...

  4. 使用JSONObject 深度序列化和反序列化

    JSONObject 和JSONArray 是json-lib.jar里面最经常使用的两个类,分别能够对对象和数组(集合)进行序列化和反序列化,结构清晰命了,简单易用,功能强大,效率比較高,使用至今一 ...

  5. Scope Is the Enemy of Success

     Scope Is the Enemy of Success Dave Quick SCopE REFERS To A pRojECT'S SizE. How much time, effort, ...

  6. hdu 4193 单调队列

    题意是给你n个数   组成的环   求以一个数开头 的数列全部前缀都为非负数的数列的个数: 思路:  先扩展成2*n的数列 然后求出sum[i]表示前i项的和     对每一个i>.=n结尾的数 ...

  7. C++之:友元类

    一.文章来由 上一篇写了友元函数,这一次写一个姊妹篇,继续深入探究一下友元类. 二.定义 友元类的全部成员函数都是还有一个类的友元函数.都能够訪问还有一个类中的隐藏信息(包含私有成员和保护成员). 当 ...

  8. Shiro学习(22)集成验证码

    在做用户登录功能时,非常多时候都须要验证码支持,验证码的目的是为了防止机器人模拟真有用户登录而恶意訪问,如暴力破解用户password/恶意评论等. 眼下也有一些验证码比較简单,通过一些OCR工具就能 ...

  9. java及前端请求跨域问题

    主要代码:<meta http-equiv="Access-Control-Allow-Origin" content="*"> 说明一下什么情况下 ...

  10. research plan1111

    Hello prof.Choi 感谢您的来电,与您的这次通话我已经期盼了很久.我来做个自我介绍,我叫陈飞,今年27岁了,是河北地质大学计算机科学专业的本科毕业生.我非常想提高自己的学历,现在经过刘老师 ...