PAT 甲级 1072 Gas Station

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 (≤10​3​​ ), the total number of houses; M (≤10), the total number of the candidate locations for the gas stations; K (≤10​4​​ ), the number of roads connecting the houses and the gas stations; and D​S​​ , 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

解题思路:本题意思是给出居民区和加油站位置,以及他们之间的距离,判断哪个加油站为最优位置,要求加油站距离每个居民区距离不超过D,同时要求加油站离最近居民区的距离越远越好,若此距离相同,选取到每个居民区平均距离最短的加油站,本题以每个加油站为起点进行Dijkstra算法,找出最优位置,注意加油站都是‘G’开头所以以此来区分是否为加油站,加油站的标号为居民区数量N+G后面的编号,本题代码较长,在选取最优位置时判断比较复杂。

#include<cstdio>
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
struct node {
	int v;
	int dis;
};
int N, M, K, D;
const int INF = 1000000000;
const int MAXV = 2000;
vector<node> Adj[MAXV];
int change(string s) {
	int ans = 0;
	if (s[0] == 'G') {
		for (int i = 1; i < s.size(); i++) {
			ans = ans * 10 + (s[i] - '0');
		}
		ans = ans + N;
	}
	else {
		for (int i = 0; i < s.size(); i++) {
			ans = ans * 10 + (s[i] - '0');
		}
	}
	return ans;
}
int d[MAXV];
int min_to_home;
int vis[MAXV] = { 0 };
void Dijkstra(int s) {
	min_to_home = INF;
	int ans = 0;
	fill(d, d + MAXV, INF);
	d[s] = 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] == 0 && d[j] < MIN) {
				MIN = d[j];
				u = j;
			}
		}
		if (u == -1) return;
		vis[u] = 1;
		for (int j = 0; j < Adj[u].size(); j++) {
			node v = Adj[u][j];
			if (d[u] + v.dis < d[v.v]) {
				d[v.v] = d[u] + v.dis;
			}
		}
	}
}

int main(void) {
	scanf("%d %d %d %d", &N, &M, &K, &D);
	for (int i = 0; i < K; i++) {
		int distance, P1, P2;
		string a, b;
		cin >> a >> b >> distance;
		P1 = change(a);
		P2 = change(b);
		node Node;
		Node.v = P2;
		Node.dis = distance;
		Adj[P1].push_back(Node);
		Node.v = P1;
		Node.dis = distance;
		Adj[P2].push_back(Node);
	}
	int mindis = -1, ans, anssum;
	for (int i = N + 1; i <= N + M; i++) {
		fill(vis, vis + MAXV, 0);
		Dijkstra(i);
		int j, tempmin = INF, tempsum = 0;
		for (j = 1; j <= N; j++) {
			if (d[j] > D)
				break;
			else if (d[j] < tempmin)
				tempmin = d[j];
			tempsum += d[j];
		}
		if (j > N && tempmin > mindis) {
			mindis = tempmin;
			ans = i;
			anssum = tempsum;
		}
		else if (j > N && tempmin == mindis && tempsum < anssum) {
			mindis = tempmin;
			ans = i;
			anssum = tempsum;
		}
	}
	if (mindis == -1) {
		printf("No Solution");
		return 0;
	}
	printf("G%d\n", ans - N);
	printf("%.1f %.1f", mindis*1.0, anssum*1.0 / N );
	return 0;
}
发布了69 篇原创文章 · 获赞 5 · 访问量 2037

猜你喜欢

转载自blog.csdn.net/lovingcgq/article/details/104722147