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的加油站,输出距所有住宅平均距离最小的那个.如果平 ...
随机推荐
- Steps to disable DRLs with GM Tech2 scanner
It is possible to get daytime running time disabled manually. But the problem can be easily settled ...
- uva 133 The Dole Queue 双向约瑟夫环 模拟实现
双向约瑟夫环. 数据规模只有20,模拟掉了.(其实公式我还是不太会推,有空得看看) 值得注意的是两个方向找值不是找到一个去掉一个,而是找到后同时去掉. 还有输出也很坑爹! 在这里不得不抱怨下Uva的o ...
- javaweb学习总结九(xml解析以及调整JVM内存大小)
一:解析XML文件的两种方式 1:dom,document object model,文档对象模型. 2:sax,simple API for XML. 3:比较dom和sax解析XML文件的优缺点 ...
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常的解决办法
为了避免这种异常,我们可以使用CopyOnWriteArrayList来代替ArrayList,CopyOnWriteArrayList支持并发访问,所以同时进行迭代和修改是没有问题的.
- hdu 3938 Portal
Portal Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Sub ...
- Ehcache(2.9.x) - API Developer Guide, Searching a Cache
About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...
- Linux 命令 - printenv: 打印全部或部分环境信息
命令格式 printenv [OPTION]... [VARIABLE]... 命令参数 -0, --null 以空字符而非换行符结束每一输出行. --help 显示帮助信息. --version 显 ...
- 第七章 jQuery操作表格及其它应用
1.表格变色 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org ...
- 一个关于如何创建类似于QQ客户端聊天窗口的模拟小项目
对于不久之前学习到的一个有关的类似于QQ聊天框的模拟项目,对其中涉及到的知识在这里做一下总结. 首先,你要先创建一个客户端聊天框(取名为:ChatClient,它是你创建的类),这个类继承了Frame ...
- SignalR 2.0入门
下载已完成的项目 本教程展示如何使用那么 SignalR 创建一个实时聊天应用程序.你会那么 SignalR 添加一个空的 ASP.NET web 应用程序,创建一个 HTML 页面发送并显示消息. ...