PAT A1072 Gas Station (30 分)

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

实现思路:

一道最短路径问题,要求找到一个加油站,既可以满足所有住户都在加油站的可加油范围内,并且为了安全,要在加油站可以服务到所有住户的基础上,让所有住户离加油站的最短距离尽可能的远,所以本题就是先求出所有的最短路径,然后在最短路径的基础上筛选出所有住户距离加油站距离最小的那一条路径即可。
细节点:
1.住户可以通过其他加油站来抵达目的加油站,所以最短路径中是n+m个结点
2.服务距离等数据用double存储。

AC代码:

#include <iostream>
#include <cstring>
using namespace std;
const int MAXN=1020;
const int INF=0x7fffffff;
int n,m,k,maxDis,G[MAXN][MAXN],d[MAXN],vist[MAXN];

void Dijkstra(int s) {
	fill(d,d+MAXN,INF);
	d[s]=0;
	for(int i=1; i<=n+m; i++) {
		int u=-1,MIN=INF;
		for(int j=1; j<=n+m; j++) {
			if(!vist[j]&&d[j]<MIN) {
				MIN=d[j];
				u=j;
			}
		}
		if(u==-1) return;
		vist[u]=true;
		for(int v=1; v<=n+m; v++) {
			if(!vist[v]&&G[u][v]!=INF) {
				if(d[u]+G[u][v]<d[v]) d[v]=d[u]+G[u][v];
			}
		}
	}
}

int main() {
	//题目要求 加油站到达居民楼的最小距离尽可能远又不能超出服务范围
	fill(G[0],G[0]+MAXN*MAXN,INF);
	cin>>n>>m>>k>>maxDis;
	string a,b;
	int dis,u,v;
	for(int i=0; i<k; i++) {
		u=v=0;
		cin>>a>>b>>dis;
		if(a[0]=='G') {
			a.erase(a.begin());
			u+=n;//单独设置加油站结点 
		}
		if(b[0]=='G') {
			b.erase(b.begin());
			v+=n;
		}
		u+=stoi(a);
		v+=stoi(b);
		G[u][v]=G[v][u]=dis;
	}
	int ansIndex;
	double ansAvg=INF,ansMinDis=-1;
	for(int i=n+1; i<=n+m; i++) {
		memset(vist,false,sizeof(vist));
		Dijkstra(i);
		double tempMinDis=INF,tempAvg=0;
		bool flag=true;
		for(int j=1; j<=n; j++) {
			if(d[j]>maxDis) {//找到一个能够服务所有住户的加油站路径了 
				flag=false;
				break;
			}
			if(d[j]<tempMinDis) tempMinDis=d[j];
			tempAvg+=d[j]*1.0/n;
		}
		if(flag) { //加油站符合服务范围要求
			if(tempMinDis>ansMinDis) {
				ansMinDis=tempMinDis;
				ansAvg=tempAvg;
				ansIndex=i;
			} else if(tempMinDis==ansMinDis&&tempAvg<ansAvg) {
				ansAvg=tempAvg;
				ansIndex=i;
			}
		}
	}
	if(ansMinDis==-1) printf("No Solution");
	else {
		printf("G");
		ansIndex-=n;
		cout<<ansIndex<<endl;
		printf("%.1f %.1f",ansMinDis,ansAvg);
	}
	return 0;
}
上一篇:1473. 粉刷房子 III(三维动态规划)


下一篇:LeetCode 1478.安排邮筒 (1、滑动窗口)