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< ...
随机推荐
- Windows 系统cmd设置添加静态路由方式
电脑上添加静态路由,cmd设置路由 方法/步骤 1.首先在“运行”窗口输入cmd(按WIN+R打开运行窗口),然后回车进入命令行,输入 route add 10.253.251.0 mask ...
- [sqlite] 数据库遇到的问题 “该字符串未被识别为有效的 DateTime”
异常详细信息: System.FormatException: 该字符串未被识别为有效的 DateTime. 解决方案: 在日期保存到Sqlite数据库时转换一个类型,比如:string _now = ...
- CMDB Autoclient思路分析
1.start.py里的script.run():执行run函数--> 2.script.py run方法--> 3.判断模式MODE(Agent/SSHSALT)-->4.执行cl ...
- (转载)Android学习笔记⑨——android.content.ActivityNotFoundException异常处理
异常1:Java.lang.ClassNotFoundException 08-13 18:29:22.924: E/AndroidRuntime(1875):Caused by: Java.lang ...
- Linux下常用压缩、解压缩命令
常用的压缩工具 一是单纯的单文件压缩工具,主要有compress,gzip,bzip2. 二是打包压缩工具,也是最常用的,tar 压缩比:compress<gzip<bzip2 co ...
- PostgreSQL CheckPoint设置(转)
今天在研究checkpoint process的问题时,顺便复习了一下checkpoint设置问题,又有新的疑惑了. checkpoint又名检查点,在oracle中checkpoint的发生意味着之 ...
- 动态加载vs静态加载
动态加载: 1:灵活,可以在需要的时候用LoadLibrary进行加载,在不需要的时候用FreeLibrary进行卸载,这样可以不必占用内存. 2:可以在没有dll时候发现,而不致程序报错. 3:加载 ...
- functools 和 itertools
functools 补充 1 wraps 在编写装饰器时,在实现前加入 @functools.wraps(func) 可以保证装饰器不会对被装饰函数造成影响.wraps 保存被装饰函数的原信息 def ...
- DHL学习--<asp:literal
<asp:literal ID="ltlJS" runat="server"></asp:literal> 标签的Text属性可以放J ...
- 《Drools7.0.0.Final规则引擎教程》第4章 4.6 结果条件
结果条件 在Java中,如果有重复的代码我们会考虑进行重构,抽取公共方法或继承父类,以减少相同的代码在多处出现,达到代码的最优管理和不必要的麻烦.Drools同样提供了类似的功能.下面我们以实例来逐步 ...