1. 程式人生 > >程式設計基礎31 tips 圖的最短路徑(三)

程式設計基礎31 tips 圖的最短路徑(三)

1072 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 G

1 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,像這種有字母也有數字的字串,一定不要忘了不要只取一位數字,也可能有兩位數字!!!本題最後一個點就是多位數字。

2,最後想把所有備用點弄成一個結構體,儲存最短距離,平均距離,編號,然後sort排序,取第一個。其實不必這樣,因為只需要取最值,設三個變數一個個比較儲存最值即可。

二,我的程式碼

#include<cstdio>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int max_n = 1100;
const int INF = 1000000000;
int N = 0;
int M = 0;
int D = 0;
double dis[max_n] = { 0 };
bool vis[max_n] = { false };
struct Node {
	int v;
	int dis;
	Node(int _v, int _dis) :v(_v), dis(_dis) {};
};
vector<Node> map[max_n];
int judge(char str[]) {
	int i = 0, len = strlen(str), id = 0;	
	while (i<len) { 
		if (str[i] != 'G')id = id * 10 + str[i] - '0';		
		i++; 
	}	
	if (str[0] == 'G')return id + N;
	else return id; 
}
void Dijsktra(int s) {
	fill(dis, dis + max_n, INF);
	fill(vis, vis + max_n, false);
	dis[s] = 0;
	for (int i = 1; i <= N+M; i++) {
		double MIN = INF;
		int u = -1;
		for (int j = 1; j <= N+M; j++) {
			if (vis[j] == false && dis[j] < MIN) {
				u = j;
				MIN = dis[j];
			}
		}
		if (u == -1)return;
		vis[u] = true;
		for (int j = 0; j < map[u].size(); j++) {
			int v = map[u][j].v;
			int d = map[u][j].dis;
			if (vis[v] == false && dis[u] + d < dis[v]) {
				dis[v] = dis[u] + d;
			}
		}
	}
}
double find_min(double arr[]) {
	double min_1 = INF;
	for (int i = 1; i <= N; i++) {
		if (min_1 > arr[i])
			min_1 = arr[i];
	}
	return min_1;
}
double find_ave(double arr[]) {
	double sum = 0;
	for (int i = 1; i<=N; i++) {
		sum += arr[i];
	}
	return sum / N;
}
int main() {
	int K = 0;
	char str1[4], str2[4];
	int x = 0, y = 0, z = 0;
	scanf("%d %d %d %d", &N, &M, &K, &D);
	for (int i = 0; i < K; i++) {
		scanf("%s", str1);
		scanf("%s", str2);
		scanf("%d", &z);
		x = judge(str1);
		y = judge(str2);
		map[x].push_back(Node(y, z));
		map[y].push_back(Node(x, z));
	}
	double real_min = -1, real_ave = INF;
	int id = 0;
	for (int i = N+1; i <= M+N; i++) {
		int ds = 0;
		Dijsktra(i);
		for (int j = 1; j <= N; j++) {
			if (dis[j]>D) {
				ds = -1;
				break;
			}
		}
		if (ds == -1)continue;
		double min_1 = find_min(dis);
		double ave_1 = find_ave(dis);
		if (min_1 > real_min || min_1 == real_min&&ave_1 < real_ave) {
			real_min = min_1;
			real_ave = ave_1;
			id = i - N;
		}
	}
	if (id == 0) {
		printf("No Solution\n");
	}
	else {
		printf("G%d\n", id);
		printf("%.1f %.1f", real_min, real_ave);
	}
	return 0;
}