题目大意:两个青蛙,n个石头,(两只青蛙分别在石头1和石头2上),求青蛙1到青蛙2的最小距离中的最大值(石头间的最大值)。
最小生成树、最短路、dfs。。。都可以AC
附AC代码
#include<iostream>
#include<math.h>
using namespace std;
struct Point
{
int x, y;
};
double CalValue(const Point Point_1, const Point Point_2)
{
return (double)sqrt((double)pow((double)Point_2.x - (double)Point_1.x, 2) + (double)pow((double)Point_1.y - (double)Point_2.y, 2));
}
int main()
{
int n;
int nFlag = 0;
double Map[201][201];
bool ArrMark[201];
while (cin >> n&&n)
{
Point *ArrPoint = new Point[n];
double *ArrTemp = new double[n];
for (int i = 0; i < n; i++)
{
cin >> ArrPoint[i].x >> ArrPoint[i].y;
}
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
Map[i][j] = CalValue(ArrPoint[i], ArrPoint[j]);
Map[j][i] = Map[i][j];
}
}
memset(ArrMark, false, sizeof(ArrMark));
ArrMark[0] = true;
for (int i = 1; i < n; i++)
{
ArrTemp[i] = Map[0][i];
}
int k = -1;
double nMin = 10000000.0;
double fAns = 0.0;
for (int i = 0; i < n; i++)
{
k = -1;
nMin = 10000000.0;
for (int j = 0; j < n; j++)
{
if (!ArrMark[j] && nMin > ArrTemp[j])
{
nMin = ArrTemp[j];
k = j;
}
}
if (k == -1)
{
break;
}
if (nMin >= fAns)
{
fAns = nMin;
}
if (k == 1)
{
break;
}
ArrMark[k] = true;
for (int j = 0; j < n; j++)
{
if (ArrTemp[j] > Map[k][j])
{
ArrTemp[j] = Map[k][j];
}
}
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n", ++nFlag, fAns);
delete[] ArrTemp;
delete[] ArrPoint;
}
}