UVa 10278 - Fire Station

题目:一个城市有i个小镇,其中有一些小镇建有消防站,现在想增加1个消防站,

使得所有小镇到最近的消防站的距离中的最大值最小。

分析:图论,最短路。利用spfa算法可以高效解决本问题。

首先,利用已有的消防站,计算多源最短路径,储存在集合dist中;

然后,枚举所有顶点,计算单元最短路,存储在集合newd中,则得到新的多元最短路集合S;

他的元素为对应newd与dist元素的最小值,即S = { min(dist(i),newd(i))};

(如果,一个新的消防站可以更新之前消防站的最短路,则这组解一定在这个点的单元最短路中)

最后,计算出每种情况的最远距离,去最小值即可。

说明:注意数据读入格式,有点恶心。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

//link_define
typedef struct enode
{
	int 	point;
	int 	length;
	enode*	next;
}edge;
edge *H[505];
edge  E[100001];

int  e_count;

void link_initial()
{
	e_count = 0;
	memset(H, 0, sizeof(H));
	memset(E, 0, sizeof(E));
}

void link_add(int a, int b, int c)
{
	E[e_count].point = b;
	E[e_count].length = c;
	E[e_count].next = H[a];
	H[a] = &E[e_count ++];
}
//link_end

int  fire[505],dist[505],newd[505];

int  visit[505],stack[505];
void spfa(int s, int *path)
{
	int save = 0;
	stack[save ++] = s;
	visit[s] = 1;
	while (save) {
		int now = stack[-- save];
		for (edge *p = H[now] ; p ; p = p->next)
			if (path[p->point] > path[now] + p->length) {
				path[p->point] = path[now] + p->length;
				if (!visit[p->point]) {
					visit[p->point] = 1;
					stack[save ++] = p->point;
				}
			}
		visit[now] = 0;
	}
}

int main()
{
	int  t,n,m,a,b,c;
	char buf[255];
	while (~scanf("%d",&t))
	while (t --) {
		scanf("%d%d",&n,&m);
		getchar();
		for (int i = 1 ; i <= n ; ++ i) {
			scanf("%d",&fire[i]);
			getchar();
		}

		link_initial();
		while (gets(buf) && strlen(buf)) {
			sscanf(buf, "%d%d%d",&a,&b,&c);
			link_add(a, b, c);
			link_add(b, a ,c);
		}

		//处理原有的消防站
		for (int i = 1 ; i <= m ; ++ i) {
			dist[i] = 0x7ffffff;
			visit[i] = 0;
		}
		for (int i = 1 ; i <= n ; ++ i) {
			dist[fire[i]] = 0;
			spfa(fire[i], dist);
		}

		int Min = 0x7ffffff,Spa = 1;
		for (int i = 1 ; i <= m ; ++ i) {
			for (int j = 1 ; j <= m ; ++ j) {
				newd[j] = 0x7ffffff;
				visit[j] = 0;
			}
			newd[i] = 0;
			spfa(i, newd);
			int Max = 0;
			for (int j = 1 ; j <= m ; ++ j)
				Max = max(Max, min(dist[j], newd[j]));
			if (Max < Min) {
				Min = Max;
				Spa = i;
			}
		}

		printf("%d\n",Spa);
		if (t) printf("\n");
	}
	return 0;
}
时间: 2024-11-08 12:56:12

UVa 10278 - Fire Station的相关文章

uva Fire Station(FLODY+枚举)(挺不错的简单题)

消防站 题目链接:Click Here~ 题意分析: 就是给你f个消防站,n个路口.要你求出在已有消防站的基础上在n个路口的哪个路口上在建立一个消防站,使得n个路口的到离自己最近的消防站最近的距离中最大的一个值最小.即:求n个最近路口中最大的一个,使其改最大值最小.详细的要求自己看题目吧~ 算法分析: 因为,是n个路口到每个消防站的距离.所以,我们可以想到先用一次Flody算法.把每两点的最近距离给算出来.之后在枚举N个路口,进行判断比较得出答案. #include <iostream> #i

uva 11624 Fire!(多源BFS)

uva 11624 Fire! 题目大意:J在迷宫里工作,有一天迷宫起火了,火源有多处.每过一秒,火源都会向四个方向蔓延,J也会向四个方向移动,问J能不能跑出去,能的话输出他跑出去的最短时间,否则输出"IMPOSSIBLE" 解题思路:先进行一次BFS,找出每一点火焰蔓延到该处的最短时间.然后根据这张"火势图",对J的行进路线进行BFS.注意J在边缘的时候,以及没有火源的时候. #include <cstdio> #include <cstring

UVa 11624 Fire!(着火了!)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New Roman"; font-size: 10.5000pt } h3 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 1

BFS(两点搜索) UVA 11624 Fire!

题目传送门 1 /* 2 BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-4 8:11:54 7 File Name :UVA_11624.cpp 8 ****************************************

poj 2607 Fire Station (spfa)

Fire Station Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3783   Accepted: 1337 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 to

UVA 11624 - Fire!(BFS)

UVA 11624 - Fire! 题目链接 题意:一个迷宫,一些格子着火了,火每秒向周围蔓延,现在J在一个位置,问他能走出迷宫的最小距离 思路:BFS2次,第一次预处理每个位置着火时间,第二次根据这个再BFS一次 代码: #include <cstdio> #include <cstring> #include <queue> using namespace std; const int d[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; co

Nyoj Fire Station

描述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

UVA 11642 Fire!

Fire! Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 11624 64-bit integer IO format: %lld      Java class name: Main Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the own

(简单) UVA 11624 Fire! ,BFS。

Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe's location in the maze and which squares of the maze are o