1072. Gas Station (30) 多源最短路
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.
考试的时候这句话意思没有完全理解,悲剧鸟。。。
其实就是让你找一个加油站,它到所有房子最短距离为len , 找一个加油站len的距离越大越好
加油站最多10个,以这几个加油站为原点,做dijk最短路即可
#include<stdio.h> int map[][];
const int MAX=; int use[];
int dis[];
int n,m,k,ds;
void djik(int start){
int i,j,end=n+m;
for(i=;i<=end;i++){
use[i]=;
dis[i]=MAX;
}dis[start]=; for(i=;i<=end;i++){
int min=MAX,rj;
for(j=;j<=end;j++){
if(use[j]==)continue;
if(dis[j]<min){
min=dis[j];
rj=j;
}
}use[rj]=; for(j=;j<=end;j++){ if(dis[j]>dis[rj]+map[rj][j])
dis[j]=dis[rj]+map[rj][j];
}
} for(i=;i<=end;i++){
if(start!=i)
map[start][i]=dis[i];
}
} int main()
{ while(scanf("%d%d%d%d",&n,&m,&k,&ds)!=EOF){
int i,j,end,x,ri;
end=n+m; for(i=;i<=end;i++){
for(j=;j<=end;j++)map[i][j]=MAX;
}
char ss[],ss2[];
int ll,rr,v;
for(i=;i<=k;i++){
scanf("%s",ss);
scanf("%s",ss2);
scanf("%d",&v);
if(ss[]=='G'){
if(ss[]==){
ll=ss[]-''+n;
}else if(ss[]==){
ll=ss[]-''+n;
}else{
ll=+n;
}
}else{
sscanf(ss,"%d",&ll);
} if(ss2[]=='G'){
if(ss2[]==){
rr=ss2[]-''+n;
}else if(ss2[]==){
rr=ss2[]-''+n;
}else{
rr=+n;
}
}else{
sscanf(ss2,"%d",&rr);
} map[ll][rr]=map[rr][ll]=v;
} for(i=n+;i<=end;i++){
djik(i);
} /* for(x=1;x<=end;x++){
for(i=1;i<=end;i++){
for(j=1;j<=end;j++){
if(map[i][x]+map[x][j]<map[i][j])
map[i][j]=map[i][x]+map[x][j];
}
}
}*/ int tempall,all,min=,fail=;
for(i=n+;i<=end;i++){
tempall=;
int tempmin=MAX;
int ok=;
for(j=;j<=n;j++){
if(i==j)continue;
if(map[i][j]>ds)
ok=;
if(ok==)break;
tempall+=map[i][j]; if(tempmin>map[i][j])
tempmin=map[i][j];
}
if(ok==){
fail++;
continue;
} if(tempmin==min){
if(tempall<all){
ri=i;
all=tempall;
}
}
if(tempmin>min){
min=tempmin;
ri=i;
all=tempall;
} // printf("G%
//d\n",ri-n);
// printf("%.1lf %.1lf\n",min*1.0,all*1.0/n);
} if(fail==m){
printf("No Solution\n");
}else{
printf("G%d\n",ri-n);
printf("%.1lf %.1lf\n",min*1.0,all*1.0/n);
}
}
return ;
}
1072. Gas Station (30) 多源最短路的更多相关文章
- 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)
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 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- 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 ...
- 1072. Gas Station (30)
先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...
- 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 (30)-dijkstra最短路
题意:从m个加油站里面选取1个站点,使得其离住宅的最近距离mindis尽可能地远,并且离所有住宅的距离都在服务范围ds之内.如果有很多相同mindis的加油站,输出距所有住宅平均距离最小的那个.如果平 ...
- PAT (Advanced Level) 1072. Gas Station (30)
枚举一下选的位置,每次算一下就可以了. #include<cstdio> #include<cstring> #include<cmath> #include< ...
随机推荐
- Ubuntu下彻底卸载mysql
删除 mysql sudo apt-get autoremove --purge mysql-server-5.0 sudo apt-get remove mysql-server sudo apt- ...
- css实现水平 垂直居中
css实现水平居中 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- div居中和table居中,jQuery获取下拉列表值
一.div居中 margin-left: auto;margin-right: auto; <div style="width:960px ; margin-left: auto;m ...
- Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制
Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...
- ubuntu 14.04安装c++遇到的问题
这是我在安装c++编译器时遇到的一些问题和解决办法,总结一下希望以后的人能少走弯路 安装g++编译器:sudo apt-get install build-essential build-essent ...
- 内存保护机制及绕过方法——通过覆盖部分地址绕过ASLR
ASLR保护机制 ASLR简介 微软在Windows Vista.2008 server.Windows 7.Windows 8等系统的发布中, 开始将ASLR作为内置的系统保护机制运行, 将系统映像 ...
- 对于get方法是否需要synchronized修饰
具体用法没有总结,只是说明一个用法而已,对于以前个人理解出现的偏差 [问题描述] 对于一个计数功能的实现,获取值的方法是否需要加锁? [以前理解] 我只需要在进行累加的方法上进行加锁即可,这样保证其可 ...
- JSP的EL和JSTL解析
1. EL 简介EL 全名为Expression Language,所有EL都是以${ 为起始.以} 为结尾的.EL 语法很简单,它最大的特点就是使用上很方便. 接下来介绍EL 主要的语法结构: ${ ...
- 浅析C#中ASP.NET页面的生存周期
一般来说,页要经历下表概述的各个阶段.除了页生命周期阶段以外,在请求前后还存在应用程序阶段,但是这些阶段并不特定于页. 阶段 说明 页请求 页请求发生在页生命周期开始之前.用户请求页时,ASP.NET ...
- Java基础从头再来?
今天遇到一个就是从后台解析的时候出现null字符串的处理 bug图如下每一个name属性都包含null 对于那些java基础好的直接撸码了,我就是不会哈哈! 最后请教别人还是解决了这个问题 简单分享下 ...