题意/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.
题解/solution:
这题一看就是最短路,因为题目说到求边缘的最小值。题目还讲到一个关于边缘价格的式子
{(边缘的价格)=(所有后代节点的权重之和)×(边缘单价)},这在输出的时候处理一下就行了。因为这题有T组数据,所以注意让数组和变量还原值。
这题我用SPFA做,本因为简单的。结果出现了神奇的错误:Runtime Error。我一脸懵逼,改了不少地方,刷了10多次,才AC。结果那错了,我也不知道。我在这祝大家好运,没有此错误。
代码/Code:
const
maxE=100001;
maxV=2000001;
type
arr=record
x,y,w,next:int64;
end;
var
n,m,nm:longint;
a:array [0..maxV] of arr;
ls:array [0..maxE] of longint;
list,b,d,v:array [0..maxE] of int64;
ans,max:int64;
procedure spfa;
var
i,j,k,h,t:longint;
begin
fillchar(d,sizeof(d),63);
max:=d[1]; h:=0; t:=1;
v[1]:=1; list[1]:=1; d[1]:=0;
repeat
h:=h+1;
j:=ls[list[h]];
while j<>0 do
begin
with a[j] do
begin
if d[x]+w<d[y] then
begin
d[y]:=d[x]+w;
if v[y]=0 then
begin
t:=t+1;
list[t]:=y;
v[y]:=1;
end;
end;
j:=next;
end;
end;
v[list[h]]:=0;
until h=t;
end;
procedure init1;
begin
fillchar(ls,sizeof(ls),0);
fillchar(list,sizeof(list),0);
fillchar(b,sizeof(b),0);
fillchar(a,sizeof(a),0);
fillchar(v,sizeof(v),0);
end;
procedure init2;
var
i,t,k:longint;
begin
readln(n,m);
for i:=1 to n do
read(b[i]);
for i:=1 to m do
begin
t:=i*2; k:=t-1;
with a[k] do
begin
readln(x,y,w);
next:=ls[x];
ls[x]:=k;
end;
with a[t] do
begin
x:=a[k].y; y:=a[k].x; w:=a[k].w;
next:=ls[x];
ls[x]:=t;
end;
end;
m:=m*2;
end;
procedure print;
var
i:longint;
begin
ans:=0;
for i:=1 to n do
if d[i]=max then begin writeln('No Answer'); exit; end
else ans:=d[i]*b[i]+ans;
writeln(ans);
end;
procedure init_main;
var
i:longint;
begin
readln(nm);
for i:=1 to nm do
begin
init1;
init2;
spfa;
print;
end;
end;
begin
init_main;
end.