记录编号 114726 评测结果 AAAAAA
题目名称 [USACO 2.4.3]牛的旅行 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.063 s
提交时间 2014-08-01 10:59:00 内存使用 0.54 MiB
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
const int INF=0x7ffffff;
//==============struct declaration==============
struct farms
{
  int x,y;
  double dist(const farms i) const
  {
    return sqrt(double((x-i.x)*(x-i.x)+(y-i.y)*(y-i.y)));
  }
}farm[160];
//==============var declaration=================
bool con[160][160];
double dist[160][160];
int n;
int root[160];
//==============function declaration============
void cross(int a,int b);
int findr(int x);
//==============main code=======================
int main()
{  
  string FileName="cowtour";//程序名 
  string FloderName="USACO";//文件夹名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG  
  system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
#endif  
  scanf("%d",&n);
  For(i,1,n)
    For(j,1,n)
      dist[i][j]=INF;
  For(i,1,n)
  {
    root[i]=i;
    scanf("%d%d",&farm[i].x,&farm[i].y);
    For(j,1,i-1)
      dist[i][j]=dist[j][i]=farm[i].dist(farm[j]);
    dist[i][i]=0;
  }
  char ava;
  cin.ignore(10,10);
  For(a,1,n)
  {
    For(b,1,n)
    {
      scanf("%c",&ava);
      if (ava=='0')
        con[a][b]=false;
      else
      {
        con[a][b]=true;
        if (a<b) 
          cross(a,b);
      }
    }
    cin.ignore(10,10);
  }
  For(k,1,n)
    For(i,1,n)
      For(j,1,n)
        if (dist[i][k]+dist[k][j]<dist[i][j])
          dist[i][j]=dist[i][k]+dist[j][k];
  double mindist=INF;
  For(first,2,n)
    For(sec,1,first-1)
    {
      if (findr(first)==findr(sec))
        continue;
      double dist1=0,dist2=0;
      For(i,1,n)
        if (findr(first)==findr(i))
          if (dist[first][i]>dist1)
            dist1=dist[first][i];
      For(i,1,n)
        if (findr(sec)==findr(i))
          if (dist[sec][i]>dist2)
            dist2=dist[sec][i];
      if (dist1+dist2+dist[first][sec]<mindist)
         mindist=dist1+dist2+dist[first][sec];
    }
  printf("%.6lf",mindist);
  return 0;
}
//================fuction code====================
void cross(int a,int b)
{
  root[findr(a)]=findr(b);
}
int findr(int x)
{
  return root[x]==x?x:root[x]=findr(root[x]);
}