博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——1072 Gas Station
阅读量:4541 次
发布时间:2019-06-08

本文共 4461 字,大约阅读时间需要 14 分钟。

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 (≤), the total number of houses; M (≤), the total number of the candidate locations for the gas stations; K(≤), 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 2

Sample Output 1:

G12.0 3.3

Sample Input 2:

2 1 2 101 G1 92 G1 20

Sample Output 2:

No Solution 即对每个加油站都是用Dijkstra计算最小距离
1 #include 
2 #include
3 #include
4 #define inf 999999999 5 using namespace std; 6 int N, M, K, Ds, indexG = -1; 7 double minG = -1, avgG; 8 int city[1010][1010]; 9 int main()10 {11 cin >> N >> M >> K >> Ds;12 fill(city[0], city[0] + 1010 * 1010, inf);13 for (int i = 0; i < K; ++i)14 {15 string s1, s2;16 int a = 0, b = 0, dis;17 cin >> s1 >> s2 >> dis;18 if (s1[0] == 'G')19 {20 a += N;21 s1.erase(0, 1);22 }23 if (s2[0] == 'G')24 {25 b += N;26 s2.erase(0, 1);27 }28 a += stoi(s1);29 b += stoi(s2);30 city[a][b] = city[b][a] = dis;31 }32 //Dijsktra33 for (int k = N+1; k <= N+M; ++k)//每个加油站都是用一次dij34 {35 int temp[1010];36 fill(temp, temp + 1010, inf);37 bool visit[1010];38 fill(visit, visit + 1010, false);39 temp[k] = 0;40 for (int i = 1; i <= N+M; ++i)41 {42 int v = -1, minDis = inf;43 for (int j = 1; j <= N+M; ++j)44 {45 if (visit[j] == false && minDis > temp[j])46 {47 v = j;48 minDis = temp[j];49 }50 }51 if (v == -1)break;52 visit[v] = true;53 for (int u = 1; u <= N+M; ++u)54 {55 if (visit[u] == false && city[v][u] != inf)56 {57 if (temp[u] > temp[v] + city[v][u])58 temp[u] = temp[v] + city[v][u];59 }60 }61 }62 int flag = 1, minD = inf;63 double avgD = 0.0;64 for (int i = 1; i <= N; ++i)65 {66 minD = minD < temp[i] ? minD : temp[i];67 avgD += (double)temp[i];68 if (temp[i] > Ds)69 {70 flag = 0;71 break;72 }73 }74 avgD /= N;75 if (flag == 1 && minG <= minD)76 {77 if ((minG < minD) || (minG == minD && avgD < avgG))78 {79 minG = minD;80 indexG = k;81 avgG = avgD;82 }83 else if (minG == minD && avgD == avgG)84 indexG = indexG < k ? indexG : k;85 }86 }87 if (indexG == -1)88 cout << "No Solution" << endl;89 else90 printf("G%d\n%.1f %.1f\n", indexG - N, minG, avgG);91 return 0;92 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11312473.html

你可能感兴趣的文章
决策树基础
查看>>
献给程序员之如何与陌生人交谈
查看>>
Python之登录接口
查看>>
【arc074E】RGB Sequence
查看>>
工作3年了才开始有自己的积累
查看>>
使用axios向后端传递数据,后端接收不到?
查看>>
List 去处自定义重复对象方法
查看>>
CoreData的使用-1
查看>>
阿里云安装samba
查看>>
jsonp跨域
查看>>
day1-xml语言
查看>>
有谁知道瑞星在windows登录界面图标按钮是如何放上去的吗
查看>>
Delphi高效的字符串处理
查看>>
消息中间件ActiveMQ、RabbitMQ、RocketMQ、ZeroMQ、Kafka如何选型?
查看>>
带宽的理解
查看>>
一、简单工厂模式
查看>>
查询出结果 给其 加上序号的方法 msql
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第2节 线程实现方式_1_并发与并行...
查看>>
asp.net web.config配置节说明(转发)
查看>>
PPT幻灯片放映时无法全屏的解决方法
查看>>