1072 Gas Station (30)(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 (<= 10^3^), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 10^4^), the number of roads connecting the houses and the gas stations; and D~S~, 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

//看了好几遍,终于理解了题目的意思。并且搜索别的题解。

1.修建加油站,要求加油站能够服务所有的居民屋子,是有距离限制的。

2.要求距离加油站最近的屋子最远,出于安全的考虑。

3.如果这些要求满足,那么输出到所有居民屋子平均距离最短的。

4.同等条件下,输出编号最小的。

代码来自:https://www.liuchuo.net/archives/2376

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int inf = ;
int n, m, k, ds, station;
int e[][], dis[]; bool visit[];
int main() {
fill(e[], e[] + * , inf);
fill(dis, dis + , inf);
scanf("%d%d%d%d", &n, &m, &k, &ds);
for(int i = ; i < k; i++) {
int tempdis;
string s, t;
cin >> s >> t >> tempdis;
int a, b;
if(s[] == 'G') {
s = s.substr();
a = n + stoi(s);
} else {
a = stoi(s);
}
if(t[] == 'G') {
t = t.substr();
b = n + stoi(t);
} else {
b = stoi(t);
}
e[a][b] = e[b][a] = tempdis;
}
int ansid = -;
double ansdis = -, ansaver = inf;
for(int index = n + ; index <= n + m; index++) {
double mindis = inf, aver = ;
fill(dis, dis + , inf);
fill(visit, visit + , false);
dis[index] = ;
for(int i = ; i < n + m; i++) {
int u = -, minn = inf;
for(int j = ; j <= n + m; j++) {
if(visit[j] == false && dis[j] < minn) {
u = j;
minn = dis[j];
}
}
if(u == -) break;
visit[u] = true;
for(int v = ; v <= n + m; v++) {
if(visit[v] == false && dis[v] > dis[u] + e[u][v])
dis[v] = dis[u] + e[u][v];
}
}
for(int i = ; i <= n; i++) {
if(dis[i] > ds) {
mindis = -;
break;
}
if(dis[i] < mindis) mindis = dis[i];
//mindis是加油站到某个居民屋子的最短距离。
aver += 1.0 * dis[i];
}
if(mindis == -) continue;
aver = aver / n;
if(mindis > ansdis) {
ansid = index;
ansdis = mindis;
ansaver = aver;
} else if(mindis == ansdis && aver < ansaver) {
ansid = index;
ansaver = aver;
}
}
if(ansid == -)
printf("No Solution");
else
printf("G%d\n%.1f %.1f", ansid - n, ansdis, ansaver);
return ;
}

//大佬写的可真好,思路清晰,代码简洁!关键是将加油站接着存在了居民屋后面,省空间,而且好遍历,这个我肯定想不到。厉害。

最新文章

  1. 免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
  2. SQL Server 不清空数据,修改数据库字段、结构,阻止保存要求重新创建表的更改
  3. python程序性能分析
  4. 编码Q&amp;A
  5. 一周一话题之四(JavaScript、Dom、jQuery全面复习总结&lt;js篇&gt;)
  6. JVM内存模型,垃圾回收算法
  7. Memcached源码分析之内存管理
  8. Linux经常使用的文件传输的几种方式
  9. MySQL完全备份、增量备份与恢复[转]
  10. NLP+词法系列(二)︱中文分词技术简述、深度学习分词实践(CIPS2016、超多案例)
  11. Linux IPC实践(13) --System V IPC综合实践
  12. C++之虚函数
  13. Python笔记-面向对象编程
  14. 自动化测试基础篇--Selenium cookie操作
  15. 转://oracle Wallet在expdp/impdp中使用场景
  16. ProcessExplorer使用分享
  17. rpm梳理
  18. 什么是跨域访问,JSON&amp;JSONP
  19. SpringBoot 使用Hikaricp连接池
  20. K均值算法

热门文章

  1. c++ 用new创建二维数组~创建指针数组【转】
  2. Oracle 学习之触发器
  3. 报错---“node install.js”
  4. sencha touch Demo(示例)(2014-6-25)
  5. 学习生活必须知道的网站或者App
  6. tornado web开发
  7. calloc(), malloc(), realloc(), free(),alloca()
  8. jquery的ajax()函数中文传值出现乱码完美解决方案
  9. SAP全球企业官孙小群的生活智慧
  10. thinkCMF----调用幻灯片