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 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=;i<N+M;i++)
{
G[i] = new int [N+M];
collected[i] = false;
dist[i] = ;
}
for (int i=;i<N+M;i++)
for (int j=;j<N+M;j++)
G[i][j] = ; int iP1,iP2;
string P1,P2;
int Dist; //开始输入道路信息
for (int i=;i<K;i++)
{
cin >> P1 >> P2 >> Dist;
if (P1[] == 'G')
{
P1.erase(P1.begin());
iP1 = atoi(P1.c_str())+N-;
}
else
iP1 = atoi(P1.c_str())-; if (P2[] == 'G')
{
P2.erase(P2.begin());
iP2 = atoi(P2.c_str())+N-;
}
else
iP2 = atoi(P2.c_str())-; G[iP1][iP2] = Dist;
G[iP2][iP1] = Dist;
} //开始Dijkstra
int mind=;
double averd=.;
bool flag_all = true;
int minG=-,mindd=-;
double averdd=-.; for (int i=N;i<N+M;i++) //对每一个G进行Dijkstra
{
Dijkstra(i);
//对dist[]进行扫描,找出最小和平均值,或者没有
for (int j=;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=;j<N+M;j++)
{
collected[j] = false;
dist[j] = ;
}
mind=;
averd=.;
flag_all = true;
} if (minG == -)
cout << "No Solution" << endl;
else
{
cout << 'G' << minG-N+ << endl;
cout << fixed << setprecision() << double(mindd) << ' ' << fixed << setprecision() << averdd << endl;
} return ;
} void Dijkstra(int S)
{
//初始化源节点
collected[S]=true;
dist[S]=; //初始化与S相邻的节点
for (int i=;i<N+M;i++)
{
if (G[S][i])
{
dist[i]=G[S][i];
}
} //开始贪心算法
int V;
while ()
{
V = GetMinDist();
if (V == -)
break;
collected[V]=true; for (int i=;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 = ,min_d_i=-; for (int i=;i<N+M;i++)
if (!collected[i] && dist[i] < min_d)
{
min_d = dist[i];
min_d_i = i;
}
return min_d_i;
}

PAT 1072. Gas Station (30)的更多相关文章

  1. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

  2. 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise

    题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...

  3. PAT 甲级 1072 Gas Station (30 分)(dijstra)

    1072 Gas Station (30 分)   A gas station has to be built at such a location that the minimum distance ...

  4. PAT 1072 Gas Station[图论][难]

    1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...

  5. PAT Advanced 1072 Gas Station (30) [Dijkstra算法]

    题目 A gas station has to be built at such a location that the minimum distance between the station an ...

  6. 1072. Gas Station (30)

    先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...

  7. 1072. Gas Station (30) 多源最短路

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  8. 1072 Gas Station (30)(30 分)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  9. PAT 1072. Gas Station

    A gas station has to be built at such a location that the minimum distance between the station and a ...

随机推荐

  1. oracle 配置监听 windows下 oracle 12c

    1, 在命令行窗口中 输入 hostname 查看主机名 2 查看本机ip地址 ,输入 ipconfig 本地连接 下面的 IPv4 地址 192.168.1.1053,测试连接 输入 ping lo ...

  2. 从下往上看--新皮层资料的读后感 第四部分 来自神经元的设计-perceptron 感知机

    搬地方了,其他的部分看知乎:https://zhuanlan.zhihu.com/p/22114481 直到50年代,perceptron被Frank Rosenblatt搞了出来.perceptro ...

  3. android SDK下载及中文API地址

    中文API:http://wiki.eoeandroid.com/Android_API_Guides Android Dev Tools官网地址:www.androiddevtools.cn 收集整 ...

  4. 关于android的@TargetApi和@SuppressLint("NewApi")

    看别人的代码好多地方用到了@TargetApi.以前一直不知道这个是什么意思.后面偶然看了下sdk.才有所明白. 其实这个东西就是在你使用了android Lint检查工具的时候,为了防止代码出现提示 ...

  5. ORACLE 连接SQLSERVER 数据库备忘

    最近工作需要,要从SQL SERVER数据库中同步提取数据. 这里采用了  Oracle Gateway 来连接,折腾了半天,终于搞定,记录下已备下次使用. 基本资料网上都可以搜很多,官网配置说明在这 ...

  6. JavaScript Array对象sort() 方法小结

    sort() 方法用于对数组的元素进行排序. 语法arrayObject.sort(sortfunction) 参数sortfunction 可选.规定排序顺序.必须是函数. 返回值对数组的引用.请注 ...

  7. 开启LOH压缩?

    我们知道.NET CLR的GC堆中有一种特殊的堆,它专门存放超过85000byte的对象(详见这里),这就是大对象堆(LOH). 在.NET Framework 4.5.1之前,微软并没有提供对LOH ...

  8. php开发必备小工具

    /*递归删除目录及目录下的文件*/ function del_dir($dir){ $files = new DirectoryIterator($dir); foreach ($files as $ ...

  9. WHMCS成功安装和使用方法及添加支付宝,PayPal收款教程

    一.WHMCS安装前准备 1.WHMCS官网: 1.官方首页:http://www.whmcs.com/ 2.WHMCS需要安装在一个带MysqL数据库的PHP服务器中,一般地我们日常安装的VPS控制 ...

  10. MVC模式与Android

    MVC模式是软件工程中的一种软件架构,“Model-View-Controller”的缩写,中文翻译为“模型-视图-控制器”. MVC模式将一个交互式应用程序分为3各组件: 1.Model(模型):业 ...