zoukankan      html  css  js  c++  java
  • pat 1072

    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 (≤10^3), the total number of houses; M (≤10), the total number of the candidate locations for the gas stations; K (≤10^4), the number of roads connecting the houses and the gas stations; and D , 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

    题意:从m个加油站里面选取1个站点,让他离居民区的最近的人最远,并且没有超出服务范围d之内。如果有很多个最远的加油站,输出距离所有居民区距离平均值最小的那个。如果平均值还是一样,就输出按照顺序排列加油站编号最小的那个

    思路:首先构建图(对于数据的读入,使用char数组,因为会读入G1这样的表示加油站的结点,然后利用hash的思想把结点转换为 int类型的id,方便后续往图里面插入边(这里用的邻接矩阵)),然后dijkstra求单源最短路径,这里的源点是给定的加油站,由于有多个加油站,因此多次调用dijkstra算法(floyd算法可以很方便的求解,但是floyd算出所有点之间的最短路径,时间复杂度是V^3,最后一个点会超时,下面我也给出floyd算法的解)。然后求所有路径中最符合条件的。

    floyd算法:

     1 #include<cstdio>
     2 #include<algorithm>
     3 #define inf 999999
     4 int n,m,k,d;
     5 using namespace std;
     6 int G[1020][1020];
     7 int getId(char str[]){
     8     int id=0;
     9     if(str[0]=='G'){
    10         id+=n;
    11         return id+str[1]-'0';
    12     }
    13     else
    14         return id+str[0]-'0';
    15 }
    16 void floyd(){
    17     for(int i=1;i<=n+m;i++){
    18         for(int j=1;j<=n+m;j++){
    19             for(int k=0;k<=n+m;k++){
    20                 if(G[j][k]>G[j][i]+G[i][k])
    21                     G[j][k]=G[j][i]+G[i][k];
    22             }
    23         }
    24     }
    25 }
    26 int main(){
    27     fill(G[0],G[0]+1000010,inf);
    28     char stra[5],strb[5];
    29     int a,b,dis;
    30     scanf("%d%d%d%d",&n,&m,&k,&d);
    31     for(int i=0;i<k;i++){
    32         scanf("%s %s %d",stra,strb,&dis);
    33         a=getId(stra);
    34         b=getId(strb);
    35         G[a][b]=G[b][a]=dis;
    36     }
    37     floyd();
    38     int mindist=inf;
    39     int minG=-1;
    40     int max=-1;
    41     for(int i=n+1;i<=n+m;i++){
    42         int sum=0;
    43         int min=inf;
    44         for(int q=1;q<=n;q++){
    45             if(G[i][q]<min)
    46                 min=G[i][q];
    47         }
    48         if(min>max){
    49             int mark=0;
    50             for(int j=1;j<=n;j++){
    51                 if(G[i][j]>d){
    52                     mark=1;
    53                     break;
    54                 }
    55                 sum+=G[i][j];
    56             }
    57             if(mark==0){
    58                 minG=i;
    59                 mindist=sum;
    60                 max=min;
    61             }
    62         }
    63         else if(min==max){
    64             int mark=0;
    65             for(int j=1;j<=n;j++){
    66                 if(G[i][j]>d){
    67                     break;    
    68                     mark=1;
    69                 }
    70                 sum+=G[i][j];
    71             }
    72             if(mark==0){
    73                 if(sum<mindist){
    74                     mindist=sum;
    75                     minG=i;
    76                 }
    77             }
    78         }
    79         
    80         
    81 
    82     }
    83     if(minG==-1){
    84         printf("No Solution");
    85         return 0;
    86     }
    87     int min=inf;
    88     for(int i=1;i<=n;i++){
    89         if(min>G[minG][i]){
    90             min=G[minG][i];
    91         }
    92             
    93     }
    94     printf("G%d
    ",minG-n);
    95     printf("%.1lf %.1lf",min*1.0,(mindist+0.01)/n);
    96     return 0;
    97 } 
  • 相关阅读:
    Linux 忘记密码解决方法
    Linux 系统目录结构
    【Linux】正确的关机方法
    CentOS7设置系统/yum以及firefox web代理上网
    怎样安装CentOS 6.6之三:磁盘分区的划分和修改
    在虚拟机(VMware)中安装Linux CentOS 6.4系统(图解) 转
    LiveCD、LiveDVD和BinDVD区别在哪里
    关于对FLASH开发,starling、starling feathers、starling MVC框架的理解
    【 D3.js 进阶系列 — 1.0 】 CSV 表格文件的读取
    javascript实现代码高亮-wangHighLighter.js
  • 原文地址:https://www.cnblogs.com/foodie-nils/p/13153879.html
Copyright © 2011-2022 走看看