先要求出各个加油站 最短的 与任意一房屋之间的 距离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. cocos2d-x之jni使用(对接Android各种sdk)

    游戏弄完了,要发布到各个平台,ios.Android是肯定少不了的,那么本文就来讲讲Android平台对接代理商付费sdk.各渠道.五大运营商.广告.分享.数据统计等等少不了的jni调用,接sdk真是 ...

  2. FTP服务详解

    FTP服务 本章机构 简介 File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文件传输协议".用于Internet上的控制文件的双向传输.同时, ...

  3. 一些网摘的hpc材料

    source from: https://computing.llnl.gov Factors determines a large-scale program's performance 4     ...

  4. [未完成]关于xml文件的解析

    附录一个比较特别的xml文件. MultiNamespaceInlineRequest.xml <?xml version="1.0" encoding="ISO- ...

  5. GDB调试器简介

     Linux系统中包含了GNU 调试程序gdb,它是一个用来调试C和 C++ 程序的调试器.可以使程序开发者在程序运行时观察程序的内部结构和内存的使用情况. GDB提供了一下一些功能: (1)监视程序 ...

  6. Windows 8.1 归档 —— Step 2 对新系统的少量优化

    下面是来自 iplaysoft 的优化技巧:

  7. MVC清除缓存设置+数据验证

    [OutputCache(Location = OutputCacheLocation.None, NoStore = true)] [Table("User")]:定义UserD ...

  8. 【转】APP测试要点

    APP测试的时候,建议让开发打好包APK和IPA安装包,测试人员自己安装应用,进行测试.在测试过程中需要注意的测试点如下: 1.安装和卸载 ●应用是否可以在IOS不同系统版本或android不同系统版 ...

  9. As,is含义?using 语句

    Is:检查对象是否与给定的类型兼容.例如,下面的代码可以确定MyObject类型的一个实例,或者对象是否从MyObject派生的一个类型:        if(obj is MyObject){}   ...

  10. jQuery API 3.1.0 速查表-打印版

    jQuery API 3.1.0 速查表-打印图,(API来自:http://jquery.cuishifeng.cn/index.html)