码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2253 Frogger

时间:2018-06-15 20:01:53      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:思路   define   ted   efi   pos   CM   TE   最小   style   

题意:找第0个顶点到第1个顶点建,每条边距离最小的那个长度

思路:Dijkstra松弛的时候改一下就行了

notice:WA了一小时,就因为输出写的.3lf,要写成.3f,黑人问号???

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define MAXVERTEXNUM 210
#define INF 99999999
using namespace std;

struct Node
{
    int x, y;
}Stone[MAXVERTEXNUM];
double D[MAXVERTEXNUM][MAXVERTEXNUM], dist[MAXVERTEXNUM];
Node s, e;
int cas;

double calcDist(const Node & S1, const Node & S2)
{
    return sqrt(double((S1.x - S2.x) * (S1.x - S2.x) + (S1.y - S2.y) * (S1.y - S2.y)));
}

void Dijkstra()
{
    int Visited[MAXVERTEXNUM];
    memset(Visited, 0, sizeof(Visited));

    for (int i = 1; i < cas; ++i)
    {
        if (D[0][i])
            dist[i] = D[0][i];
        else
            dist[i] = INF;
    }
    Visited[0] = 1;
    dist[0] = 0;

    for (int i = 0; i < cas; ++i)
    {
        int MinDist = INF, pos;
        for (int j = 0; j < cas; ++j)
            if (!Visited[j] && dist[j] < MinDist)
            {
                MinDist = dist[j];
                pos = j;
            }

        Visited[pos] = 1;

        for (int j = 0; j < cas; ++j)
        {
            //松弛变形
            if (!Visited[j])
                dist[j] = min(dist[j], max(dist[pos], D[pos][j]));
        }
    }
}

int main()
{
//    freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    int Sc = 0;

    while (true)
    {
        cin >> cas;
        if (cas == 0)
            break;

        Sc++;
        for (int i = 0; i < cas; ++i)
            cin >> Stone[i].x >> Stone[i].y;

        for (int i = 0; i < cas; ++i)
            for (int j = 0; j < cas; ++j)
                D[i][j] = D[j][i] = calcDist(Stone[i], Stone[j]);

        Dijkstra();
        if (Sc == 1)
            printf("Scenario #%d\nFrog Distance = %.3f\n", Sc, dist[1]);
        else
            printf("\nScenario #%d\nFrog Distance = %.3f\n", Sc, dist[1]);
    }

    return 0;
}

 

POJ 2253 Frogger

标签:思路   define   ted   efi   pos   CM   TE   最小   style   

原文地址:https://www.cnblogs.com/ducklu/p/9188670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!