博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT_1072 Gas Station
阅读量:5249 次
发布时间:2019-06-14

本文共 7682 字,大约阅读时间需要 25 分钟。

1072. Gas Station (30)

时间限制 
200 ms
内存限制 
32000 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 51 2 21 4 21 G1 41 G2 32 3 22 G2 13 4 23 G3 24 G1 3G2 G1 1G3 G2 2
Sample Output 1:
G12.0 3.3
Sample Input 2:
2 1 2 101 G1 92 G1 20
Sample Output 2:
No Solution ==============================src==================================== dijikstra 方法计算图中 单源最短路径
#include 
#include
#include
#include
#include
using namespace std ;const int maxn = 1000 +10 +1 ;struct HeapNode{ int d , u ; bool operator < ( const HeapNode &rhs) const { return d > rhs.d ; }} ;struct Edge{ int from, to ,dist ;} ;struct Dijkstra{ int n , m ; vector
edges ; vector
G[maxn] ; bool done[maxn] ; int d[maxn] ; int p[maxn] ; void init ( int n ) { int i ; this->n = n ; for ( i = 0 ; i < n ; i++ ) { G[i].clear() ; } edges.clear() ; } void AddEdge ( int from , int to , int dist ) { Edge e ; int m ; e.from = from ; e.to = to ; e.dist = dist ; edges.push_back(e) ; m = edges.size() ; G[from].push_back( m-1 ) ; } void dijkstra ( int s ) { priority_queue
Q ; HeapNode node ; for ( int i = 0 ; i < n ; i++ ) d[i] = maxn ; d[s] = 0 ; memset(done , 0 , sizeof(done) ) ; node.d = 0 ; node.u = s ; Q.push( (HeapNode) node) ; while( !Q.empty() ) { node = Q.top () ; Q.pop() ; int u = node.u ; if ( done[u] ) continue ; done[u] = true ; for ( int i = 0 ; i < G[u].size() ; i++ ) { Edge &e = edges[G[u][i]] ; if ( d[e.to] > (d[u] + e.dist) ) { d[e.to] = d[u]+e.dist ; p[e.to] = G[u][i] ; node.d = d[e.to] ; node.u = e.to ; Q.push(node) ; } } } }} ;//set global varsint N , M , K , Ds ;Dijkstra Graph ;int charToInt (char *p ){ int len ; int sum = 0 ; int i ; len = strlen(p) ; if (p[0] == 'G') { for (i= 1 ; i < len ; i++) { sum += (int)(p[i]-'0')*(int)pow(10,len-i-1 ) ; } sum += N ; } else { for (i= 0 ; i < len ; i++) { sum += (int)(p[i]-'0')*(int)pow(10,len-i-1 ) ; } } return sum ;}void Input(){ int i ; char line[3][6] ; int x,y,v ; scanf("%d%d%d%d", &N,&M,&K,&Ds) ; Graph.init(N+M) ; for ( i = 0 ; i < K ; i++ ) { scanf("%s",line[0]) ; scanf("%s",line[1]) ; scanf("%s",line[2]) ; x = charToInt(line[0]) ; y = charToInt(line[1]) ; v = charToInt(line[2]) ; Graph.AddEdge(x-1 ,y-1 ,v ) ; Graph.AddEdge(y-1 ,x-1 , v ) ; }}int main ( void ){ int i , j ; double min; bool flag = true ; double sum = 0 ; Input() ; for ( i = N ; i < N+M ; i++ ) { Graph.dijkstra( i ) ; sum = 0 ; flag = true ; min = maxn ; for ( j = 0 ; j < N ; j++ ) { if ( Graph.d [j] <= Ds ) { sum += Graph.d[j] ; if ( min > Graph.d[j] ) min = Graph.d[j] ; } else { flag = false ; break ; } } if ( flag ) { sum = sum/N ; printf("G%d\n",i+1-N) ; printf("%.1f %.1f", min , sum) ; return 0 ; } } printf("No Solution") ; return 0 ;}

 

 

----------------------------another version----------------------------------------

#include 
#include
#include
#include
#include
using namespace std ;const int maxn = 1000 +10 +1 ;struct HeapNode{ int d , u ; bool operator < ( const HeapNode &rhs) const { return d > rhs.d ; }} ;struct SolutionNode{ double aver, min ; int Gx ; bool operator < ( const SolutionNode &rhs) const { if ( aver > rhs.aver ) return true ; else if ( aver == rhs.aver ) { return Gx > rhs.aver ; } }} ;struct Edge{ int from, to ,dist ;} ;struct Dijkstra{ int n , m ; vector
edges ; vector
G[maxn] ; bool done[maxn] ; int d[maxn] ; int p[maxn] ; void init ( int n ) { int i ; this->n = n ; for ( i = 0 ; i < n ; i++ ) { G[i].clear() ; } edges.clear() ; } void AddEdge ( int from , int to , int dist ) { Edge e ; int m ; e.from = from ; e.to = to ; e.dist = dist ; edges.push_back(e) ; m = edges.size() ; G[from].push_back( m-1 ) ; } void dijkstra ( int s ) { priority_queue
Q ; HeapNode node ; for ( int i = 0 ; i < n ; i++ ) d[i] = maxn ; d[s] = 0 ; memset(done , 0 , sizeof(done) ) ; node.d = 0 ; node.u = s ; Q.push( (HeapNode) node) ; while( !Q.empty() ) { node = Q.top () ; Q.pop() ; int u = node.u ; if ( done[u] ) continue ; done[u] = true ; for ( int i = 0 ; i < G[u].size() ; i++ ) { Edge &e = edges[G[u][i]] ; if ( d[e.to] > (d[u] + e.dist) ) { d[e.to] = d[u]+e.dist ; p[e.to] = G[u][i] ; node.d = d[e.to] ; node.u = e.to ; Q.push(node) ; } } } }} ;//set global varsint N , M , K , Ds ;Dijkstra Graph ;int charToInt (char *p ){ int len ; int sum = 0 ; int i ; len = strlen(p) ; if (p[0] == 'G') { for (i= 1 ; i < len ; i++) { sum += (int)(p[i]-'0')*(int)pow(10,len-i-1 ) ; } sum += N ; } else { for (i= 0 ; i < len ; i++) { sum += (int)(p[i]-'0')*(int)pow(10,len-i-1 ) ; } } return sum ;}void Input(){ int i ; char line[3][6] ; int x,y,v ; scanf("%d%d%d%d", &N,&M,&K,&Ds) ; Graph.init(N+M) ; for ( i = 0 ; i < K ; i++ ) { scanf("%s",line[0]) ; scanf("%s",line[1]) ; scanf("%s",line[2]) ; x = charToInt(line[0]) ; y = charToInt(line[1]) ; v = charToInt(line[2]) ; Graph.AddEdge(x-1 ,y-1 ,v ) ; Graph.AddEdge(y-1 ,x-1 , v ) ; }}void Output() { int i , j ; bool flag ; priority_queue
q ; for ( i = N ; i < N+M ; i++ ) { Graph.dijkstra(i) ; SolutionNode node ; node.aver= 0 ; node.min = maxn ; node.Gx = i-N+1; flag = true ; for ( j = 0 ; j < N ; j++ ) { if ( Graph.d[j] <= Ds ) { node.aver += Graph.d[j] ; if ( node.min > Graph.d[j] ) node.min = Graph.d[j] ; } else { flag = false ; break ; } } if ( flag ) { node.aver = node.aver / N ; q.push(node) ; printf( "now q length :%d\n" , q.size() ) ; } } if ( q.empty() ) { printf("No Solution") ; return ; } else { SolutionNode node = q.top() ; printf("G%d\n", node.Gx) ; printf("%.1f %.1f", node.min , node.aver) ; return ; }}int main ( void ){ Input() ; Output() ; return 0 ; }

个人觉得这道题出的有一点问题,

从题中大意可知,

如果存在着 多个满足 解决方案的 Gx 点的话,

首先 要选取平均距离 为最小的 Gx , 可是从例子可以看出 G1 G2 中平均距离最小的应该是 G2,而并非是 G1。

 

或许是LZ理解的有误,先存档一下, 等有时间再通关。

转载于:https://www.cnblogs.com/inuyasha1027/p/pat_18.html

你可能感兴趣的文章
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
查看>>
java 常用命令
查看>>
CodeForces Round #545 Div.2
查看>>
卷积中的参数
查看>>
51nod1076 (边双连通)
查看>>
Item 9: Avoid Conversion Operators in Your APIs(Effective C#)
查看>>
深入浅出JavaScript(2)—ECMAScript
查看>>
STEP2——《数据分析:企业的贤内助》重点摘要笔记(六)——数据描述
查看>>
ViewPager的onPageChangeListener里面的一些方法参数:
查看>>
Jenkins关闭、重启,Jenkins服务的启动、停止方法。
查看>>
CF E2 - Array and Segments (Hard version) (线段树)
查看>>
Linux SPI总线和设备驱动架构之四:SPI数据传输的队列化
查看>>
SIGPIPE并产生一个信号处理
查看>>
CentOS
查看>>
Linux pipe函数
查看>>
java equals 小记
查看>>
爬虫-通用代码框架
查看>>
2019春 软件工程实践 助教总结
查看>>
YUV 格式的视频呈现
查看>>
Android弹出框的学习
查看>>