题目来源:PAT (Advanced Level) Practice
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
words:
residential 住宅的 candidate 候选人 recommendation 推荐,建议 accurate 精确的
题意:
给定一个n+m个顶点k条边的带权无向图,其中n个顶点表示居民点,m个顶点表示加油站点,求距离居民点最远但可以到达任何一个居民点的加油站点(加油站到居民点的距离大于服务范围Ds则认为不可达);
思路:
1. 使用邻接矩阵存储无向带权图,居民点从1-n编号,加油站从n+1到m编号;
2. 使用迪杰斯特拉算法以每个顶点为源点求到居民点的最短路径,记录每个加油站到居民点的最短距离和平均距离;
3. 选一个最短距离最大的点(若不唯一则在其中再选一个平均距离最小的点,(若还不唯一则在其中再选一个编号最小的点),输出居民点的最短距离和平均距离(保留一位小数),若无解则输出“No Solution”;
4. 浮点数四舍五入保留一位小数时直接使用printf()或setprecision(1)即可,输出可能显示有误但可通过测试(测试点4);
//PAT ad 1072 Gas Station
#include <iostream>
using namespace std;
#define N 1020
#define maxInt 999999999
#include <iomanip>
int n1,n2,n,m,d;
int adj[N][N]; //邻接矩阵
int dis[N]; //最短路径
bool vis[N]; //访问标记
int path[N]; //记录前驱
int Dijkstra(int v0) //求以v0为源点到其余点的最短路径
{
int i,v,w;
for(v=1;v<=n;v++)
{
vis[v]=false;
dis[v]=adj[v][v0];
if(dis[v]<maxInt)
path[v]=v0; //定前驱
else
path[v]=-1;
}
vis[v0]=true;dis[v0]=0; //源点
for(i=0;i<n-1;i++)
{
int mi=maxInt;
for(w=1;w<=n;w++)
if(!vis[w]&&dis[w]<mi) //找最小
{
v=w;mi=dis[w];
}
vis[v]=true;
for(w=1;w<=n;w++) //做更新
if(!vis[w]&&dis[v]+adj[v][w]<dis[w]) //间接距离小于直接距离
{
dis[w]=dis[v]+adj[v][w];
path[w]=v;
}
}
}
int toNum(string s) 将非数字或数字的顶点编号s转化为数字编号
{
int ans=0;
if(isdigit(s.front())) //数字编号
{
for(int i=0;i<s.size();i++)
ans=ans*10+s[i]-'0';
}
else //非数字编号
{
for(int i=1;i<s.size();i++)
ans=ans*10+s[i]-'0';
ans+=n1;
}
return ans;
}
int mi,sum;
bool abc() //找加油站到最近居民点的距离mi和到所有居民点的距离和sum
{
int i;
for(i=1;i<=n1;i++)
{
if(dis[i]>d) //超出覆盖范围
return false;
mi=min(dis[i],mi); //找最小
sum+=dis[i]; //求和
}
return true;
}
int main()
{
cin>>n1>>n2>>m>>d;cin.ignore();
n=n1+n2;
int i,j,w,a,b;
string s1,s2;
for(i=1;i<=n;i++) //初始化邻接矩阵
for(j=i;j<=n;j++)
adj[i][j]=adj[j][i]=maxInt;
for(i=0;i<m;i++)
{
cin>>s1>>s2>>w;cin.ignore();
a=toNum(s1); //将非数字或数字的顶点编号转化为数字编号
b=toNum(s2);
adj[a][b]=adj[b][a]=w; //增加边
}
int ansMi=0; //最大的最小距离
int ansSum=maxInt; //最小的平均距离
int index=-1;
for(i=n1+1;i<=n;i++)
{
mi=maxInt;sum=0;
Dijkstra(i); //以某个加油站为起点,用迪杰斯特拉算法求最短路径
if(abc())
{
if(mi>ansMi) //距离远的
{
ansMi=mi;
ansSum=sum;
index=i-n1-1;
}
else if(mi==ansMi&&sum<ansSum) //平均距离近的
{
ansSum=sum;
index=i-n1-1;
}
}
}
if(index==-1) //无解
cout<<"No Solution"<<endl;
else
{
cout<<"G"<<index+1<<endl;
double average=ansSum*1.0/n1;
cout<<fixed<<setprecision(1)<<ansMi*1.0<<" "; //输出最小距离
cout<<fixed<<setprecision(1)<<average<<endl; //输出平均距离
}
return 0;
}