标签:
题解:题目中给的是要求任意点之间的费用/距离,所以很容易想到floyd算法(不过N<=250,O(n^3)都能A是神马节奏= =,也对,常数不是很大)
然后就是按照点权来排一下序之后开干啦么么哒
1 /************************************************************** 2 Problem: 1774 3 User: HansBug 4 Language: Pascal 5 Result: Accepted 6 Time:1672 ms 7 Memory:940 kb 8 ****************************************************************/ 9 10 var 11 i,j,k,l,m,n,p,x,y,z:longint; 12 f,c:array[0..300,0..300] of longint; 13 a:array[0..500,1..2] of longint; 14 b:array[0..500] of longint; 15 function min(x,y:longint):longint; 16 begin 17 if x<y then min:=x else min:=y; 18 end; 19 function max(x,y:longint):longint; 20 begin 21 if x>y then max:=x else max:=y; 22 end; 23 procedure swap(var x,y:longint); 24 var z:longint; 25 begin 26 z:=x;x:=y;y:=z; 27 end; 28 procedure sort(l,r:longint); 29 var i,j,x,y:longint; 30 begin 31 i:=l;j:=r;x:=a[(l+r) div 2,1]; 32 repeat 33 while a[i,1]<x do inc(i); 34 while a[j,1]>x do dec(j); 35 if i<=j then 36 begin 37 swap(a[i,1],a[j,1]); 38 swap(a[i,2],a[j,2]); 39 inc(i);dec(j); 40 end; 41 until i>j; 42 if i<r then sort(i,r); 43 if l<j then sort(l,j); 44 end; 45 46 begin 47 fillchar(f,sizeof(f),63); 48 fillchar(c,sizeof(c),63); 49 readln(n,m,p); 50 for i:=1 to n do 51 begin 52 readln(a[i,1]);a[i,2]:=i; 53 f[i,i]:=0; 54 end; 55 sort(1,n); 56 for i:=1 to n do b[a[i,2]]:=i; 57 for i:=1 to m do 58 begin 59 readln(x,y,z); 60 x:=b[x];y:=b[y]; 61 f[x,y]:=min(f[x,y],z); 62 f[y,x]:=f[x,y]; 63 end; 64 for k:=1 to n do 65 for i:=1 to n do 66 for j:=1 to n do 67 begin 68 f[i,j]:=min(f[i,j],f[i,k]+f[k,j]); 69 c[i,j]:=min(c[i,j],f[i,j]+max(a[k,1],max(a[i,1],a[j,1]))); 70 end; 71 for i:=1 to p do 72 begin 73 readln(x,y); 74 writeln(c[b[x],b[y]]); 75 end; 76 readln; 77 end.
标签:
原文地址:http://www.cnblogs.com/HansBug/p/4426284.html