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 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

即对每个加油站都是用Dijkstra计算最小距离
 #include <iostream>
#include <vector>
#include <string>
#define inf 999999999
using namespace std;
int N, M, K, Ds, indexG = -;
double minG = -, avgG;
int city[][];
int main()
{
cin >> N >> M >> K >> Ds;
fill(city[], city[] + * , inf);
for (int i = ; i < K; ++i)
{
string s1, s2;
int a = , b = , dis;
cin >> s1 >> s2 >> dis;
if (s1[] == 'G')
{
a += N;
s1.erase(, );
}
if (s2[] == 'G')
{
b += N;
s2.erase(, );
}
a += stoi(s1);
b += stoi(s2);
city[a][b] = city[b][a] = dis;
}
//Dijsktra
for (int k = N+; k <= N+M; ++k)//每个加油站都是用一次dij
{
int temp[];
fill(temp, temp + , inf);
bool visit[];
fill(visit, visit + , false);
temp[k] = ;
for (int i = ; i <= N+M; ++i)
{
int v = -, minDis = inf;
for (int j = ; j <= N+M; ++j)
{
if (visit[j] == false && minDis > temp[j])
{
v = j;
minDis = temp[j];
}
}
if (v == -)break;
visit[v] = true;
for (int u = ; u <= N+M; ++u)
{
if (visit[u] == false && city[v][u] != inf)
{
if (temp[u] > temp[v] + city[v][u])
temp[u] = temp[v] + city[v][u];
}
}
}
int flag = , minD = inf;
double avgD = 0.0;
for (int i = ; i <= N; ++i)
{
minD = minD < temp[i] ? minD : temp[i];
avgD += (double)temp[i];
if (temp[i] > Ds)
{
flag = ;
break;
}
}
avgD /= N;
if (flag == && minG <= minD)
{
if ((minG < minD) || (minG == minD && avgD < avgG))
{
minG = minD;
indexG = k;
avgG = avgD;
}
else if (minG == minD && avgD == avgG)
indexG = indexG < k ? indexG : k;
}
}
if (indexG == -)
cout << "No Solution" << endl;
else
printf("G%d\n%.1f %.1f\n", indexG - N, minG, avgG);
return ;
}

最新文章

  1. 采用DBCP连接池技术管理连接
  2. 用纯CSS创建一个三角形
  3. HDU 2838 (DP+树状数组维护带权排序)
  4. 查看Linux系统版本信息
  5. [CCF2015.12]题解
  6. python UTC,时间戳,字符串转换
  7. 2015南阳CCPC H - Sudoku 暴力
  8. lnmp安装--php安装
  9. HDU 1394-Minimum Inversion Number(BIT)
  10. Nginx/Apache图片缩略图技术
  11. Swift 算法实战之路:基本语法与技巧
  12. BeyondCompare两个文件中同一行字符长度不一致的文件对比,比如pi文件对比(xjl456852原创)
  13. SqlServer之触发器
  14. 为进度条增加“伪进度条”?(使用QEventLoop和QTimer)
  15. Cell重用时数据混乱的管理方法
  16. Debug模式下程序卡
  17. Vue.js库的第一天的学习
  18. Linux命令行报错 bash: cannot create temp file for here-document: No space left on device
  19. vs视图引入命名空间设置方法
  20. Linux巩固记录(9) keepalived+nginx搭建高可用负载分发环境

热门文章

  1. 最短路(sp
  2. BeanShell Sampler生成uuid
  3. MySQL更新指定分组中最大值记录
  4. MYSQL查询查找重复的电子邮箱
  5. CF475F meta-universe
  6. zabbix_agentd 报错
  7. 尚学linux课程---8、rpm软件包安装
  8. Android Butterknife使用方法总结
  9. 【转载】一定要会用selenium的等待,三种等待方式必会
  10. 无法启动此程序,因此计算机中丢失VCRUNTIME140.dll。