• POJ2607:Fire Station(SPFA+枚举)


    http://poj.org/problem?id=2607

    Description

    A city is served by a number of fire stations. Some residents have complained that the distance from their houses to the nearest station is too far, so a new station is to be built. You are to choose the location of the fire station so as to reduce the distance to the nearest station from the houses of the disgruntled residents.
    The city has up to 500 intersections, connected by road segments of various lengths. No more than 20 road segments intersect at a given intersection. The location of houses and firestations alike are considered to be at intersections (the travel distance from the intersection to the actual building can be discounted). Furthermore, we assume that there is at least one house associated with every intersection. There may be more than one firestation per intersection.

    Input

    The first line of input contains two positive integers: f,the number of existing fire stations (f <= 100) and i, the number of intersections (i <= 500). The intersections are numbered from 1 to i consecutively. f lines follow; each contains the intersection number at which an existing fire station is found. A number of lines follow, each containing three positive integers: the number of an intersection, the number of a different intersection, and the length of the road segment connecting the intersections. All road segments are two-way (at least as far as fire engines are concerned), and there will exist a route between any pair of intersections.

    Output

    You are to output a single integer: the lowest intersection number at which a new fire station should be built so as to minimize the maximum distance from any intersection to the nearest fire station.

    Sample Input

    1 6
    2
    1 2 10
    2 3 10
    3 4 10
    4 5 10
    5 6 10
    6 1 10
    

    Sample Output

    5
    

    题意分析:

    有f个已经建好的消防站,在村庄里再建一个消防站,要求离最远的村庄的距离最小。

    解题思路:

    先对于每一个已经建好的消防站,求出村庄离消防站的最短距离,再枚举每一个村庄,在这个村庄建消防站后求最短路,再看最大距离是否变小,更新消防站的位置。

    #include <stdio.h>
    #include <vector>
    #include <queue>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    #define N 1020
    int a[N], dis[N], dis1[N], book[N], inf=99999999;
    struct edge {
    	int v;
    	int w;
    };
    
    vector<edge>e[N];
    void SPFA(int u)
    {
    	int i, v, w;
    	dis[u] = 0;
    	queue<int>q;
    	memset(book, 0, sizeof(book));
    	q.push(u);
    	book[u] = 1;
    	while (!q.empty())
    	{
    		u = q.front();
    		q.pop();
    		book[u] = 0;
    		for (i = 0; i < e[u].size(); i++) 
    		{
    			v = e[u][i].v;
    			w = e[u][i].w;
    			if (dis[v] > dis[u] + w)
    			{
    				dis[v] = dis[u] + w;
    				if (book[v] == 0)
    				{
    					book[v] = 1;
    					q.push(v);
    				}
    			}
    		}
    	}
    }
    int main()
    {
    	int n, q, i, j, u, v, w, num, ans, maxi;
    	while(scanf("%d%d", &q, &n)!=EOF)
    	{
    		for (i = 1; i <= n; i++)
    			e[i].clear();
    		for (i = 0; i < q; i++)
    		{
    			scanf("%d", &a[i]);
    		}
    		while (scanf("%d%d%d", &u, &v, &w) != EOF)
    		{
    			e[u].push_back(edge{v, w});
    			e[v].push_back(edge{ u, w });
    		}
    		for (i = 1; i <= n; i++)
    			dis[i] = inf;
    		for (i = 0; i < q; i++)
    			SPFA(a[i]);
    		maxi = 0;
    		for (i = 1; i <= n; i++)
    		{
    			dis1[i] = dis[i];
    			maxi = max(maxi, dis[i]);
    		}
    		ans = 1;
    		for (i = 1; i <= n; i++)
    		{
    			for (j = 1; j <= n; j++)
    				dis[j] = dis1[j];
    			SPFA(i);
    			num = 0;
    			for (j = 1; j <= n; j++)
    				num = max(num, dis[j]);
    			if (num < maxi)
    			{
    				ans = i;
    				maxi = num;
    			}
    				
    		}
    		printf("%d
    ", ans);
    	}	
    	return 0;
    }
  • 相关阅读:
    ios开发函数(数学函数应用)
    苹果的软件/系统盘 网站 http://www.panduoduo.net/u/bd-369186934/2
    iOS-运行时机制
    ios滑动手势全屏(这段代码实现了下一级控制器滑到上一级控制器)
    js正则表达式总结
    text-shadow属性
    css3的box-shadow属性
    white-space属性
    js的sort()方法详解
    搜索框demo
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852604.html
Copyright © 2020-2023  润新知