[PAT A1072]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.
输入格式
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 G
M.
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.
输出格式
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
.
输入样例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
输出样例1
G1
2.0 3.3
输入样例2
2 1 2 10
1 G1 9
2 G1 20
输出样例2
No Solution
解析
1.题目读起来确实很难读懂,说实话,第一遍看得时候不知所云,出于理解问题,写代码往错的方向走了,后面才改正。
2.题目意思是,给出n,m,k和dis,n表示房屋数,m表示加油站的数,k表示边数,dis表示加油站最远供给距离。现在要求从m个加油站站点中选出一个来,使得所有房屋离它尽可能远(怕爆炸!这里算数理解为:所有房屋里面,离它最近的房屋都要尽可能远),如果有两种方案离它最近的房屋有着相同的最小距离,那么谁的平均最短路径小就选谁。
3.注意!!:由于这里M的取值可以取到10哦,所以操作这里最简单的方法还是使用stoi()函数,不易出错。
4.代码如下:
#include<iostream>
#include<string>
using namespace std;
const int maxn = 1020; //最多占用1000+10+1=1011个结点(0号位置不用),这里开到1020稍大一点
const int INF = 1000000000;
int Graph[maxn][maxn];
int d[maxn];
bool visit[maxn];
int n, m, k, dis;
void Dijkstra(int s);
int main()
{fill(Graph[0], Graph[0] + maxn * maxn, INF);scanf("%d%d%d%d", &n, &m, &k, &dis);for (int i = 0; i < k; i++){string s1, s2;int c1, c2;int len;cin >> s1 >> s2 >> len;
//对于加油站1~m,我把它们拼在1~n之后,这样共有n+m个结点要进入Dijkstra算法if (s1[0] == 'G'){ //使用stoi灵活转化,注意M为10(两位数)的情况s1=s1.substr(1);c1=n+stoi(s1);}else c1=stoi(s1);if (s2[0] == 'G'){s2=s2.substr(1);c2=n+stoi(s2);}else c2=stoi(s2);Graph[c1][c2] = Graph[c2][c1] = len;}int maxL = -1, maxx = -1;int min_len = -1;for (int i = n + 1; i <= n + m; i++) { Dijkstra(i); //对于每个加油站,使用Dijkstra算法bool ok = true; //判断当前案例是否合法int sum = 0, min_dist = INF; //记录路径总和,房屋中到加油站最近的房屋距离for (int j = 1; j <= n; j++) {sum += d[j];if (d[j] > dis) { //如果某房子得不到供给,退出,并标记不合法ok = false;break;}if (d[j] < min_dist) min_dist = d[j];}if (ok) { //如果合法if (min_dist > min_len) { //最近的房屋到加油站的距离比原先最短的要长(更不容易发生危险)maxL = sum;maxx = i - n;min_len = min_dist;}else if (min_dist == min_len && sum < maxL) { //一样长的情况,summaxL = sum;maxx = i - n;}}}if (maxx != -1) {printf("G%d\n", maxx);printf("%.1f %.1f", min_len*1., maxL*1. / n);}else printf("No Solution");return 0;
}
void Dijkstra(int s) { //Dijkstra算法,这里不再赘述fill(d, d + maxn, INF);fill(visit, visit + maxn, 0);d[s] = 0;for (int i = 0; i < m + n; i++) {int u = -1, min = INF;for (int j = 1; j <= m + n; j++) {if (visit[j] == false && d[j] < min) {u = j;min = d[j];}}if (u == -1) return;visit[u] = true;for (int v = 1; v <= m + n; v++) {if (visit[v] == false && Graph[u][v] != INF) {if (Graph[u][v] + d[u] < d[v]) {d[v] = Graph[u][v] + d[u];}}}}
}