poj2253--dijkstra

昨天学的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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值