PAT 甲级 1072. Gas Station (30)
来源:互联网 发布:淘宝xbox360手柄正品 编辑:程序博客网 时间:2024/05/16 07:40
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 (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 104), the number of roads connecting the houses and the gas stations; and DS, 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 51 2 21 4 21 G1 41 G2 32 3 22 G2 13 4 23 G3 24 G1 3G2 G1 1G3 G2 2Sample Output 1:
G12.0 3.3Sample Input 2:
2 1 2 101 G1 92 G1 20Sample Output 2:
No Solution
#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <cctype>#include <map>#include <cstdio>using namespace std;const int maxv = 1020;const int inf = 100000000;int n, m, k, ds, g[maxv][maxv];int d[maxv];bool vis[maxv] = { false };void Dijkstra(int s) {memset(vis, false, sizeof(vis));fill(d, d + maxv, inf);d[s] = 0;for (int i = 0; i < n + m; i++) {int u = -1, min = inf;for (int j = 1; j <= n + m; j++) {if (vis[j] == false && d[j] < min) {u = j;min = d[j];}}if (u == -1) return;vis[u] = true;for (int v = 1; v <= n + m; v++) {if (vis[v] == false && g[u][v] != inf) {if (d[u] + g[u][v] < d[v])d[v] = d[u] + g[u][v];}}}}int getID(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 n + ID;else return ID;}int main() {scanf("%d%d%d%d", &n, &m, &k, &ds);int u, v, w;char city1[5], city2[5];fill(g[0], g[0] + maxv*maxv, inf);for (int i = 0; i < k; i++) {scanf("%s %s %d", city1, city2, &w);u = getID(city1);v = getID(city2);g[u][v] = g[v][u] = w;}double ansDis = -1, ansAvg = inf;int ansID = -1;for (int i = n + 1; i <= m + n; i++) {double minDis = inf, avg = 0;Dijkstra(i);for (int j = 1; j <= n; j++) {if (d[j] > ds) {minDis = -1;break;}if (d[j] < minDis) minDis = d[j];avg += 1.0*d[j] / n;}if (minDis == -1) continue;if (minDis > ansDis) {ansID = i;ansDis = minDis;ansAvg = avg;}else if (minDis == ansDis&&avg < ansAvg) {ansID = i;ansAvg = avg;}}if (ansID == -1) printf("No Solution\n");else {printf("G%d\n", ansID - n);printf("%.1f %.1f\n", ansDis, ansAvg);}cin >> n;return 0;}
- PAT甲级练习1072. Gas Station (30)
- 1072. Gas Station (30) PAT 甲级
- PAT甲级1072. Gas Station (30)
- 【PAT甲级】1072. Gas Station (30)
- PAT 甲级 1072. Gas Station (30)
- 1072. Gas Station (30)-PAT甲级真题(Dijkstra)
- PAT 1072. Gas Station (30)
- PAT 1072. Gas Station (30)
- 【PAT】1072. Gas Station (30)
- PAT 1072. Gas Station (30)
- PAT 1072. Gas Station
- PAT 1072. Gas Station
- PAT 1072. Gas Station
- 【PAT】1072. Gas Station
- pat 1072. Gas Station
- PAT A 1072. Gas Station (30)
- PAT-A-1072. Gas Station (30)
- 【PAT】【Advanced Level】1072. Gas Station (30)
- Win10下Docker使用阿里专属加速器
- matlib不同界面之间的数据传递机制
- NWERC 2015B/bzoj 4426 Better Productivity
- 写时拷贝
- JS(8)
- PAT 甲级 1072. Gas Station (30)
- 软件开发的功能性需求和非功能性需求
- 《deep learning》学习笔记(7)——深度学习中的正则化
- POJ 2699 The Maximum Number of Strong Kings(枚举/二分+最大流)
- Hdu4819 Mosaic 二维线段树维护区间最值+单点更新
- 在 MacOS 上简单的使用 GitHub
- USERADD命令详解
- Linux C语言文件读写
- GIt远程代码管理配置