先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!)。如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 选 编号小的。

时间限制
200 ms
内存限制
65536 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
 #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)的更多相关文章

  1. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 1072. Gas Station (30) 多源最短路

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  6. 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 ...

  7. 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 ...

  8. PAT (Advanced Level) 1072. Gas Station (30)

    枚举一下选的位置,每次算一下就可以了. #include<cstdio> #include<cstring> #include<cmath> #include< ...

  9. PAT甲题题解-1072. Gas Station (30)-dijkstra最短路

    题意:从m个加油站里面选取1个站点,使得其离住宅的最近距离mindis尽可能地远,并且离所有住宅的距离都在服务范围ds之内.如果有很多相同mindis的加油站,输出距所有住宅平均距离最小的那个.如果平 ...

随机推荐

  1. css笔记17:盒子模型加强版的案例

    1.先看看经典案例效果图,导出思路: 分析:思路 基本结构 <div> <ul> <li><img src=""/> </li ...

  2. Android(java)学习笔记96:如何改变spinner系统自带的字体和颜色

    1.首先我们要知道spinner系统自带字体和颜色本质: 原生的Spring 控件是无法更改字体和颜色的... 从下面的代码可以看出...红色的标注显示使用的是Android默认的布局.. Spinn ...

  3. ASP.NET MVC and jqGrid 学习笔记 1-基本配置

    新建一个mvc项目后

  4. Apache配置文件中的deny与allow小结

    今天在公司配置Zend本地Apache环境的时候,发现在zend.conf中的权限控制中的几段句子,如下所示: 复制代码代码如下: <Location /server-status>    ...

  5. Hibernate的几种主键生成策略

    主键类型: 业务主键(natural key):业务主键的值是来源于一个业务数据. 代理主键(surrogate key):代理主键需要采用一种方式来生成某个唯一值. 代理主键的生成策略: 1.hib ...

  6. WampServer下修改和重置MySQL密码(转)

    转自:www.2cto.com/database/201504/387589.html WampServer安装后密码是空的, 修改一般有两种方式: 一是通过phpMyAdmin直接修改: 二是使用W ...

  7. Redux你的Angular 2应用--ngRx使用体验

    Angular2和Rx的相关知识可以看我的Angular 2.0 从0到1系列第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2 ...

  8. windows下cmd命令行显示UTF8字符设置(CHCP命令)

    本文由 www.169it.com 收集整理 在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况下 ...

  9. Memcached学习(一)

    1.Memcached是什么? 引用维基百科上得简介,Memcached 是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,目前已被诸如Facebook等许多 ...

  10. Unity User Group 北京站图文报道:《Unity3D VR游戏与应用开发》

    很高兴,能有机会回报Unity技术社区:我和雨松MOMO担任UUG北京站的负责人, 组织Unity技术交流和分享活动. 本次北京UUG活动场地–微软大厦 成功的UUG离不开默默无闻的付出:提前2小时到 ...