PAT甲级——1072 Gas Station
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 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 G
1 to G
M.
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 ;
}
PAT甲级——1072 Gas Station的更多相关文章
- pat 甲级 1072. Gas Station (30)
1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...
- 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 ...
- 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 ...
- PAT 1072 Gas Station[图论][难]
1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...
- 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 ...
- PAT 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- PAT 1072. Gas Station
A gas station has to be built at such a location that the minimum distance between the station and a ...
- 1072. Gas Station (30)
先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...
- 1072. Gas Station (30) 多源最短路
A gas station has to be built at such a location that the minimum distance between the station and a ...
随机推荐
- 2018今日头条湖北省赛【D】
[题目链接]https://www.nowcoder.com/acm/contest/104/C 不知道这题为啥没过.队友现场推的都是对的..233333好像代码写的有问题,下来就很恼火. 题意大概就 ...
- neo4j采坑记
1.安装后启动不起来,解决方案: https://stackoverflow.com/questions/38607283/failed-to-start-neo4j-service 2.一直启动不 ...
- sql (6) exists
SQL中EXISTS的用法 查询所有选修了1号课程的学生的姓名,年龄,性别首先取Student表中的一个元组,然后在SC表中依次找SC.Sno=该元组的So,并且对应的Cno='2',如果存在,则外层 ...
- CSIC_716_20191101【编程语言、变量、垃圾回收机制】
编程语言分类:机器语言.汇编语言.高级语言. 机器语言:机器能直接识别的程序语言或指令代码(二进制指令),勿需经过翻译,每一操作码在计算机内部都有相应的电路来完成它 汇编语言:比机器语言略高级,用英文 ...
- [JZOJ2865]【集训队互测 2012】Attack
题目 题目大意 平面上有一堆带权值的点.两种操作:交换两个点的权值,查找一个矩形的第\(k\)小 \(N<=60000\) \(M<=10000\) \(10000ms\) 思考历程&am ...
- .net下MVC中使用Tuple分页查询数据
主要是在DAL层写查询分页的代码. 例如DAL层上代码: public Tuple<List<WxBindDto>, int> GetMbersInfo(int start, ...
- kubernetes istio之流量管理
1.部署 Bookinfo 应用 要在 Istio 中运行这一应用,无需对应用自身做出任何改变.我们只要简单的在 Istio 环境中对服务进行配置和运行,具体一点说就是把 Envoy sidecar ...
- 解决k8s gcr.io被墙下载不了镜像的问题
gcr.io镜像 根据开源项目: https://github.com/anjia0532/gcr.io_mirror 作者将gcr.io相关镜像pull下来,然后push到docker官方仓库htt ...
- Spring Cloud Config的配置中心使用非对称性加密
首先,我们需要通过keytool工具来生成密钥对. keytool是JDK中的一个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认 ...
- <每日一题>题目3:编写装饰器,为多个函数加上记录调用功能,要求每次调用函数都将被调用的函数名称写入文件
def log(func): def inner(*args,**kwargs): with open('log',mode='a',encoding='utf-8') as f: #以追加的方式打开 ...