0
点赞
收藏
分享

微信扫一扫

PAT甲级1072


1072. Gas Station (30)


时间限制



200 ms



内存限制



65536 kB



代码长度限制



16000 B



判题程序



Standard



作者



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 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<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
struct Node
{
int v, dis;
}node;
struct compare
{
bool operator()(Node n1, Node n2)
{
return n1.dis > n2.dis;
}
};
struct Result
{
string solution;
double mindistance, averagedistance;
bool operator<(Result r)
{
if (mindistance != r.mindistance)
return mindistance > r.mindistance;//最大的最近距离,注意理解题意
else if (averagedistance != r.averagedistance)
return averagedistance < r.averagedistance;
else
return solution < r.solution;
}
}result;
int N, M, K, Ds;
const int maxn = 1020;//这里注意maxn必须是1011以上
const int INF = 1000000000;
map<string, int> gas2num;
map<int,string> num2gas;
vector<Node> Adj[maxn];
bool vis[maxn];
int d[maxn];
vector<Result> solutions;
void Dijkstra(int s)
{
fill(vis, vis + maxn, false);
fill(d, d + maxn, INF);
d[s] = 0;
priority_queue<Node, vector<Node>, compare>Q;
node.v = s; node.dis = 0;
Q.push(node);
int u;
for (int i = 1; i <= N + M; i++)
{
if (!Q.empty())
{
u = Q.top().v;
vis[u] = true;
Q.pop();
}
for (int j = 0; j < Adj[u].size(); j++)
{
int v = Adj[u][j].v;
int dis = Adj[u][j].dis;
if (!vis[v]&&d[u]+dis<d[v])
{
d[v] = d[u] + dis;
node.v = v; node.dis = d[v];
Q.push(node);
}
}
}
double sum = 0.0;
result.mindistance = INF;
for (int i = 1; i <= N; i++)
{
if (d[i] > Ds)return;
else sum += d[i];
if (result.mindistance > d[i])
{
result.mindistance = d[i];
}
}
result.solution = num2gas[s];
result.averagedistance = sum / N;
solutions.push_back(result);
}
int main()
{
scanf("%d%d%d%d", &N, &M, &K, &Ds);
string s1, s2; int distance;
int n = N;
for (int i = 0; i < K; i++)
{
cin >> s1 >> s2>>distance;
int v1, v2;
if (s1[0] != 'G')
{
//v1 = s1[0] - '0';这个地方注意若输入的是10以上的数就不行
v1 = stoi(s1);
}
else
{
if (gas2num.find(s1) == gas2num.end())
{
v1 = gas2num[s1] = ++n;
num2gas[n] = s1;
}
else
v1 = gas2num[s1];
}
if (s2[0] != 'G')
{
//v2 = s2[0] - '0';
v2 = stoi(s2);
}
else
{
if (gas2num.find(s2) == gas2num.end())
{
v2 = gas2num[s2] = ++n;
num2gas[n] = s2;
}
else
v2 = gas2num[s2];
}
node.v = v2; node.dis = distance;
Adj[v1].push_back(node); node.v = v1;
Adj[v2].push_back(node);
}
for (int i = N + 1; i <= N + M; i++)
{
Dijkstra(i);
}
if (solutions.size())
{
sort(solutions.begin(), solutions.end());
cout << solutions[0].solution << endl;
printf("%.1f %.1f\n", solutions[0].mindistance, solutions[0].averagedistance);
}
else
printf("No Solution\n");
return 0;
}


举报

相关推荐

0 条评论