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

poj-2253 Frogger

时间:2015-01-28 16:05:24      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:acm   算法   poj   

题目链接:http://poj.org/problem?id=2253

求最短路径中的最长边,修改的 d [MAXN]; 的意义即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;

const int MAXV = 4010;
const int inf = 10000000;

struct Node
{
    float x,y;
}node[10010];

float makedis(Node a ,Node b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

float map[MAXV][MAXV];
float d[MAXV];
bool vis[MAXV];
int n,m;

void dijkstra(int s)
{
	for(int i=1;i<=n;i++)
    {
		vis[i]=0;
		d[i]=map[s][i];
	}
    d[1] = 0;

	while (1)
    {
		float min=inf;int v = -1;
		for(int i=1;i<=n;i++)
			if(!vis[i] && d[i]<min)
			{
				v=i;
				min=d[i];
			}
        if(v == -1)
            break;
		vis[v]=1;
        if (v == 2)
            break;
		for(int i=1;i<=n;i++)
			if(!vis[i] && d[i] > max(d[v] , map[v][i]))
				d[i] = max(map[v][i],d[v]);
	}
}

int main()
{
	int i,j,a,b,c;
	int cases = 1;
	while(scanf("%d",&n) != EOF)
    {
        if(n == 0 ) break;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                if(i==j)
                    map[i][i]=0;
                else
                    map[i][j]=map[j][i]=inf;
            }
            for(i=1;i<=n;i++)
            {
                scanf("%f%f",&node[i].x,&node[i].y);
            }
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    {
                        if (makedis(node[i],node[j]) < map[i][j])
                            map[j][i] = map[i][j] = makedis(node[i],node[j]);
                    }
		dijkstra(1);
		printf("Scenario #%d\nFrog Distance = ",cases++);
		printf("%.3f\n\n",d[2]);
	}
	return 0;
}


poj-2253 Frogger

标签:acm   算法   poj   

原文地址:http://blog.csdn.net/u014427196/article/details/43230537

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