题目大意
农民John的农场里有很多牧区。有的路径连接一些特定的牧区。一片所有连通的牧区称为一个牧场。但是就目前而言,你能看到至少有两个牧区不连通。这样,农民John就有多个牧区了。
John想在农场里添加一条路径(注意,恰好一条)。对这条路径有以下限制:
一个牧场的直径就是牧场中最远的两个牧区的距离(本题中所提到的所有距离指的都是最短的距离)。考虑如下的有5个牧区的牧场,牧区用“*”表示,路径用直线表示。每一个牧区都有自己的坐标:
![](http://10.156.17.250/JudgeOnline/image/1119-1.jpg)
这个牧场的直径大约是12.07106, 最远的两个牧区是A和E,它们之间的最短路径是A-B-E。
这里是另一个牧场:
![](http://10.156.17.250/JudgeOnline/image/1119-2.jpg)
这两个牧场都在John的农场上。John将会在两个牧场中各选一个牧区,然后用一条路径连起来,使得连通后这个新的更大的牧场有最小的直径。
注意,如果两条路径中途相交,我们不认为它们是连通的。只有两条路径在同一个牧区相交,我们才认为它们是连通的。
输入文件包括牧区、它们各自的坐标,还有一个如下的对称邻接矩阵:
A B C D E F G H
A 0 1 0 0 0 0 0 0
B 1 0 1 1 1 0 0 0
C 0 1 0 0 1 0 0 0
D 0 1 0 0 1 0 0 0
E 0 1 1 1 0 0 0 0
F 0 0 0 0 0 0 1 0
G 0 0 0 0 0 1 0 1
H 0 0 0 0 0 0 1 0
输入文件至少包括两个不连通的牧区。
请编程找出一条连接两个不同牧场的路径,使得连上这条路径后,这个更大的新牧场有最小的直径。
Input
第1行: 一个整数N (1 <= N <= 150), 表示牧区数
第2到N+1行: 每行两个整数X,Y (0 <= X ,Y<= 100000), 表示N个牧区的坐标。注意每个 牧区的坐标都是不一样的。
第N+2行到第2*N+1行: 每行包括N个数字(0或1) 表示如上文描述的对称邻接矩阵。
Output
只有一行,包括一个实数,表示所求答案。数字保留六位小数。
算出两点之间的距离
sqrt(sqr(x[a]-x[b])+sqr(y[a]-y[b]))
用Floyd求出任意两点之间的最短路径,然后求出每个点到所有可以到达点的最大距离。
f[i,j]:=min(f[i,j],f[i,k]+f[k,j]);
max:=max(m[i]+m[j]+t,max);
代码
var i,j,k,n:longint; s:char; f:array[0..200,0..200] of real; min,t:real; x,y:array[0..200] of longint; m:array[0..200] of real; function dis(a,b:longint):real; begin exit(sqrt(sqr(x[a]-x[b])+sqr(y[a]-y[b]))) end; begin readln(n); for i:=1 to n do readln(x[i],y[i]); for i:=1 to n do begin for j:=1 to n do begin read(s); if s='1' then f[i,j]:=sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j])) else f[i,j]:=maxlongint div 3; end; readln; end; for k:=1 to n do for i:=1 to n do for j:=1 to n do if (i<>j)and(i<>k)and(j<>k) then if f[i,k]+f[k,j]<f[i,j] then f[i,j]:=f[i,k]+f[k,j]; min:=maxlongint; for i:=1 to n do for j:=1 to n do if (f[i,j]<100000)and(m[i]<f[i,j]) then m[i]:=f[i,j]; for i:=1 to n do for j:=1 to n do if (i<>j)and(f[i,j]>100000)then begin t:=dis(i,j); if m[i]+m[j]+t<min then min:=m[i]+m[j]+t; end; for i:=1 to n do if m[i]>min then min:=m[i]; writeln(min:0:6); end.
![睡觉](http://static.blog.csdn.net/xheditor/xheditor_emot/default/sleep.gif)