1072. Gas Station (30)
先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!)。如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 选 编号小的。
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h> #define MAX 1050
int INF = ; struct Station
{
bool ifis;
double MIN;
double Avg;
}; int Grap[MAX][MAX]; Station Sta[]; bool visit[MAX]; int d[MAX]; void Dijkstra(int begin,int NodeNum)
{
d[begin] = ;
for(int i = ;i<NodeNum ;i++)
{
int index = -;
int MIN = INF;
for(int j = ;j <= NodeNum ;j++)
{
if(!visit[j] && MIN > d[j])
{
MIN = d[j];
index = j;
}
} if(index== -) return ; visit[index] = true ; for(int v = ; v <= NodeNum ;v++)
{
if(!visit[v] && Grap[index][v]!=INF)
{
if(d[index]+ Grap[index][v] < d[v])
{
d[v] = d[index]+ Grap[index][v];
}
}
}
}
} int main()
{
int i,j,N,M,K,Ds,Dtem,x,y ;
scanf("%d%d%d%d",&N,&M,&K,&Ds);
char dis1[],dis2[]; for(i = ;i <= N + M ;i++)
{
for(j = ;j <= N + M ;j++)
{
Grap[i][j] = INF ;
}
} for(i = ; i<= M ;i++)
Sta[i].ifis = true; for( i = ;i <K;i++)
{
scanf("%s%s%d",dis1,dis2,&Dtem);
if(dis1[]!='G') x = atoi(dis1);
else
{
char tem[];
for(j = ; j <strlen(dis1);j++)
tem[j-] = dis1[j];
tem[j-] = '\0';
x = atoi(tem) + N;
} if(dis2[]!='G') y = atoi(dis2);
else
{
char tem[];
for(j = ; j <strlen(dis2);j++)
tem[j-] = dis2[j];
tem[j-] = '\0';
y = atoi(tem) + N;
} Grap[x][y] = Grap[y][x] = Dtem;
} for(i = N + ;i <= N + M ;i++)
{
for(j = ;j<= N+M ; j ++)
{
d[j] = INF;
visit[j] =false;
} Dijkstra(i,N+M); int Gas_Min = INF ;
double sum=;
for(j = ;j <= N ;j++)
{
if(d[j] > Ds )
{
Sta[i-N].ifis = false;
break;
}
sum += d[j];
if(d[j]< Gas_Min)
{
Gas_Min = d[j];
}
} if(Sta[i-N].ifis)
{
Sta[i-N].Avg = sum /N;
Sta[i-N].MIN = Gas_Min;
}
} bool AllNot = true;
double MaxLen= -;
double MinAvd = INF;
int Gas_index;
for(i = ;i <= M ;i++)
{
if(Sta[i].ifis)
{
AllNot = false;
if(MaxLen < Sta[i].MIN)
{
MaxLen = Sta[i].MIN;
MinAvd = Sta[i].Avg;
Gas_index = i;
}
else if( MaxLen == Sta[i].MIN && MinAvd > Sta[i].Avg)
{
MinAvd = Sta[i].Avg;
Gas_index = i;
}
}
} if(AllNot) printf("No Solution\n");
else
{
printf("G%d\n%0.1lf %0.1lf\n",Gas_index,Sta[Gas_index].MIN,Sta[Gas_index].Avg);
}
}
1072. Gas Station (30)的更多相关文章
- pat 甲级 1072. Gas Station (30)
1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...
- 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 分)(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) 多源最短路
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 ...
- 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 (Advanced Level) 1072. Gas Station (30)
枚举一下选的位置,每次算一下就可以了. #include<cstdio> #include<cstring> #include<cmath> #include< ...
- PAT甲题题解-1072. Gas Station (30)-dijkstra最短路
题意:从m个加油站里面选取1个站点,使得其离住宅的最近距离mindis尽可能地远,并且离所有住宅的距离都在服务范围ds之内.如果有很多相同mindis的加油站,输出距所有住宅平均距离最小的那个.如果平 ...
随机推荐
- html/css 两个div在同一行
在界面设计的时候,经常需要将两个div在同一行显示. 但是每次都会忘记怎么做,特此随笔,备忘. 如以下要将“第一个div”和“第二个div”显示在同一行: <div id="id1&q ...
- Python mongoDB 的简单操作
#!/usr/bin/env python # coding:utf-8 # Filename:mongodb.py from pymongo import MongoClient,ASCENDING ...
- iOS之duplicate symbols for architecture x86_64错误
在我们写代码过程中可能会经常遇到这样一个错误: <span style="font-size:32px;color:#ff0000;">ld: 4 duplicate ...
- 移动互联网(APP)产品设计的经验分享【转】
随着移动互联网的发展,越来越多的Web产品开始布局移动端,因此最近经常碰到PM们在交流讨论移动APP产品的设计.我从事移动互联网已经有一年多了,通过不断的学习和实践也积累了一些心得,今天整理并分享一下 ...
- mongodb windows下的安装
(1)上mongodb的官网下载windows版本的mongo的安装包,安装包是绿色版的解压出来就可以直接使用. (2)将解压出来的bin文件夹复制到c:\mongoDB下(c:\mongoDB这个文 ...
- 基于BaseHTTPServer的简单存储服务器
服务器代码: from BaseHTTPServer import BaseHTTPRequestHandler from BaseHTTPServer import HTTPServer impor ...
- Linux 源码安装apache 与常见错误解决
文档原位置 一.编译安装apache 1.解决依赖关系 httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级. 升级方式有两种,一种是通过源代码编译安装,一种是直接升 ...
- PPI_network&calc_ppi
# -*- coding: utf-8 -*- # __author__ = 'JieYao' from biocluster.agent import Agent from biocluster.t ...
- 网络编辑器插件ckeditor+ckfinder配置
原帖地址 另外一个 去掉编辑器的下边栏 在config.js中加入: config.removePlugins = 'elementspath'; config.resize_enabled = fa ...
- Quartz Scheduler(2.2.1) - Working with JobStores
About Job Stores JobStores are responsible for keeping track of all the work data you give to the sc ...