zoukankan      html  css  js  c++  java
  • poj2253

    dijkstra

    把存最短距离的数组改为存储frog distance即可

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    using namespace std;
    
    const int maxn = 201, inf = 1000000000;
    
    struct point
    {
    	int x, y;
    } stone[maxn];
    
    int n;
    double dist[maxn];
    bool vis[maxn];
    
    void init()
    {
    	for (int i = 0; i < n; i++)
    		scanf("%d%d", &stone[i].x, &stone[i].y);
    }
    
    double distan(point &a, point &b)
    {
    	return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }
    
    void dijkstra()
    {
    	double best = 0;
    	int besti = 0;
    
    	for (int i = 0; i < n; i++)
    		dist[i] = inf;
    	memset(vis, 0, sizeof(vis));
    	vis[0] = true;
    	dist[0] = 0;
    	while (besti != -1 && !vis[1])
    	{
    		for (int i = 0; i < n; i++)
    		{
    			double temp = max(dist[besti], distan(stone[i], stone[besti]));
    			if (dist[i] > temp)
    				dist[i] = temp;
    		}
    		best = inf;
    		besti = -1;
    		for (int i = 0; i < n; i++)
    			if (!vis[i] && dist[i] < best)
    			{
    				best = dist[i];
    				besti = i;
    			}
    		vis[besti] = true;
    	}
    }
    
    int main()
    {
    	//freopen("D:\\t.txt", "r", stdin);
    	int t = 0;
    	while (scanf("%d", &n) != EOF && n != 0)
    	{
    		init();
    		dijkstra();
    		t++;
    		printf("Scenario #%d\nFrog Distance = %.3f\n\n", t, dist[1]);
    	}
    	return 0;
    }
  • 相关阅读:
    NEO从入门到开窗(4)
    NEO从入门到开窗(3)
    NEO从入门到开窗(2)
    NEO从入门到开窗(1)
    重读大型网站技术架构
    c#并行编程
    关于使用CPU缓存的一个小栗子
    Visual Studio中从应用程序中调试SQL脚本
    JavaScript启示录
    LabVIEW工控二进制数据存储
  • 原文地址:https://www.cnblogs.com/rainydays/p/1948668.html
Copyright © 2011-2022 走看看