(PAT 1072) Gas Station (Dijkstra算法)

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.首先要注意加油站的输入,表示为GM,如果1-N表示住宅,那么N-M可以表示加油站,我们通过字符串输入,并利用转化函数将字符串转化为1-M中的结点

int getIndex(char s[]) {
	int num = 0;
	int itr = 1;
	for (int i = strlen(s) - 1; i >= 0; --i) {
		num += itr * (s[i] - '0');
		itr *= 10;
	}
	return num;
}

如果输入的是G开头的话,判断位数,位数为2位的话就是N+char[1]-'0',3位的话是N+10

2,将每个加油站结点通过迪杰斯特拉算法求得与每个住宅的最短距离,得到最小值,注意求每个加油站时要清空visited数组和d数组

3.最后保存到vector中,根据题意排序

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
const int MAXV = 1050;
const int INF = 0x3fffffff;
int GasMap[MAXV][MAXV];
bool GVisited[MAXV] = { false };
int Gd[MAXV];
int N, M, K, DS;
//参数
struct soluStation {
	int ID;
	double Dis, aveDis;
};
vector<soluStation> reses;
bool cmp(soluStation a, soluStation b) {
	if (a.Dis != b.Dis) { return a.Dis > b.Dis; }
	else {
		if (a.aveDis != b.aveDis) { return a.aveDis < b.aveDis; }
		else {
			return a.ID < b.ID;
		}
	}
}
double tempMMin = INF;
//加油站最短路处理
void GDijkstraGas(int s) {
	fill(Gd, Gd + MAXV, INF);  //切记一定要初始化
	Gd[s] = 0;
	for (int i = 1; i <= M + N; ++i) {
		int u = -1, MMIN = INF;
		for (int j = 1; j <= M + N; ++j) {
			if (!GVisited[j] && Gd[j] < MMIN) {
				MMIN = Gd[j];
				u = j;
			}
		}
		if (u == -1) return;
		GVisited[u] = true;
		for (int v = 1; v <= M + N; ++v) {
			if (!GVisited[v] && GasMap[u][v] != INF && GasMap[u][v] + Gd[u] < Gd[v]) {
				Gd[v] = GasMap[u][v] + Gd[u];
				if (v <= N && Gd[v] < tempMMin) {
					tempMMin = Gd[v];
				}
			}
		}
	}
}
int getIndex(char s[]) {
	int num = 0;
	int itr = 1;
	for (int i = strlen(s) - 1; i >= 0; --i) {
		num += itr * (s[i] - '0');
		itr *= 10;
	}
	return num;
}
int main() {
	fill(GasMap[0], GasMap[0] + MAXV * MAXV, INF);
	scanf("%d %d %d %d", &N, &M, &K, &DS);
	char city1[1010], city2[1010];
	int Glength;
	int node1, node2;
	for (int i = 0; i < K; ++i) {
		scanf("%s %s %d", city1, city2, &Glength);
		if (city1[0] == 'G') {
			if (strlen(city1) == 3) { node1 = 10 + N; }
			else node1 = city1[1] - '0' + N;
		}
		else {
			node1 = getIndex(city1);
		}
		if (city2[0] == 'G') {
			if (strlen(city2) == 3) { node2 = 10 + N; }
			else node2 = city2[1] - '0' + N;
		}
		else {
			node2 = getIndex(city2);
		}
		GasMap[node1][node2] = Glength;
		GasMap[node2][node1] = Glength;
	}
	//处理加油站
	for (int i = 1; i <= M; ++i) {
		bool outrange = false;
		double avedistemp = 0.0;
		tempMMin = INF;
		fill(GVisited, GVisited + MAXV, false);
		GDijkstraGas(N + i); //求最短路
		for (int j = 1; j <= N; ++j) {
			if (Gd[j] > DS) {
				outrange = true;
				break;
			}
			avedistemp += Gd[j];
		}
		if (!outrange) {
			soluStation tempsolu;
			tempsolu.ID = i;
			tempsolu.Dis = tempMMin;
			tempsolu.aveDis = avedistemp / N;
			reses.push_back(tempsolu);
		}
	}
	if (reses.empty()) {
		printf("No Solution\n");
	}
	else {
		sort(reses.begin(), reses.end(), cmp);
		printf("G%d\n%.1lf %.1lf", reses[0].ID, reses[0].Dis, reses[0].aveDis);
	}
	system("PAUSE");
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值