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
题意
可以参考这题的[中文题意]: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1419
题解
其实说白了,这道题是让你求每个节点到根节点的路径长度*点权值之和最小。最短路可以通过dijkstra来算,不过得用优先队列优化,否则会TLE。(也可以用SPFA)、
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
typedef long long LL;
const LL INF=(1LL<<16)*50000;
const int maxn=50005;
typedef long long LL;
#define ms(s,v) memset(s,v,sizeof(s))
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct edge
{
int to, cost;
};
typedef pair<int,int> P;
int n,m;
int w[maxn];
LL d[maxn];
vector<edge> G[maxn];
void clear()
{
for(int i=1;i<=n;i++)
G[i].clear();
}
void dijkstra()
{
priority_queue<P,vector<P>,greater<P> > que;
fill(d+1,d+n+1,INF);
d[1]=0;
que.push(P(0,1));
while(!que.empty())
{
P p=que.top();que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=0;i<G[v].size();i++)
{
edge &e=G[v][i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
LL sum=0;
for(int i=1;i<=n;i++)
{
if(d[i]==INF)
{
puts("No Answer");
return ;
}
sum+=w[i]*d[i];
}
printf("%lld
",sum);
}
int main()
{
ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
while(t--)
{
clear();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
for(int i=0;i<m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
G[u].push_back((edge){v,c});
G[v].push_back((edge){u,c});
}
dijkstra();
}
return 0;
}