昨天学的dijkstra算法,尝试了下,解这种裸算法的题目还是很方便的啊~
#define LOCAL
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAXN 1000000 + 10
typedef struct Point
{
double x;
double y;
} ;
Point points[MAXN];
double minJump[MAXN];
int redPoint[MAXN];
int bluePoint[MAXN];
//计算两点间的距离
double calcMinJump(Point point1, Point point2)
{
double distance;
distance = sqrt((point1.x - point2.x) * (point1.x - point2.x) + (point1.y - point2.y) * (point1.y - point2.y));
return distance;
}
int main()
{
#ifdef LOCAL
freopen("C:\\Users\\Administrator\\Desktop\\ACMTempIn.txt", "r", stdin);
freopen("C:\\Users\\Administrator\\Desktop\\ACMTempOut.txt", "w", stdout);
#endif
int cases;
int i, j, min, count = 0;
//数据输入
while(scanf("%d", &cases) && cases != 0)
{
memset(points, 0, sizeof(points));
count ++;
for(i = 0; i < cases; i++)
{
scanf("%lf%lf", &points[i].x, &points[i].y);
}
//初始化红点集和蓝点集
memset(redPoint, 0, sizeof(redPoint));
memset(bluePoint, 0, sizeof(bluePoint));
memset(minJump, 0, sizeof(minJump));
redPoint[0] = 1;
bluePoint[0] = 0;
for(i = 1; i < cases; i++)
{
bluePoint[i] = 1;
}
for(i = 1; i < cases; i++)
{
minJump[i] = calcMinJump(points[i],points[0]);
}
//遍历蓝点集
for(i = 1; i < cases; i++)
{
//获得最短路径的蓝点
for(min = 1; bluePoint[min] == 0;)min++;
for(j = min; j < cases; j++)
if((minJump[j] < minJump[min]) && bluePoint[j] == 1) min = j;
bluePoint[min] = 0;
redPoint[min] = 1;
for(j = 0; j < cases; j++)
{
if(min != j)
{
double temp = calcMinJump(points[min], points[j]);
if(calcMinJump(points[min], points[j]) < minJump[min])
temp = minJump[min];
if(minJump[j] > temp)
{
minJump[j] = temp;
}
}
}
}
printf("Scenario #%d\nFrog Distance = %.3lf\n\n", count, minJump[1]);
}
return 0;
}