#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
const int MAXN = 1011;
const int INF = 999999999;
struct node{
int id;
int dist;
node(int _id, int _dist): id(_id), dist(_dist){}
};
struct station{
std::string id;
double minDist;
double avgDist;
station(std::string _id, double _minDist, double _avgDist):
id(_id), minDist(_minDist), avgDist(_avgDist){}
};
int N, M, K, ds, dist, m, n;
std::string s, a, b;
std::map<std::string, int> mp;
std::vector<std::vector<node>> adj;
std::vector<station> ans;
void dijkstra(int k){
bool visited[MAXN];
int d[MAXN];
int pivot, minD, totalDist;
std::fill(visited, visited + N + M + 1, false);
std::fill(d, d + M + N + 1, INF);
d[k] = 0;
for(int i = 0; i < M + N; ++i){
pivot = -1;
minD = INF;
for(int j = 1; j <= M + N; ++j){
if(!visited[j] && d[j] < minD){
minD = d[j];
pivot = j;
}
}
if(pivot == -1){
break;
}
visited[pivot] = true;
for(int j = 0; j < adj[pivot].size(); ++j){
if(!visited[adj[pivot][j].id] && d[pivot] + adj[pivot][j].dist < d[adj[pivot][j].id]){
d[adj[pivot][j].id] = d[pivot] + adj[pivot][j].dist;
}
}
}
minD = INF;
totalDist = 0;
for(int i = 1; i <= N; ++i){
if(d[i] > ds){
return;
}
if(d[i] < minD){
minD = d[i];
}
totalDist += d[i];
}
std::string str = "G" + std::to_string(k - N);
ans.push_back(station(str, minD * 1.0, totalDist * 1.0 / N));
}
bool cmp(station &a, station &b){
return a.minDist != b.minDist ? a.minDist > b.minDist : (a.avgDist != b.avgDist ? a.avgDist < b.avgDist : a.id < b.id);
}
int main(){
scanf("%d %d %d %d", &N, &M, &K, &ds);
for(int i = 1; i <= M; ++i){
s = "G" + std::to_string(i);
mp[s] = N + i;
}
adj.resize(N + M + 1);
for(int i = 0; i < K; ++i){
std::cin >> a >> b >> dist;
if(mp[a] == 0){
m = std::stoi(a);
} else{
m = mp[a];
}
if(mp[b] == 0){
n = std::stoi(b);
} else{
n = mp[b];
}
adj[m].push_back(node(n, dist));
adj[n].push_back(node(m, dist));
}
for(int i = N + 1; i <= N + M; ++i){
dijkstra(i);
}
if(ans.empty()){
printf("No Solution");
return 0;
}
sort(ans.begin(), ans.end(), cmp);
printf("%s\n", ans[0].id.c_str());
printf("%.1f %.1f", ans[0].minDist, ans[0].avgDist);
return 0;
}
题目如下:
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 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.文章来源:https://www.toymoban.com/news/detail-494271.html
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
.文章来源地址https://www.toymoban.com/news/detail-494271.html
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
到了这里,关于1072 Gas Station (PAT甲级)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!