Not Only Algorithm,不仅仅是算法,关注数学、算法、数据结构、程序员笔试面试以及一切涉及计算机编程之美的内容 。。
你的位置:NoAlGo博客 » 题解 » 

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 (<= 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

解答

#include <cstdio>
#include <algorithm>
using namespace std;

const int mxsz = 1000 + 100, oo = 2100000000;
int n, m, k, ds; 
int g[mxsz][mxsz], dis[mxsz], vs[mxsz];
int mnid, mn = -1; //最短距离最大的候选地,对应的最短距离
double avg = oo;   //对应的平均距离

void dijkstra(int src)
{
	for (int i = 1; i <= n + m; i++) dis[i] = g[src][i] ? g[src][i] : oo, vs[i] = 0;
	dis[src] = 0, vs[src] = 1;
	for (int i = 1; i < n + m; i++)
	{
		int mndis = oo, id;
		for (int j = 1; j <= n + m; j++)
			if (!vs[j] && dis[j] < mndis) mndis = dis[j], id = j;

		vs[id] = 1;
		for (int j = 1; j <= n + m; j++)
			if (!vs[j] && g[id][j] && dis[id] + g[id][j] < dis[j])
				dis[j] = dis[id] + g[id][j];
	}

	int tmn = oo; double tavg = 0;
	for (int j = 1; j <= n; j++)
	{
		if (dis[j] > ds) return;
		tavg += dis[j], tmn = min(tmn, dis[j]);
	}
	tavg /= n;
	if (tmn > mn || (tmn == mn && tavg < avg))
		mn = tmn, avg = tavg, mnid = src;
}

inline int change (char *p)
{
	int ans, tem = 0;
	if (*p == 'G') tem = n, p++;
	sscanf(p, "%d", &ans);
	return ans + tem;
}

int main()
{
	scanf("%d%d%d%d", &n, &m, &k, &ds);
	for (int i = 0; i < k; i++)
	{
		char t1[10], t2[10]; int t; scanf("%s%s%d", t1, t2, &t);
		int n1 = change(t1), n2 = change(t2); 
		g[n1][n2] = g[n2][n1] = t;
	}
	for (int i = n + 1; i <= n + m; i++) dijkstra(i);			

	if (mn == -1) printf("No Solution\n");
	else printf("G%d\n%.1lf %.1lf\n", mnid-n, double(mn), avg);
	return 0;
}
上一篇: 下一篇:

我的博客

NoAlGo头像编程这件小事牵扯到太多的知识,很容易知其然而不知其所以然,但真正了不起的程序员对自己程序的每一个字节都了如指掌,要立足基础理论,努力提升自我的专业修养。

站内搜索

最新评论