PAT_1072 Gas Station
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. 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 ==============================src====================================
dijikstra 方法计算图中 单源最短路径
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <math.h> using namespace std ; const int maxn = + + ;
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 <Edge> edges ;
vector <int> G[maxn] ; bool done[maxn] ;
int d[maxn] ;
int p[maxn] ; void init ( int n )
{
int i ; this->n = n ;
for ( i = ; 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- ) ;
} void dijkstra ( int s )
{
priority_queue <HeapNode> Q ;
HeapNode node ; for ( int i = ; i < n ; i++ )
d[i] = maxn ; d[s] = ; memset(done , , sizeof(done) ) ; node.d = ;
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 = ; 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 vars
int N , M , K , Ds ;
Dijkstra Graph ; int charToInt (char *p )
{
int len ;
int sum = ;
int i ;
len = strlen(p) ; if (p[] == 'G')
{
for (i= ; i < len ; i++)
{
sum += (int)(p[i]-'')*(int)pow(,len-i- ) ;
}
sum += N ;
} else
{ for (i= ; i < len ; i++)
{
sum += (int)(p[i]-'')*(int)pow(,len-i- ) ;
}
}
return sum ;
} void Input()
{
int i ;
char line[][] ;
int x,y,v ; scanf("%d%d%d%d", &N,&M,&K,&Ds) ; Graph.init(N+M) ; for ( i = ; i < K ; i++ )
{
scanf("%s",line[]) ;
scanf("%s",line[]) ;
scanf("%s",line[]) ; x = charToInt(line[]) ;
y = charToInt(line[]) ;
v = charToInt(line[]) ; Graph.AddEdge(x- ,y- ,v ) ;
Graph.AddEdge(y- ,x- , v ) ;
} } int main ( void )
{
int i , j ; double min; bool flag = true ; double sum = ; Input() ; for ( i = N ; i < N+M ; i++ )
{
Graph.dijkstra( i ) ;
sum = ;
flag = true ;
min = maxn ; for ( j = ; 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+-N) ;
printf("%.1f %.1f", min , sum) ; return ;
} } printf("No Solution") ; return ;
}
----------------------------another version----------------------------------------
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <math.h> using namespace std ; const int maxn = + + ;
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 <Edge> edges ;
vector <int> G[maxn] ; bool done[maxn] ;
int d[maxn] ;
int p[maxn] ; void init ( int n )
{
int i ; this->n = n ;
for ( i = ; 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- ) ;
} void dijkstra ( int s )
{
priority_queue <HeapNode> Q ;
HeapNode node ; for ( int i = ; i < n ; i++ )
d[i] = maxn ; d[s] = ; memset(done , , sizeof(done) ) ; node.d = ;
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 = ; 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 vars int N , M , K , Ds ;
Dijkstra Graph ; int charToInt (char *p )
{
int len ;
int sum = ;
int i ;
len = strlen(p) ; if (p[] == 'G')
{
for (i= ; i < len ; i++)
{
sum += (int)(p[i]-'')*(int)pow(,len-i- ) ;
}
sum += N ;
} else
{ for (i= ; i < len ; i++)
{
sum += (int)(p[i]-'')*(int)pow(,len-i- ) ; }
}
return sum ;
} void Input()
{
int i ;
char line[][] ;
int x,y,v ; scanf("%d%d%d%d", &N,&M,&K,&Ds) ; Graph.init(N+M) ; for ( i = ; i < K ; i++ )
{
scanf("%s",line[]) ;
scanf("%s",line[]) ;
scanf("%s",line[]) ; x = charToInt(line[]) ;
y = charToInt(line[]) ;
v = charToInt(line[]) ; Graph.AddEdge(x- ,y- ,v ) ;
Graph.AddEdge(y- ,x- , v ) ;
} } void Output()
{
int i , j ;
bool flag ;
priority_queue<SolutionNode> q ; for ( i = N ; i < N+M ; i++ )
{ Graph.dijkstra(i) ;
SolutionNode node ; node.aver= ;
node.min = maxn ;
node.Gx = i-N+;
flag = true ; for ( j = ; 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 ; }
个人觉得这道题出的有一点问题,
从题中大意可知,
如果存在着 多个满足 解决方案的 Gx 点的话,
首先 要选取平均距离 为最小的 Gx , 可是从例子可以看出 G1 G2 中平均距离最小的应该是 G2,而并非是 G1。
或许是LZ理解的有误,先存档一下, 等有时间再通关。
PAT_1072 Gas Station的更多相关文章
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 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 ...
- Leetcode 134 Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- 20. Candy && Gas Station
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- LeetCode——Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Gas Station
Description: There are N gas stations along a circular route, where the amount of gas at station i i ...
- Gas Station [LeetCode]
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- apple公司的潮起潮落——浪潮之巅
今天代码写不下去的时候,躺在床上看了一下浪潮之巅.翻了一下书目,选了apple公司那一篇. 其实apple公司的事情我已经听过不止一次了,但是每次都是间间断断地听说,都没有系统地了解它到底是经历了怎么 ...
- 动态加载JS脚本的4种方法
实现OPOA(One Page One Application)时,必须使用动态加载js. 也就是在用户选择某个菜单项后,再动态加载对应的全部js到客户端. 动态加载js的情况很多啊,比如解决ajax ...
- Mac Vim 如何设置高亮
首先进入如下目录 cd /usr/share/vim 然后打开vimrc sudo vim vimrc 在vimrc中的“set backspace=2”这行下插入如下代码: set ai " ...
- [读书笔记]算法(Sedgewick著)·第一章(1)
到家放松放松之后就开始学习算法了,手里拿的是拿的是一本Robert Sedgewick的橙皮书<算法(第四版)>的.这本书与导论那本书的不同之处在于轻数学思想.重实现,也就是说这是一本很不 ...
- https原理:证书传递、验证和数据加密、解密过程解析
写的太好了,就是我一直想找的内容,看了这个对https立马明白多了 http://www.cnblogs.com/zhuqil/archive/2012/07/23/2604572.html 我们都知 ...
- devexpress中gridcontrol头部添加垂直线(右边框)
winform开发,用devexpress中的gridcontrol控件,头部默认是3D样式,当客户希望像内容一样扁平化显示且需要添加垂直线(右边框)时恶梦开始了..经过一阵摸索发现可以这样解决: 1 ...
- Sublime Text3使用及常用插件
1.安装packages组件: 参考一: https://sublime.wbond.net/installation 参考二: http://blog.csdn.net/superskk6/arti ...
- 大数据与可靠性会碰撞出什么样的Spark?
可靠性工程领域的可靠性评估,可靠性仿真计算,健康检测与预管理(PHM)技术,可靠性试验,都需要大规模数据来进行支撑才能产生好的效果,以往这些数据都是不全并且收集困难,而随着互联网+的大数据时代的来临, ...
- Redis_基本类型介绍和指令___1
1.Key(键) 主要的方法: ->set key(设置) ->get key(得到) ->del key(删除) ->exitst key(存在) -> expires ...
- 文件正在上传的转圈圈gif图片引出的fixed定位和absolute定位
文件正在上传的转圈圈gif图片 一.文件上传时,未上传返回成功状态之前给个gif动态图片显示在页面,改善用户体验. <!--S 遮罩层 --> <div id="mas ...