pat 1072 Gas Station
来源:互联网 发布:淘宝网刷单 物流单号 编辑:程序博客网 时间:2024/05/24 06:15
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
#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>using namespace std;#define N 1015double connect[N][N];double _distance[N];int visited[N];int n, m, k;struct GG{int i;double minn;double avg;bool operator < (const GG& a) const{if(minn != a.minn) {return minn > a.minn;} else if(avg != a.avg) {return avg < a.avg;} else return i < a.i;}};double dijstla(int t) {visited[t] = 1;for(int i = 1; i <= n+m; i++) {if(connect[t][i] > 0.0)_distance[i] = connect[t][i];}for (int i = 0; i < n; ++i){int minn = 0x7fffffff, index = -1;for (int j = 1; j <= n+m; ++j){if(_distance[j] > 0.0 && visited[j] == 0) {if(_distance[j] < minn) {minn = _distance[j];index = j;}}}if(minn == 0x7fffffff) break;visited[index] = 1;for (int j = 1; j <= n+m; ++j){if(visited[j] == 1) continue;if(connect[index][j] <= 0.0) continue;if(_distance[j] > 0.0) {if(_distance[j] > _distance[index] + connect[index][j])_distance[j] = _distance[index] + connect[index][j];} else if(_distance[j] == 0.0) {_distance[j] = _distance[index] + connect[index][j];}}}double maxx = 0.0;for (int i = 1; i <= n; ++i){if(_distance[i] > 0.0) {if(_distance[i] > maxx)maxx = _distance[i];} else if(_distance[i] == 0.0) {maxx = 0.0;break;}}return maxx;}double avg_distance() {double sum = 0.0;for (int i = 1; i <= n; ++i){sum += _distance[i];}return sum/n;}double min_distance() {double minn = 0x7fffffff;for (int i = 1; i <= n; ++i){if(_distance[i] > 0.0) {if(_distance[i] < minn)minn = _distance[i];}}return minn;}int str2int(string a) {int i = 0, be=0;if(a[0] == 'G') i = 1;if(true) {int tmp = 0;for (int j = i; j < a.length(); ++j){tmp *= 10;tmp += a[j]-'0';}be = (i==1?n:0)+tmp;}return be;} int main() {double ds;vector<GG> vec;scanf("%d %d %d %lf", &n, &m, &k, &ds);string a, b;int be, ed;double dist;for (int i = 0; i < k; ++i){cin >> a >> b >> dist;be = str2int(a);ed = str2int(b);connect[be][ed] = dist;connect[ed][be] = dist;}for (int i = 1; i <= m; ++i){memset(_distance, 0.0, sizeof(_distance));memset(visited, 0, sizeof(visited));double tmp = dijstla(n+i);double avg = avg_distance();double minn = min_distance();GG g;g.i=i;g.avg = avg;g.minn = minn;if(tmp <= ds && tmp > 0)vec.push_back(g);//printf("%lf %lf %lf\n", tmp, avg, minn);}if(vec.size() > 0) {sort(vec.begin(), vec.end());printf("G%d\n", vec[0].i);printf("%.1lf %.1lf\n", vec[0].minn, vec[0].avg);} else {printf("No Solution\n");}return 0;}
开始时,最后一个测试点过不了。是因为没有考虑到节点数大于10的时候,用原来的方法将字符串转换为数字便会出现错误。
- pat 1072 Gas Station
- PAT 1072 Gas Station
- PAT 1072 Gas Station SPFA
- 浙大PAT 1072题 1072. Gas Station
- Pat(Advanced Level)Practice--1072(Gas Station)
- PAT 1072. Gas Station
- PAT 1072. Gas Station
- PAT 1072. Gas Station
- 【PAT】1072. Gas Station
- PAT A1072. Gas Station
- pat 1072. Gas Station
- PAT-1072. Gas Station(DIJKSTRA)
- PAT 1072. Gas Station (dijkstra)
- PAT 1072. Gas Station (30)
- PAT 1072. Gas Station (30)
- 【PAT】1072. Gas Station (30)
- PAT 1028Gas Station (30)
- PAT A1072 Gas Station(30)
- 标签
- HDU 5569 (BC#63)DP
- 在python3.3中使用matplotlib的安装方式
- Epoll详解及源码分析
- OpenCV中特征点提取和匹配的通用方法
- pat 1072 Gas Station
- 南大软院大神养成计划--第四天JavaScript
- ubuntu常用软件包deb的安装与卸载
- 关于Mybaties 的简单应用和 入门
- Office 365 Service Communications API operations--Get Historical Status
- Kappa系数
- 设计模式之反射与配置文件
- 这个功能很简单,要做多久
- 核函数与支持向量机入门