PAT 甲级 A1072 Gas Station

Author CHEN, Yue

Organization 浙江大学

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.

Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive integers: N (≤103), the total number of houses; M (≤10), the total number of the candidate locations for the gas stations; K (≤104), the number of roads connecting the houses and the gas stations; and DS​, the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N, and all the candidate locations are numbered from G1 to GM.

Then K lines follow, each describes a road in the format

P1 P2 Dist

where P1 and P2 are the two ends of a road which can be either house numbers or gas station numbers, and Dist is the integer length of the road.

Output Specification:

For each test case, print in the first line the index number of the best location. In the next line, print the minimum and the average distances between the solution and all the houses. The numbers in a line must be separated by a space and be accurate up to 1 decimal place. If the solution does not exist, simply output No Solution.

Sample Input 1:

4 3 11 5
1 2 2
1 4 2
1 G1 4
1 G2 3
2 3 2
2 G2 1
3 4 2
3 G3 2
4 G1 3
G2 G1 1
G3 G2 2

Sample Output 1:

G1
2.0 3.3

Sample Input 2:

2 1 2 10
1 G1 9
2 G1 20

Sample Output 2:

No Solution

题意:

有n个居民楼,m个加油站,k条边(是无向图),DS是服务距离,然后要找出在所有加油站中,找出一个加油站,该加油站离居民楼尽可能地远(相对其他加油站),但是该距离又是和居民楼的最短距离设作dis,如果有多个加油站的dis相同,就比较平均距离,平均路径是该加油站到所有居民楼的最短路径之和的平均值,选最小的平均距离的加油站。

思路:
所有加油站使用dijstra算法找到居民楼的最短距离,找到最短距离中的最短距离,和其他的加油站的最短距离中的最短距离比较,找出最短距离中的最短距离的最长距离,这个就是dis,如果相等就比较平均距离。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1020;
const int INF = 1 << 30 - 1;
int G[maxn][maxn];
int d[maxn];
bool vis[maxn] = {false};
int n, m, k, DS;

void Dijkstra(int st) {
	fill(d, d + maxn, INF);
	memset(vis, false, sizeof(vis)); 
	d[st] = 0;
	for(int i = 0; i < n + m; i++) {//单源最短路径,总顶点数要明确; 
		int u = -1, MIN = INF;
		for(int j = 1; j <= n + m; j++) {
			if(vis[j] == false && d[j] < MIN) {
				u = j;
				MIN = d[j];
			}
		}
		if(u == -1) return ;
		vis[u] = true;
		for(int v = 1; v <= n + m; v++) {
			if(vis[v] == false && G[u][v] != INF) {
				if(d[u] + G[u][v] < d[v]) {
					d[v] = d[u] + G[u][v];
				}
			}
		}
	}
}

int getID(char s[]) {//给顶点编号; 
	int len = strlen(s);
	int id = 0;
	int i = 0;
	//字符串转数字; 
	while(i < len) {
		if(s[i] != 'G') {
			id = id * 10 + (s[i] - '0');
		}
		i++;
	}
	if(s[0] == 'G') return n + id;
	else {
		return id;
	}
}


int main () {
	scanf("%d %d %d %d", &n, &m, &k, &DS);
	//居民楼个数,加油站个数,无向图的边数,服务范围; 
	fill(G[0], G[0] + maxn * maxn, INF);
	for(int i = 0; i < k; i++) {
		char city1[5], city2[5];
		int w;
		scanf("%s %s %d", &city1, &city2, &w);
		int u = getID(city1);
		int v = getID(city2);
		G[u][v] = G[v][u] = w;
	}//建图;
	
	
	double ansDis = -1, ansAvg = INF;//1,相对加油站中,离居民楼最远 2.相对居民楼,加油站和居民楼最近 
	int ansID = -1;//赋值判断是否有解; 
	for(int i = n + 1; i <= n + m; i++) {//遍历所有加油站; 
		Dijkstra(i);
		double mindis = INF;//单源最短距离;
		double avg = 0; 
		
		//遍历该加油站的最短距离; 
		for(int j = 1; j <= n; j++) {//只需要遍历到居民楼的距离; 
			if(d[j] > DS) {//超过服务距离,该服务站不合格; 
				mindis = -1;
				break; 
			}
			if(d[j] < mindis) {
				mindis = d[j];
			}
				avg += 1.0 * d[j] / n; 
		}//找出最短距离和平均距离;
		
		if(mindis == -1) continue; 
		if(mindis > ansDis) {
			ansDis = mindis; 
			ansID = i;
			ansAvg = avg;
		}else if(mindis == ansDis && avg < ansAvg) {
			ansID = i;
			ansAvg = avg;
		}  
	}
	
	
	if(ansID == -1) {
		printf("No Solution");
	}else {
		printf("G%d\n", ansID - n);
		printf("%.1f %.1f", ansDis, ansAvg);		
	}
	return 0; 
}

注意:
测试点1,哪里写的是3.3,其实输出的是3.2也是对的(平均距离是3.25,保留一位就是3.2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值