pat 1072
来源:互联网 发布:蚂蚁公舍 上海 知乎 编辑:程序博客网 时间:2024/06/06 18:40
1072. Gas Station (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
//最好的station是距离最近的村庄的距离最大的station(刚开始做的时候以为是距离所有村庄距离总和最大的station是最好的)//最后一个测试案例没有通过。//在全局变量中最好不要用 max,count,min这样的变量(有时会编译错误,变量定义模糊,因为在引入的头文件中或许有以max这样命名的函数)#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define INF 1 << 30 //INF 最好不要用0x7fffffff 因为若在dij算法中若本次得到的min值是正数,则语句 min + map[index][j]有时便失去了作用。int n, m, k;double Ds;double map[1015][1015], dist[1015];int vis[1015];double sum, maxx, mm;void dij(int st){bool flag = false;int i, j;int tmp = n+m;for(i = 1; i <= tmp; i++)dist[i] = INF;dist[st] = 0;for(i = 1; i <= tmp; i++){double min = INF;int index = -1;for(j = 1; j <= tmp; j++){if(vis[j] == 0 && dist[j] < min){min = dist[j];index = j;}}if(min == INF) return;vis[index] = 1;if(index <= n) {sum += min;if(flag == false){mm = min; //记录距离村庄的最短距离flag = true;}if(min > maxx)maxx = min;}for(j = 1; j <= tmp; j++){if(vis[j] == 0) {double tt = min + map[index][j];if(tt < dist[j])dist[j] = tt;}}}}int main(){int i, j;char str1[4], str2[4];int a, b;double l;while(scanf("%d%d%d%lf", &n, &m, &k, &Ds) != EOF){for(i = 1; i <= n+m; i++)for(j = 1; j <= n+m; j++)map[i][j] = map[j][i] = INF;for(i = 1; i <= k; i++){scanf("%s%s%lf", str1, str2, &l);if(str1[0] != 'G')a = str1[0] - '0';else if(str1[0] == 'G')a = n + str1[1] - '0';if(str2[0] != 'G')b = str2[0] - '0';else if(str2[0] == 'G')b = n + str2[1] - '0';map[a][b] = map[b][a] = l;}int index;double ll = -1, least = 0;for(i = 1; i <= m; i++){memset(vis, 0, sizeof(vis));sum = 0;maxx = -1;dij(i+n);if(maxx > Ds)continue;if(i == 1){ll = sum;index = i;least = mm;}else if(i > 1){if(least < mm){ll = sum;index = i;least = mm;}else if(least == mm){if(sum / n < ll / n){ll = sum;index = i;least = mm;}}}}if(ll == -1) printf("No Solution\n");else{printf("G%d\n", index);printf("%.1lf %.1lf\n", 1.0 * least, ll * 1.0 / n);}}return 0;}
- pat 1072
- PAT Advanced Level 1072
- pat-advance(1069-1072)
- PAT(甲级)1072
- pat 1072 Gas Station
- PAT甲级1072
- PAT 1072 Gas Station
- pat
- 【PAT】
- PAT
- PAT
- PAT
- PAT
- PAT
- PAT
- PAT
- PAT
- PAT
- JAVA中String与StringBuffer的区别
- OSI开放协议会话层的初步理解
- struts2.3.16环境搭建要用到的jar包
- MachineLearning(Hsuan-Tien Lin)第三讲
- Optimal Parking
- pat 1072
- Oracle EBS R12.0.6 - XXX is not a valid responsibility for the current user. Please contact your Sys
- 2014-3-12 FPGA学习-按键消抖
- Shopaholic
- 【ORACLE】flash recovery area(闪回恢复区)管理
- Codeforces 401A Vanya and Cards(水题)
- 【Prim】-POJ-2421-构造公路
- bnu条形码设计(递推)
- 一个或多个音频服务未运行 win7 错误1079:此服务的账户不同于运行于同一进程上的其他服务账户