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 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的更多相关文章

  1. [LeetCode] Gas Station 加油站问题

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

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

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

  4. 【leetcode】Gas Station

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  5. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  6. 20. Candy && Gas Station

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  7. LeetCode——Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  8. Gas Station

    Description: There are N gas stations along a circular route, where the amount of gas at station i i ...

  9. Gas Station [LeetCode]

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

随机推荐

  1. 求正整数n所有可能的和式的组合(如;4=1+1+1+1、1+1+2、1+3、2+1+1、2+2

    作者:张小二 nyoj90 ,可以使用递归的方式直接计算个数,也可以通过把满足的个数求出来计数,因为在juLy博客上看到整数划分,所以重写了这个代码,就是列出所m的可能性,提交后正确.acmer的入门 ...

  2. wuzhicms刷新按钮的功能开发

    这个刷新按钮可以刷新当前框架的页面. 但有的页面使用了弹窗打开后,再点击刷新就会打开之前的弹窗页面. 如: 再刷新的时候,这个框架内容就变了.而这里,我们实际需要刷新的是列表页面 打开这个程序的具体文 ...

  3. cloudstack安装篇3-SELinux配置、NTP时间同步、配置ClouStack软件库

    一.SELinux配置 为了让CloudStack正常工作,我们必须将SELinux设置为permissive.需要在当前系统运行状态下和启动后都能够生效,进行以下配置. 在系统运行状态下的将SELi ...

  4. SQL Server Data Tool 嘹解(了解)一下 SSDT -摘自网络

    Visual Studio 2010 的伺服器管理員可以用來連接 Sharepoint.伺服器,還可以透過資料連接連結至 SQL Server  等資料來源.以資料來源為例,您可以利用單一工具(Vis ...

  5. POJ1182食物链 (并查集)

    第一反应就是和那个搞基的虫子的题很像(poj2492 http://www.cnblogs.com/wenruo/p/4658874.html),不过是把种类从2变成了3. 错在很白痴的地方,卡了好久 ...

  6. 教程-隐藏/显示任务栏-程序不在任务显示-全面控制Windows

    1.隐藏任务条 var  h:THandle; //变量h:=FindWindow('Shell_TrayWnd',nil);ShowWindow(h,SW_hide); 2.显示任务条h:=Find ...

  7. 关于easyui模拟win2012桌面的一个例子系列

    最近时间比较充裕,想到之前领导问我,什么界面更适合公司这种屏幕小但是又要求可以同时处理更多的工作. 我感觉  windows是最合适的,毕竟微软已经做了这么多年的系统了,人的操作习惯已经被他们确定了. ...

  8. java常见算法

    1.冒泡排序 public int[] bubbleSort(int arr){ int temp; boolean isOk; for(int i = 0; i < arr.length; i ...

  9. 发布ASP(非.Net)网站

    1.安装IIS 2.设置网址.端口 3.设置文档(默认访问的文档,比如index.asp,index.htm等) 4.双击asp - 展开行为 - 启用父路径:true - 允许访问父目录 5.应用程 ...

  10. spring问题org.springframework.beans.factory.CannotLoadBeanClassException

    1.看jdk是否配置正确 2.把MyEclipse里面的那个classes删除让他重新编译就没什么问题了,只要配置是对的 3.重新clean project