Time Limit: 3000MS
Memory Limit: 131072K
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) 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 a , b , c indicating the edge which is able to connect two nodes a a and , and unit price c c .
All numbers in input are less than .
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
Source
POJ Monthly–2006.09.29, Kim, Chan Min (kcm1700@POJ)
题意:
给一个图,n个点,m条无向边。询问是否存在一个以1为根的有根树,使得这个树的价值最小(树的价值表示为
∑(树上的边权∗该边权所连接的子树的点权和)
∑
(
树
上
的
边
权
∗
该
边
权
所
连
接
的
子
树
的
点
权
和
)
)询问最小价值,如果不存在则输出No Answer
题解:
∑(树上的边权∗该边权所连接的子树的点权和)=∑(每个点的点权∗从点1到该点的最短路长度)
∑
(
树
上
的
边
权
∗
该
边
权
所
连
接
的
子
树
的
点
权
和
)
=
∑
(
每
个
点
的
点
权
∗
从
点
1
到
该
点
的
最
短
路
长
度
)
然后就是最短路了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#define ll long long
#define LiangJiaJun main
#define INF 19991227LL*100000000LL
using namespace std;
int n,m,ne,h[50004],a[50004];
ll dis[50004],ans;
struct edge{
int to,nt;ll w;
}e[200004];
int add(int u,int v,int w){
e[++ne].to=v;e[ne].nt=h[u];e[ne].w=1LL*w;
h[u]=ne;
return 0;
}
queue<int>q;
bool inq[50004];
int spfa(int st){
for(int i=0;i<=50001;i++)dis[i]=INF;
dis[1]=0;
inq[1]=1;
q.push(1);
while(!q.empty()){
int x=q.front();q.pop();
for(int i=h[x];i;i=e[i].nt){
if( dis[e[i].to]>dis[x]+e[i].w){
dis[e[i].to]=dis[x]+e[i].w;
if(!inq[e[i].to]){
inq[e[i].to]=1;
q.push(e[i].to);
}
}
}
inq[x]=0;
}
return 0;
}
int w33ha(){
scanf("%d%d",&n,&m);
ne=0;
memset(h,0,sizeof(h));
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
spfa(1);
ans=0;
int ok=1;
for(int i=2;i<=n;i++){
if(dis[i]!=INF)ans+=(1LL*a[i]*dis[i]);
else {ok=0;break;}
}
if(ok)printf("%lld\n",ans);
else puts("No Answer");
return 0;
}
int LiangJiaJun(){
int T;scanf("%d",&T);
while(T--)w33ha();
return 0;
}