1072 Gas Station (30 分)

it2022-05-06  5

Keywords: Dij, 审题理解

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

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

Note1

题意: 选一个加油站,使得离他最近的住房距离最大,若果有多个这样的加油站,选其中到该加油站平均距离最短的那个,输出离加油站最近的距离和平均距离问题: – 几个最大最小的初始化 – <= max_service_range – G 1 - 10 没能成功录入G10 – 最后一个测试点没过:因为住宅其实也不是只有一位数的。。。

Code1

#include<iostream> #include<vector> #include<fstream> #include<algorithm> #include<cmath> #include<string> using namespace std; const int MAX = 1e4 ; const int INF = 1e9; int StringToInt[MAX], IntToSring[MAX]; int id = 0; int num, gas_num, collected[MAX] = {0}, Distance[MAX], a[MAX][MAX]; void Dijsktra(int start){ //collected[start] = 1; Distance[start] = 0; while(1){ int min = MAX, ptr = 0; for(int i = 1; i <= num + gas_num; i++){ if(collected[i] == 0 && Distance[i] < min){ min = Distance[i]; ptr = i; } } if(min == MAX) break; collected[ptr] = 1; for(int i = 1; i <= num + gas_num; i++){ if(collected[i] == 0 && a[ptr][i] + Distance[ptr] < Distance[i]){ Distance[i] = a[ptr][i] + Distance[ptr]; } } } } int main() { int edge, max_service_range, cnt = 0; cin >> num >> gas_num >> edge >> max_service_range; fill(a[0], a[0] + (num + gas_num+ 1) * MAX, INF); fill(Distance, Distance + gas_num + 1 +num, INF); for(int i =1; i <= edge; i++){ string tempa, tempb; int aa, bb, tempc; cin >> tempa >> tempb >> tempc; if(tempa[0] == 'G'){ aa=stoi(tempa.substr(1))+num; // if(tempa[2] == '0') aa = 10 + num; // else aa = tempa[1] - '0' + num; } else aa = tempa[0] - '0'; if(tempb[0] == 'G'){ bb=stoi(tempb.substr(1))+num; // if(tempb[2] == '0') bb = 10 + num; // else bb = tempb[1] - '0' + num ; } else bb = tempb[0] - '0'; a[aa][bb] = tempc; a[bb][aa] = tempc; } int Max_sum = INF, ptr; //最大的平均距离,目标选择最小的 int Max_to_gs = 0;//一轮中距离加油站最yuan的距离,目标选择最大的 for(int i = 1; i <= gas_num; i++){ double min_dis = INF;//一轮中离加油站最近的距离,目标找最小的 int id = i + num, fflag = 0; int sum = 0; fill(Distance, Distance + num + gas_num + 1, INF); fill(collected, collected + num + gas_num +1, 0); //for(int j = 1; j <= num + gas_num; j++){ // if(a[id][j] < INF) // Distance[j] = a[id][j]; //} Dijsktra(id); for(int k = 1; k <= num; k++){ if(Distance[k] <= max_service_range ){ sum += Distance[k]; if(Distance[k] < min_dis){ min_dis = Distance[k]; } } else{min_dis = INF; fflag = 1;} } if(fflag == 1) continue; if(min_dis > Max_to_gs){ Max_to_gs = min_dis; ptr = id; Max_sum = sum; } else if(min_dis == Max_to_gs) if(sum < Max_sum){ ptr = id; Max_sum = sum; } } if(Max_sum < INF){ printf("G%d\n", ptr - num); printf("%.1lf %.1lf", (double)(Max_to_gs), (double)(Max_sum * 1.0 / num)); } else printf("No Solution"); return 0 ; }

Code2

#include<iostream> #include<vector> #include<fstream> #include<algorithm> #include<cmath> #include<string> using namespace std; const int MAX = 1024 ; const int INF = 1e9; int id = 0; int num, gas_num, collected[MAX] = {0}, Distance[MAX], a[MAX][MAX]; void Dijsktra(int start){ Distance[start] = 0; while(1){ int min = MAX, ptr = 0; for(int i = 1; i <= num + gas_num; i++){ if(collected[i] == 0 && Distance[i] < min){ min = Distance[i]; ptr = i; } } if(min == MAX) break; collected[ptr] = 1; for(int i = 1; i <= num + gas_num; i++){ if(collected[i] == 0 && a[ptr][i] + Distance[ptr] < Distance[i]){ Distance[i] = a[ptr][i] + Distance[ptr]; } } } } int main() { int edge, max_service_range, cnt = 0; cin >> num >> gas_num >> edge >> max_service_range; fill(a[0], a[0] + (num + gas_num+ 1) * MAX, INF); fill(Distance, Distance + gas_num + 1 +num, INF); for(int i =1; i <= edge; i++){ string tempa, tempb; int aa, bb, tempc; cin >> tempa >> tempb >> tempc; if(tempa[0] == 'G'){ aa=stoi(tempa.substr(1))+num; } else aa = stoi(tempa); if(tempb[0] == 'G'){ bb=stoi(tempb.substr(1))+num; } else bb = stoi(tempb); a[aa][bb] = tempc; a[bb][aa] = tempc; } int Max_sum = INF, ptr; //最大的平均距离,目标选择最小的 int Max_to_gs = 0;//一轮中距离加油站最yuan的距离,目标选择最大的 for(int i = 1; i <= gas_num; i++){ double min_dis = INF;//一轮中离加油站最近的距离,目标找最小的 int id = i + num, fflag = 0; int sum = 0; fill(Distance, Distance + num + gas_num + 1, INF); fill(collected, collected + num + gas_num +1, 0); Dijsktra(id); for(int k = 1; k <= num; k++){ if(Distance[k] <= max_service_range ){ sum += Distance[k]; if(Distance[k] < min_dis){ min_dis = Distance[k]; } } else{fflag = 1; break;} } if(fflag == 1) continue; if(min_dis > Max_to_gs){ Max_to_gs = min_dis; ptr = id; Max_sum = sum; } else if(min_dis == Max_to_gs) if(sum < Max_sum){ ptr = id; Max_sum = sum; } } if(Max_sum < INF){ printf("G%d\n", ptr - num); printf("%.1lf %.1lf", (double)(Max_to_gs), (double)(Max_sum * 1.0 / num)); } else printf("No Solution"); return 0 ; }

最新回复(0)