【PAT 1072】 Gas Station 最短路径Dijsktra

2024-04-05 06:18

本文主要是介绍【PAT 1072】 Gas Station 最短路径Dijsktra,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1072. Gas Station (30)

时间限制
200 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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


题意

给出一个图,其中有 N <= 103 个节点是居民房,M <= 10 个节点是计划建造加油站的候选点。给出加油站所能服务的最远距离 D。要求计算出合适的位置建造加油站,满足如下优先级条件:

  1. 所有居民房必须在加油站的服务距离内。
  2. 所有居民房中距离加油站的最近的居民房与加油站之间的距离是最远的。(大概是安全方面的考虑,加油站要离居民区远一点)
  3. 所有房间距离加油站的最小距离的总和最小。(节约居民加油的总体成本)
  4. 同等条件下,序号越小的加油站优先。
分析

实际上是求加油站到所有点的最短路径的问题,使用 Dijsktra 可以满足。

另外,需要考虑求最短路径的过程中是否要将其他加油站所构建的路径算入在内

代码

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <iomanip>
using namespace std;//此代码使用前,需删除下面两行+后面的system("PAUSE")
ifstream fin("in.txt");
#define cin finstruct Res{int index;int mm;int sum;Res(int i,int m,int s):index(i),mm(m),sum(s){}
};const int INF = 0x7fffffff;
const int NUM = 1000+10+2;int dis[NUM][NUM]={0};
int minDis[11][NUM]={0};
int n,m,k,ds;
bool visited[NUM];int calcIndex(const char p[]){if(p[0]=='G'){return n+p[1]-'0';}else{return p[0]-'0';}
}void Dijkastra(int centre){memset(visited,false,sizeof(bool)*NUM);int cur = n+1+centre;int i,next,count,t;int mm;count = 0;while(count < n+m-1)		//前提是 图为整个连通图{visited[cur] = true;mm = INF;for(i=1;i<n+m+1;i++){if(visited[i])continue;if(dis[cur][i]){			//若节点cur到i存在通路t = minDis[centre][cur] + dis[cur][i];if(t < minDis[centre][i] || minDis[centre][i]==0){minDis[centre][i] = t;		//更新centre到i的最短路径值}}if(minDis[centre][i] < mm && minDis[centre][i]!=0){mm = minDis[centre][i];next = i;}}cur = next;minDis[centre][cur] = mm;count ++;}
}bool cmp(const Res& aa,const Res& bb){if(aa.mm != bb.mm){return aa.mm > bb.mm;}else if(aa.sum != bb.sum){return aa.sum < bb.sum;}else{return aa.index < bb.index;}
}int main()
{cin>>n>>m>>k>>ds;int i;char p1[3],p2[3]; int index1,index2;for(i=0;i<k;i++){cin>>p1>>p2;index1 = calcIndex(p1);index2 = calcIndex(p2);cin>>dis[index1][index2];dis[index2][index1] = dis[index1][index2];}int j;int mm,sum;vector<Res> vec;for(i=0;i<m;i++){Dijkastra(i);				//以加油站i为源点,进行最短路径遍历mm = INF;sum = 0;bool isOK = true;for(j=1;j<n+1;j++){if(minDis[i][j] > ds){		//若加油站距居民 距离超出 服务范围,直接pass掉这个方案isOK = false;break;}else{sum += minDis[i][j];if(minDis[i][j] < mm) mm = minDis[i][j];}}if(isOK)vec.push_back(Res(i,mm,sum));}if(vec.size()==0){cout<<"No Solution"<<endl;}else{sort(vec.begin(),vec.end(),cmp);	//排序,规则①最短距离mm最大②距离和sum最小③序号index最小cout<<'G'<<vec[0].index+1<<endl;cout<<setiosflags(ios::fixed)<<setprecision(1)<<float(vec[0].mm)<<" "<<float(vec[0].sum)/n<<endl;}system( "PAUSE");return 0;
}

最后一个Case不过,不知是哪里没考虑到。


这篇关于【PAT 1072】 Gas Station 最短路径Dijsktra的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/877824

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

VSCode设置python SDK路径的实现步骤

《VSCode设置pythonSDK路径的实现步骤》本文主要介绍了VSCode设置pythonSDK路径的实现步骤,包括命令面板切换、settings.json配置、环境变量及虚拟环境处理,具有一定... 目录一、通过命令面板快速切换(推荐方法)二、通过 settings.json 配置(项目级/全局)三、

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)

《如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)》:本文主要介绍如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)问题,具有很好的参考价值,希望对大家有所帮助,如有... 目录先在你打算存放的地方建四个文件夹更改这四个路径就可以修改默认虚拟内存分页js文件的位置接下来从高级-

一文详解如何查看本地MySQL的安装路径

《一文详解如何查看本地MySQL的安装路径》本地安装MySQL对于初学者或者开发人员来说是一项基础技能,但在安装过程中可能会遇到各种问题,:本文主要介绍如何查看本地MySQL安装路径的相关资料,需... 目录1. 如何查看本地mysql的安装路径1.1. 方法1:通过查询本地服务1.2. 方法2:通过MyS

Python如何调用指定路径的模块

《Python如何调用指定路径的模块》要在Python中调用指定路径的模块,可以使用sys.path.append,importlib.util.spec_from_file_location和exe... 目录一、sys.path.append() 方法1. 方法简介2. 使用示例3. 注意事项二、imp