1072 Gas Station
分数 30
作者 CHEN, Yue
单位 浙江大学
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.
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
* 首先建图,居民所从1开始编号,加油站从N+1开始编号,如果是加油站,
* 则将顶点创建为编号加N,与居民点区分开;
*
* 此后就是一个Dijkstra模板,我使用的是堆优化的Dijkstra算法;
* 详看代码:
/*** 首先建图,居民所从1开始编号,加油站从N+1开始编号,如果是加油站,* 则将顶点创建为编号加N,与居民点区分开;* * 此后就是一个Dijkstra模板,我使用的是堆优化的Dijkstra算法;* 详看代码:
*/#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <queue>using namespace std;typedef pair<double, double> PDD;
typedef pair<int, int> PII;const int N = 1010, M = 2*N, INF = 1e9;
vector<PII> Adj[M]; //要建立居民所和加油站,所以要开二倍N的空间
int d[M];
bool hs[M];
int Nv, Ne, m, T;void Read()
{cin >> Nv >> m >> Ne >> T;for(int i=0; i<Ne; ++i){string a, b;int u, v, w;cin >> a >> b >> w;//如果是加油站,则将顶点创建为编号加N,与居民点区分开if(!isdigit(a[0])) u = stoi(a.substr(1)) + N; else u = stoi(a);if(!isdigit(b[0])) v = stoi(b.substr(1)) + N;else v = stoi(b);Adj[u].push_back({v, w});Adj[v].push_back({u, w});}
}PDD Dijkstra(int st)
{//注意别写成了N,最开始就写成了N,调试了半天fill(hs, hs+M, 0);fill(d, d+M, INF);priority_queue<PII, vector<PII>, greater<PII>> pq;pq.push({0, st}); d[st] = 0;while(pq.size()){PII fs = pq.top();pq.pop();int u = fs.second;hs[u] = 0;for(auto ele : Adj[u]){int v = ele.first, w = ele.second;if(d[u] + w < d[v]) {d[v] = d[u] + w;if(hs[v] == 0){pq.push({d[v], v});hs[v] = 1;}}}}int MIN = INF, MAX = -1;double avg = 0;for(int i=1; i<=Nv; ++i){MIN = min(MIN, d[i]);MAX = max(MAX, d[i]);avg += d[i];}//最大距离超过了服务范围,则该点不能设为加油点if(MAX > T) MIN = -1;//返回最小距离以及平均距离return {MIN, avg/Nv};
}int main()
{Read();int idx = -1;PDD res = {0, INF};for(int i=1; i<=m; ++i){int u = i + N;PDD tm = Dijkstra(u);//最短距离更大if(tm.first > res.first) {res = tm;idx = i;}//最短距离相同的情况选择平均距离更短的点else if(tm.first == res.first && tm.second < res.second){res = tm;idx = i;}}//没有解的情况if(idx == -1) puts("No Solution");else {printf("G%d\n", idx);printf("%.1f %.1f\n", res.first, res.second);}return 0;
}