PAT
来源:互联网 发布:js延迟加载方式 编辑:程序博客网 时间:2024/06/06 09:30
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 51 2 21 4 21 G1 41 G2 32 3 22 G2 13 4 23 G3 24 G1 3G2 G1 1G3 G2 2Sample Output 1:
G12.0 3.3Sample Input 2:
2 1 2 101 G1 92 G1 20Sample Output 2:
No Solution
题目要求:
在给定的“候选”建站点中挑选一个点,要求满足下列条件:
1.所有居民区都在加油站点服务范围内
2.所有居民到加油站点距离尽可能的远
3.如果存在多解,输出各居民区到加油站的距离和的平均值最小的情况4.输出符合条件的建站点,距离建站点最近的居民区的距离,所有居民区到建站点的距离和的平均值
解题:
1.分别以各个建站点为起点,通过Dijkstra算出各居民区到建站点的距离,和服务范围比较即可
2.遍历居民区到建站点的距离,找出最近点
3.遍历居民区到建站点的距离,求和计算平均数
4.输出结果
#include<iostream>#include<algorithm>#include<cstdio>#include<string>#define INF 999999999using namespace std;string s1, s2;int N, M, K, Ds, a, b, c, ans=-1;int e[1020][1020], vis[1020], dis[1020];double ave=INF,minlen=-1;int main(){while(scanf("%d%d%d%d",&N, &M, &K, &Ds) != EOF){// init()fill(e[0], e[0]+1020*1020, INF);// inputfor(int i = 0; i < K; i++){cin>>s1>>s2>>c;if(s1[0] == 'G'){s1 = s1.substr(1);a = N + stoi(s1);}else{a = stoi(s1);}if(s2[0] == 'G'){s2 = s2.substr(1);b = N + stoi(s2);}else{b = stoi(s2);}e[a][b] = e[b][a] = c;}// Dijkstra M timesfor(int i = 0; i < M; i++){// init();fill(dis, dis+1020, INF);fill(vis, vis+1020, 0);int flag = 1;double sum = 0, tminlen = INF;dis[N+i+1] = 0;// Dijkstrafor(int t = 0; t < N+M; t++){int x = -1, minl = INF;for(int j = 1; j <= N+M; j++){if(!vis[j] && dis[j] < minl){x = j;minl = dis[j];}}if(x == -1) break;vis[x] = 1;for(int j = 1; j <= N+M; j++){if(!vis[j] && e[x][j] != INF){dis[j] = min(dis[j], dis[x]+e[x][j]);}}}// judgefor(int j = 1; j <= N; j++){if(dis[j] > Ds){tminlen = -1;break;}tminlen = min(tminlen, (double)dis[j]);sum += dis[j];}if(tminlen == -1) continue;double tave = sum / N;if(tminlen > minlen){minlen = tminlen;ave = tave;ans = i;}else if(tminlen == minlen && tave < ave){ave = tave;ans = i;}}if(ans == -1)printf("No Solution\n");else printf("G%d\n%.1f %.1f\n",ans+1, minlen,ave);}return 0;}
- pat
- 【PAT】
- PAT
- PAT
- PAT
- PAT
- PAT
- PAT
- PAT
- PAT
- PAT
- pat
- PAT
- PAT
- PAT
- PAT
- PAT
- PAT
- TensorFlow 实现卷积神经网络(Basic)
- rk3399安装ros rosdep init 出现错误
- Inno Setup入门(十四)——替换安装程序和卸载程序的图标
- 检查三个是否辨别其的安全性
- mock和fake的区别
- PAT
- Hive HQL练习
- Java中的Error与Exception
- 欢迎使用CSDN-markdown编辑器
- MySQL多源复制
- Redirecting to /bin/systemctl restart sshd.service
- iOS webView 加载HTML5获取加载后的高度
- Spring Boot插件spring tool suite安装及使用
- GR(Graceful Restart,平滑重启)