PAT 1072. Gas Station (30)
2016-02-25 16:22
573 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
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 此题不难,只是考察了最简单的Dijkstra算法,读清楚题意即可,代码如下:
#include<iostream> #include<string> #include<iomanip> #include"stdio.h" using namespace std; int N,M,K,Ds; int **G; bool *collected; int *dist; void Dijkstra(int); int GetMinDist(); int main() { scanf("%d%d%d%d",&N,&M,&K,&Ds); //初始化G G = new int* [N+M]; dist = new int [N+M]; collected = new bool [N+M]; for (int i=0;i<N+M;i++) { G[i] = new int [N+M]; collected[i] = false; dist[i] = 65536; } for (int i=0;i<N+M;i++) for (int j=0;j<N+M;j++) G[i][j] = 0; int iP1,iP2; string P1,P2; int Dist; //开始输入道路信息 for (int i=0;i<K;i++) { cin >> P1 >> P2 >> Dist; if (P1[0] == 'G') { P1.erase(P1.begin()); iP1 = atoi(P1.c_str())+N-1; } else iP1 = atoi(P1.c_str())-1; if (P2[0] == 'G') { P2.erase(P2.begin()); iP2 = atoi(P2.c_str())+N-1; } else iP2 = atoi(P2.c_str())-1; G[iP1][iP2] = Dist; G[iP2][iP1] = Dist; } //开始Dijkstra int mind=65536; double averd=0.; bool flag_all = true; int minG=-1,mindd=-1; double averdd=-1.; for (int i=N;i<N+M;i++) //对每一个G进行Dijkstra { Dijkstra(i); //对dist[]进行扫描,找出最小和平均值,或者没有 for (int j=0;j<N;j++) { averd += dist[j]; if (mind > dist[j]) mind = dist[j]; if (dist[j] > Ds) { flag_all = false; break; } } if (flag_all) { averd = averd / double(N); if (mindd < mind) { mindd = mind; minG=i; averdd = averd; } else if (mindd == mind) { if (averdd > averd) { minG=i; averdd = averd; } } } //复原各变量 for (int j=0;j<N+M;j++) { collected[j] = false; dist[j] = 65536; } mind=65536; averd=0.; flag_all = true; } if (minG == -1) cout << "No Solution" << endl; else { cout << 'G' << minG-N+1 << endl; cout << fixed << setprecision(1) << double(mindd) << ' ' << fixed << setprecision(1) << averdd << endl; } return 0; } void Dijkstra(int S) { //初始化源节点 collected[S]=true; dist[S]=0; //初始化与S相邻的节点 for (int i=0;i<N+M;i++) { if (G[S][i]) { dist[i]=G[S][i]; } } //开始贪心算法 int V; while (1) { V = GetMinDist(); if (V == -1) break; collected[V]=true; for (int i=0;i<N+M;i++) { if (!collected[i] && G[V][i] && dist[i] > dist[V]+G[V][i]) dist[i] = dist[V]+G[V][i]; } } } int GetMinDist() { int min_d = 65536,min_d_i=-1; for (int i=0;i<N+M;i++) if (!collected[i] && dist[i] < min_d) { min_d = dist[i]; min_d_i = i; } return min_d_i; }
相关文章推荐
- Java之路
- CDataFile
- 【转】Solr客户端查询参数总结
- iOS开发之设置导航栏标题的字体大小和颜色
- SQL Update多表更新
- 透视投影变换推导(2)
- centos 6.4 python升级到2.7.11
- java中登录查询条件为中文时,rs.next为false,导致无法登录
- VMware虚拟环境下红帽RHCS集群使用vmware fencing失败的原因汇总
- android 开发技巧(6)--在 Canvas 上显示动画
- 36、微信通讯录页面
- Linux CentOS6.5下编译安装MySQL 5.6.22
- 23种设计模式(13):适配器模式
- Java进阶02 异常处理
- 机器学习入门1
- 设计模式-适配器模式(Adapter)
- Qt5创建模态和非模态对话框
- 转 C编译: 使用gdb调试
- SQL,根据不同条件拼接不同SQL,非if拼接 改为SQL where形式
- PDOException PDOException: could not find driver