# 2.4.3 Cow Tours 牛的旅行

2.4.3 Cow Tours 牛的旅行

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

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

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

```　 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
```

``````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``````

``22.071068``

``````#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#define N 160
#define MAX 10000000000000000000

typedef struct
{
double x;
double y;
}pos;

int n;
pos p[N];
char c;
double len[N][N];
double maxlen[N];
double res=MAX;

int main()
{
int i,j,k;
double tmplen;
cin>>n;
for(i=1;i<=n;i++)
cin>>p[i].x>>p[i].y;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
cin>>c;
if(i==j)
continue;
if(c=='1')
{
tmplen=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));
len[i][j]=tmplen;
}
else
len[i][j]=MAX;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(len[i][k]+len[k][j]<len[i][j])
len[i][j]=len[i][k]+len[k][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(maxlen[i]<len[i][j]&&len[i][j]!=MAX)
maxlen[i]=len[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(len[i][j]==MAX)
{
tmplen=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));
if(tmplen+maxlen[i]+maxlen[j]<res)
res=tmplen+maxlen[i]+maxlen[j];
}
for(i=1;i<=n;i++)
if(maxlen[i]>res)
res=maxlen[i];
cout<<setiosflags(ios::fixed)<<setprecision(6)<<res<<endl;
return 0;
}``````

``````#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#define N 160
#define MAX 10000000000000000000

typedef struct
{
double x;
double y;
}pos;

int n;
pos p[N];
char c;
double len[N][N];
double maxlen[N];
double res=MAX;

int main()
{
int i,j,k;
double tmplen;
cin>>n;
for(i=1;i<=n;i++)
cin>>p[i].x>>p[i].y;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
cin>>c;
if(i==j)
continue;
if(c=='1')
{
tmplen=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));
len[i][j]=tmplen;
}
else
len[i][j]=MAX;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(len[i][k]+len[k][j]<len[i][j])
len[i][j]=len[i][k]+len[k][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(maxlen[i]<len[i][j]&&len[i][j]!=MAX)
maxlen[i]=len[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(len[i][j]==MAX)
{
tmplen=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));
if(tmplen+maxlen[i]+maxlen[j]<res)
res=tmplen+maxlen[i]+maxlen[j];
}
for(i=1;i<=n;i++)
if(maxlen[i]>res)
res=maxlen[i];
cout<<setiosflags(ios::fixed)<<setprecision(6)<<res<<endl;
return 0;
}``````