中中救援队
背景 Background
中中酷爱滑雪,某日突发奇想,带领所有BDEZ的OIER去Alps滑雪,不幸的是,中中和OIER们遭遇了雪崩,除了中中,所有的OIER们都埋在了雪坑里,此时,中中救援队闪亮登场~!(中中救援队只有中中一个人!Orz!)
描述 Description
雪崩之后,出现了N个雪坑,每个雪坑都有一名OIER深陷其中,只有中中幸免,现在中中找到了M条双向道路,每条道路都会连接两个雪坑,但是,由于中中是路痴(-_-||),所以中中希望去除M条道路中尽可能多的道路,但是还要保证任一雪坑都能到达其他的雪坑,所以要先选择留下N-1条道路,中中可以从任意一个雪坑出发,并且任务完成后要回到出发点(起点的雪坑和终点的雪坑也需要消耗体力),而且中中只记得他选择的道路
中中每到一个雪坑i,都会消耗一定的体力值t[i],即使这个雪坑的OIER已被救出。第j条道路连接x,y两个雪坑,而从x到达y也需要消耗体力值energy
由于时间紧迫,中中请你这名OIER帮助他计算救出这N名OIER所消耗的最小体力值是多少
——By 宇溟义诚、five213ddking
输入格式 Input Format
输入第一行两个整数N和M,表示有N名OIER,M条连接的道路
接下来N行,每行一个整数t[i],表示第i个雪坑需要消耗中中的体力值
然后M行,每行三个整数x,y,energy,表示从x坑滑到y坑需要消耗的体力值为energy
输出格式 Output Format
第1行,一个整数,为中中消耗的最小体力值
样例输入 Sample Input
5 7
6
5
13
8
18
4 1 7
5 2 5
1 5 16
2 3 20
3 1 18
4 3 12
2 4 15
样例输出 Sample Output
154
注释 Hint
对于30%的数据
5<=N<=100,N-1<=M<=500
1<=t[i]<=100
x<=N,y<=N,x<>y,1<=energy<=100
对于全部数据
5<=N<=10000,N-1<=M<=100000
1<=t[i]<=1000
x<=N,y<=N,x<>y,1<=energy<=1000
结果保证不超过maxlongint
时间限制 Time Limitation
各个测试点1s
readln(a[i],b[i],c[i]);
c[i]:=c[i]*2+a[i]+b[i];
然后最小生成树即可
画一下图就知道了
var a,b,c,fa,v:array[0..100000] of longint;
n,m,i,j,k,sum,min:longint;
procedure qsort(l,r:longint);
var i,j,k,x:longint;
begin
i:=l; j:=r; x:=c[(l+r) div 2];
repeat
while c[i]<x do inc(i);
while c[j]>x do dec(j);
if i<=j then
begin
k:=a[i]; a[i]:=a[j]; a[j]:=k;
k:=b[i]; b[i]:=b[j]; b[j]:=k;
k:=c[i]; c[i]:=c[j]; c[j]:=k;
inc(i); dec(j);
end;
until i>j;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end;
function find(x:longint):longint;
begin
if x<>fa[x] then fa[x]:=find(fa[x]);
exit(fa[x]);
end;
procedure shu;
var i,j,k,dx,dy:longint;
begin
k:=0;
for i:=1 to n do fa[i]:=i;
for i:=1 to m do
begin
dx:=find(a[i]); dy:=find(b[i]);
if dx<>dy then
begin
inc(k); inc(sum,c[i]); fa[dx]:=dy;
end;
if k=n-1 then exit;
end;
end;
begin
{assign(input,'p2.in'); assign(output,'p2.out');
reset(input); rewrite(output);}
readln(n,m); min:=maxlongint;
for i:=1 to n do
begin
readln(v[i]);
k:=k+v[i]; if v[i]<min then min:=v[i];
end;
{writeln(k);}
for i:=1 to m do
begin
readln(a[i],b[i],c[i]);
c[i]:=c[i]*2+v[a[i]]+v[b[i]];
end;
qsort(1,m);
shu;
writeln(sum+min);
{close(input); close(output);}
end.