先要求出各个加油站 最短的 与任意一房屋之间的 距离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. 《sift算法详解》阅读笔记

    原博客来自:http://blog.csdn.net/zddblog/article/details/7521424 定义: 尺度不变特征转化是一种计算机视觉算法,用于侦测和描述物体的局部性特征,在空 ...

  2. 【gradle报错】error: package org.apache.http does not exist

    导入项目的时候gradle报错 error: package org.apache.http does not exist 解决方法: 在build.gradle中加入 android {   use ...

  3. 如何定位到append的当前位置,不用拉滚动条scrollIntoView方法

    var bb_mes_con = $('bb_mes_con'); var mes_html = document.createElement('div'); mes_html.setAttribut ...

  4. CF Exam (数学)

     Exam time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...

  5. CF A and B and Team Training (数学)

    A and B and Team Training time limit per test 1 second memory limit per test 256 megabytes input sta ...

  6. 今天写了几个css属性

    <!DOCTYPE html> <html> <head> <meta charset=UTF-8"> <title></t ...

  7. 也说border-box盒模型

    border-box是css3的一个新属性,使用这个属性,和以往的content-box比起来,会有诸多便利之处,bootstrap3也使用的是这个border-box,甚至很多人认为,border- ...

  8. C#保存上传来的图片示例代码

    保存上传图片的方法有很多,在接下来的文章中为大家详细介绍下使用C#是如何做到的,感兴趣的朋友不要错过 复制代码代码如下: [HttpPost]  public string UploadImage() ...

  9. Ajax-jQuery实现

    load方法: 对上一篇中的javascript原生实现(数据格式为html),用jqury的post方法进行改造:  下面这句中“h2 a”,实现了对返回内容的选择:  $.get()\$.post ...

  10. JSP之初识2

    <与%之间不可有空格,但是后面可以有空格 <%@ page language="java" import="java.util.*" pageEnc ...