题目:
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 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
思路:之后补上,我觉得这个题有点考英语,读了半天都读不懂。。最后脑抽写了堆优化(优先队列那个),调了好久
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3;
const int INF = 0x7f7f7f7f;
struct node {
int v,w;
node(int a,int b):v(a),w(b) {}
bool operator < (const node b)const {
return w > b.w;
}
};
priority_queue <node> q;
vector <node> g[maxn];
int dist[maxn];
int flag,res;
bool vis[maxn];
int n,m,ds;
double cnt;
void dj(int u) {
for(int i = 1;i <= n+m; i++) {
dist[i] = INF;
vis[i] = 0;
}
q.push(node(u,0));
dist[u] = 0;
res = INF , cnt = 0;
while(!q.empty()) {
int u1 = q.top().v;q.pop();
vis[u1] = 1;
for(int i = 0; i < g[u1].size(); i++) {
int v = g[u1][i].v,w = g[u1][i].w;
if(dist[v] > dist[u1] + w && !vis[v]) {
dist[v] = dist[u1] + w;
q.push(node(v,dist[v]));
}
}
}
for(int i = 1; i <= n; i++) {
cnt += dist[i];
if(dist[i] > ds) {
flag = 1;
return;
}
res = min(res,dist[i]);
}
flag = 0;
cnt /= n;
}
int main() {
int k;
//ios::sync_with_stdio(0);
//cin.tie(0);cout.tie(0);
cin >> n >> m >> k >> ds;
for(int i = 0; i < k; i++) {
string a,b;
int w;
cin >> a >> b >> w;
int t1 = 0,t2 = 0,len = 0;
while(len < a.length()) {
if(a[len] != 'G')
t1 = t1 * 10 + a[len] - '0';
len++;
}
if(a[0] == 'G') {
t1 += n;
}
len = 0;
while(len < b.length()) {
if(b[len] != 'G')
t2 = t2 * 10 + b[len] - '0';
len++;
}
if(b[0] == 'G') {
t2 += n;
}
//cout << t1 << " " << t2 << "\n";
g[t1].push_back(node(t2,w));
g[t2].push_back(node(t1,w));
}
double ans = -1,aans = INF;
int p = -1;
for(int i = n+1; i <= n+m; i++) {
dj(i);
if(flag)continue;
if(res > ans) {
p = i;
ans = res;
aans = cnt;
}
else if(res == ans && cnt < aans) {
p = i;
aans = cnt;
}
}
if(p == -1) {
printf("No Solution");
}
else {
printf("G%d\n",p-n);
printf("%.1f %.1f",ans,aans);
}
return 0;
}