【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中获取当前文件路径和目录的方法,包括使用__file__关键字、os.path.abspath、os.path.realp... 目录1、获取当前文件路径2、获取当前文件所在目录3、os.path.abspath和os.path.re

hdu2544(单源最短路径)

模板题: //题意:求1到n的最短路径,模板题#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#i

poj 1734 (floyd求最小环并打印路径)

题意: 求图中的一个最小环,并打印路径。 解析: ans 保存最小环长度。 一直wa,最后终于找到原因,inf开太大爆掉了。。。 虽然0x3f3f3f3f用memset好用,但是还是有局限性。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#incl

【408DS算法题】039进阶-判断图中路径是否存在

Index 题目分析实现总结 题目 对于给定的图G,设计函数实现判断G中是否含有从start结点到stop结点的路径。 分析实现 对于图的路径的存在性判断,有两种做法:(本文的实现均基于邻接矩阵存储方式的图) 1.图的BFS BFS的思路相对比较直观——从起始结点出发进行层次遍历,遍历过程中遇到结点i就表示存在路径start->i,故只需判断每个结点i是否就是stop

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

图的最短路径算法——《啊哈!算法》

图的实现方式 邻接矩阵法 int[][] map;// 图的邻接矩阵存储法map = new int[5][5];map[0] = new int[] {0, 1, 2, 3, 4};map[1] = new int[] {1, 0, 2, 6, 4};map[2] = new int[] {2, 999, 0, 3, 999};map[3] = new int[] {3, 7

vcpkg子包路径批量获取

获取vcpkg 子包的路径,并拼接为set(CMAKE_PREFIX_PATH “拼接路径” ) import osdef find_directories_with_subdirs(root_dir):# 构建根目录下的 "packages" 文件夹路径root_packages_dir = os.path.join(root_dir, "packages")# 如果 "packages"

nyoj 1072 我想回家

一道相当题目描述相当扯的题。 这道题目的描述最后说的是求出到达最后一个点的最短距离,所以输入数据最后输入的城堡的坐标是没用的。 就是先求出两点之间的距离,若不大于村落间距离,并且不大于最后的距离限制 l ,则在两点间建边。 最后任意方法求出最短路即可。 #include <iostream>#include<stdio.h>#include<vector>#include<

jmeter依赖jar包找不到类路径

这两天我在纠结这个问题,为啥我maven打包放在jmeter路径下后,jmeter的bean Shell 就是找不到这个类。纠结很久解开了。我记录下,留给后来的朋友。   Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.jmeter.protocol.http.s

运行.bat文件,如何在Dos窗口里面得到该文件的路径

把java代码打包成.jar文件,编写一个.bat文件,执行该文件,编译.jar包;(.bat,.jar放在同一个文件夹下) 运行.bat文件,如何在Dos窗口里面得到该文件的路径,并运行.jar文件: echo 当前盘符:%~d0 echo 当前路径:%cd% echo 当前执行命令行:%0 echo 当前bat文件路径:%~dp0 echo 当前bat文件短路径:%~sdp0 nc