PAT-A-1072. Gas Station (30)
来源:互联网 发布:中国网络电视在线直播 编辑:程序博客网 时间:2024/06/05 07:08
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 (<= 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<cstdio>#include<algorithm>#include<string.h>#include<stdio.h>using namespace std;const int maxn = 1100;const int inf = 100000000;int vis[maxn];int g[maxn][maxn];int d[maxn];int n, m, k, range;void dijkstra(int s){ memset(vis, false, sizeof(vis)); fill(d, d + maxn, inf); d[s] = 0; for (int i = 1; i <= n + m; i++) { int min = inf; int u = -1; 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++) { int len = g[u][v]; if (vis[v] == false && len != inf) { if (d[u] + len < d[v]) { d[v] = d[u] + len; } } } }}int getid(char c[]){ int len = strlen(c); int i = 0; int id = 0; while (i < len) { if (c[i] != 'G') id = id * 10 + (c[i] - '0'); i++; } if (c[0] == 'G') return n + id; else return id;}int main(){ cin >> n >> m >> k >> range; char c1[10], c2[10]; fill(g[0], g[0] + maxn*maxn, inf); for (int i = 0; i < k; i++) { cin >> c1 >> c2; int id1 = getid(c1); int id2 = getid(c2); cin >> g[id1][id2]; g[id2][id1] = g[id1][id2]; } double ansdis = -1; double ansavg = inf; int ansid = -1; for (int i = n + 1; i <= n + m; i++) { double mindis = inf, avg = 0; dijkstra(i); for (int j = 1; j <= n; j++) { if (d[j]>range) { mindis = -1; break; } if (d[j] < mindis) { mindis = d[j]; } avg += 1.0*d[j]/n; } if (mindis == -1) continue; if (mindis>ansdis) { ansdis = mindis; ansid = i; ansavg = avg; } else if (mindis == ansdis&&avg < ansavg) { ansid = i; ansavg = avg; } } if (ansid == -1) cout << "No Solution" << endl; else { cout << "G" << ansid-n << endl; printf("%.1f %.1f", ansdis, ansavg); } system("pause"); return 0;}
- PAT A 1072. Gas Station (30)
- PAT-A-1072. Gas Station (30)
- Pat(A) 1072. Gas Station (30)
- 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甲级练习1072. Gas Station (30)
- 1072. Gas Station (30) PAT 甲级
- PAT甲级1072. Gas Station (30)
- 【PAT甲级】1072. Gas Station (30)
- 【PAT】【Advanced Level】1072. Gas Station (30)
- PAT 甲级 1072. Gas Station (30)
- 直接修改dex破解
- MapReduce的Shuffle原理
- MySQL--操作数据表1:插入记录
- C:\Users\Administrator>net use \\dragon-nas\gyn私有云 "881017" /user:"dragon" /p ersistent:yes
- Wait与Notify
- PAT-A-1072. Gas Station (30)
- js_return 小应用
- jstl 细节操作
- 简易MySQL命令行命令
- [LeetCode] 416. Partition Equal Subset Sum
- Java反射基础学习
- 前后端项目的糅合模式mashup
- DevOps概述
- jvm调优