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
Process
最后一个测试点不过的话,可能是这两个原因:
1.输入时的问题:G10是否可以成功输入;
2.内存问题:数组大小要开双倍;
Code
#include<bits/stdc++.h>
using namespace std;
#define P pair<int, int>
#define INF 2140000000
priority_queue<P, vector<P>, greater<P>> a;
int cnt, n, m, k, d, head[20010], dis[20010], vis[20010], num;
pair<double, double> p;
class node
{
public:
int to, next, w;
}edge[20010];
void add(int u, int v, int w)
{
edge[++cnt].w = w;
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt;
}
void dijkstra(int s)
{
for (int i = 1; i <= n+m; i++)
dis[i] = INF;
dis[s] = 0;
a.push(make_pair(0, s));
while (!a.empty())
{
int temp = ().second;
a.pop();
if (!vis[temp])
{
vis[temp] = 1;
for (int i = head[temp]; i; i = edge[i].next)
{
if (dis[edge[i].to] > dis[temp] + edge[i].w)
{
dis[edge[i].to] = dis[temp] + edge[i].w;
a.push(make_pair(dis[edge[i].to], edge[i].to));
}
}
}
}
}
int To(string str)
{
if (str[0] == 'G')
{
string p = str.substr(1, str.size() - 1);
return stoi(p) + n;
}
else
{
int k = stoi(str);
return k;
}
}
int main()
{
cin >> n >> m >> k >> d;
for (int i = 0; i < k; i++)
{
int u, v, w;
string str1, str2;
cin >> str1 >> str2 >> w;
u = To(str1);
v = To(str2);
add(u, v, w);
add(v, u, w);
}
p.first = -1;
p.second = INF;
for (int j = 1; j <= m; j++)
{
memset(vis, 0, sizeof(vis));
a = {};
int flag = 1, minn = INF;
double sum = 0.0;
dijkstra(n + j);
for (int i = 1; i <= n; i++)
{
if (dis[i] > d)
{
flag = 0;
break;
}
minn = min(minn, dis[i]);
sum += dis[i];
}
if (!flag)
continue;
if (minn > p.first || (minn == p.first && p.second > sum / n))
{
p.second = sum / n;
p.first = minn;
num = j;
}
}
if (p.first == -1)
cout << "No Solution";
else
printf("G%d\n%.1lf %.1lf", num, p.first, p.second);
return 0;
}