【♂Not The Triumph♂O(∩_∩)O哈哈~But The Struggle♂】

竞赛决不是捷径,它只是另一种艰辛的生活方式。得到与失去,只有时间会去评判;成功与失败,只有历史能去仲裁。我不会永远成功,正如我不会永远失败一样

  C++博客 :: 首页 :: 联系 ::  :: 管理
  6 Posts :: 239 Stories :: 25 Comments :: 0 Trackbacks

常用链接

留言簿(7)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 104767
  • 排名 - 233

最新评论

阅读排行榜

评论排行榜

农民John的农场里有很多牧区。有的路径连接一些特定的牧区。一片所有连通的牧区称为一个牧场。但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通。这样,农民John就有多个牧场了。

John想在农场里添加一条路径(注意,恰好一条)。对这条路径有以下限制:

一个牧场的直径就是牧场中最远的两个牧区的距离(本题中所提到的所有距离指的都是最短的距离)。考虑如下的有5个牧区的牧场,牧区用“*”表示,路径用直线表示。每一个牧区都有自己的坐标:

               15,15   20,15
                 D       E
                 *-------*
                 |     _/|
                 |   _/  |
                 | _/    |
                 |/      |
        *--------*-------*
        A        B       C
        10,10   15,10   20,10

这个牧场的直径大约是12.07106, 最远的两个牧区是A和E,它们之间的最短路径是A-B-E。

这里是另一个牧场:

                             *F 30,15
                        /
                      _/ 
                    _/   
                   /     
                  *------*
                  G      H
                  25,10   30,10

这两个牧场都在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

输入文件至少包括两个不连通的牧区。
请编程找出一条连接两个不同牧场的路径,使得连上这条路径后,这个更大的新牧场有最小的直径。

格式
PROGRAM NAME: cowtour

INPUT FORMAT:
(file cowtour.in)

第1行: 一个整数N (1 <= N <= 150), 表示牧区数

第2到N+1行: 每行两个整数X,Y (0 <= X ,Y<= 100000), 表示N个牧区的坐标。注意每个 牧区的坐标都是不一样的。

第N+2行到第2*N+1行: 每行包括N个数字(0或1) 表示如上文描述的对称邻接矩阵。

OUTPUT FORMAT:
(file cowtour.out)

只有一行,包括一个实数,表示所求直径。数字保留六位小数。

SAMPLE INPUT
8
10 10
15 10
20 10
15 15
20 15
30 15
25 10
30 10
01000000
10111000
01001000
01001000
01110000
00000010
00000101
00000010

SAMPLE OUTPUT
22.071068

【参考程序】:

{
 ID: XIONGNA1
 PROG: cowtour
 LANG: PASCAL
}
program cowtour;
type edge=record
      x,y:double;
      end;

var cost:array[1..150,1..150]of double;
    elist:array[1..150]of edge;
    n,i,j:longint;
    ch:char;
    ss,maxn:double;
function jisuan(x1,y1,x2,y2:double):double;
begin
    jisuan:=sqrt(sqr(x1-x2)+sqr(y1-y2));
end;
procedure floyd;
var i,j,k:longint;
begin
  for k:=1 to n do
    for i:=1 to n do
      if k<>i then
        for j:=1 to n do
          if (k<>j)and(i<>j) then
            if cost[i,j]>cost[i,k]+cost[k,j] then
              cost[i,j]:=cost[i,k]+cost[k,j]
end;
procedure flish;
var i,j,k:longint;
    max1,max2,kk,maxd,pp,max:double;
begin
    maxd:=maxn;
    for i:=1 to n do
     for j:=1 to n do
       if i<>j then
         if cost[i,j]=maxn then
         begin
             max1:=0.0;
             for k:=1 to n do
                if k<>i then
                  if cost[i,k]<>maxn then
                    if cost[i,k]>max1 then
                      max1:=cost[i,k];
             max2:=0.0;
             for k:=1 to n do
               if k<>j then
                 if cost[j,k]<>maxn then
                   if cost[j,k]>max2 then
                     max2:=cost[j,k];
             kk:=jisuan(elist[i].x,elist[i].y,elist[j].x,elist[j].y);
             if maxd>max1+max2+kk then  maxd:=max1+max2+kk;
         end;
    max:=0.0;
    for i:=1 to n do
      for j:=1 to n do
        if (i<>j)and(cost[i,j]<>maxn) then
          if cost[i,j]>max then
            max:=cost[i,j];
    if max>maxd then pp:=max
    else pp:=maxd;
    writeln(pp:0:6);
end;
begin
     //assign(input,'cowtour.in');reset(input);
     //assign(output,'cowtour.out');rewrite(output);
    while not eof do
    begin
        readln(n);
        maxn:=500000000.88;
        for i:=1 to n do
          for j:=1 to n do
            if i=j then cost[i,j]:=0
            else cost[i,j]:=maxn;
        for i:=1 to n do
            readln(elist[i].x,elist[i].y);//du
        for i:=1 to n do
        begin
          for j:=1 to n do
          begin
            read(ch);
            if ch='1' then
            begin //jisuan
               ss:=jisuan(elist[i].x,elist[i].y,elist[j].x,elist[j].y);
               cost[i,j]:=ss;
               cost[j,i]:=ss;
            end;
          end;
          readln;
        end;
        floyd;
        flish;
    end;
    //close(input);close(output);
end.


posted on 2009-07-21 14:23 开拓者 阅读(272) 评论(0)  编辑 收藏 引用 所属分类: 图论算法&例题USACO 题解

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理