zoukankan      html  css  js  c++  java
  • 1072. Gas Station (30)

    先要求出各个加油站 最短的 与任意一房屋之间的 距离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

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #include<string.h>
      4 
      5 #define MAX 1050
      6 int INF = 1000000;
      7 
      8 struct Station
      9 {
     10     bool ifis;
     11     double MIN;
     12     double Avg;
     13 };
     14 
     15 int Grap[MAX][MAX];
     16 
     17 Station Sta[11];
     18 
     19 bool visit[MAX];
     20 
     21 int d[MAX];
     22 
     23 void Dijkstra(int begin,int NodeNum)
     24 {
     25     d[begin] = 0;
     26     for(int i = 0;i<NodeNum ;i++)
     27     {
     28         int index = -1;
     29         int MIN = INF;
     30         for(int j = 1;j <= NodeNum ;j++)
     31         {
     32             if(!visit[j] && MIN > d[j])
     33             {
     34                 MIN = d[j];
     35                 index = j;
     36             }
     37         }
     38 
     39         if(index== -1) return ;
     40 
     41         visit[index] = true ;
     42 
     43         for(int v = 1; v <= NodeNum ;v++)
     44         {
     45             if(!visit[v] && Grap[index][v]!=INF)
     46             {
     47                 if(d[index]+ Grap[index][v] < d[v])
     48                 {
     49                     d[v] = d[index]+ Grap[index][v];
     50                 }
     51             }
     52         }
     53     }
     54 }
     55 
     56 int main()
     57 {
     58     int i,j,N,M,K,Ds,Dtem,x,y ;
     59     scanf("%d%d%d%d",&N,&M,&K,&Ds);
     60     char dis1[10],dis2[10];
     61 
     62     for(i = 1 ;i <= N + M ;i++)
     63     {
     64         for(j = 1 ;j <= N + M ;j++)
     65         {
     66             Grap[i][j] = INF ;
     67         }
     68     }
     69 
     70     for(i = 1 ; i<= M ;i++)
     71         Sta[i].ifis = true;
     72 
     73     for( i = 0;i <K;i++)
     74     {
     75         scanf("%s%s%d",dis1,dis2,&Dtem);
     76         if(dis1[0]!='G') x = atoi(dis1);
     77         else
     78         {
     79             char tem[10];
     80             for(j = 1; j <strlen(dis1);j++)
     81                 tem[j-1] = dis1[j];
     82             tem[j-1] = '';
     83             x = atoi(tem) + N;
     84         }
     85 
     86         if(dis2[0]!='G') y = atoi(dis2);
     87         else
     88         {
     89             char tem[10];
     90             for(j = 1; j <strlen(dis2);j++)
     91                 tem[j-1] = dis2[j];
     92             tem[j-1] = '';
     93             y = atoi(tem) + N;
     94         }
     95 
     96         Grap[x][y] = Grap[y][x] = Dtem;
     97     }
     98 
     99     
    100 
    101     
    102 
    103     for(i = N +1 ;i <= N + M ;i++)
    104     {
    105         for(j = 1;j<= N+M ; j ++)
    106         {
    107             d[j] = INF;
    108             visit[j] =false;
    109         }
    110 
    111         Dijkstra(i,N+M);
    112 
    113         int Gas_Min = INF ;
    114         double sum=0;
    115         for(j = 1 ;j <= N ;j++)
    116         {
    117             if(d[j] > Ds ) 
    118             {
    119                 Sta[i-N].ifis = false;
    120                 break;
    121             }
    122             sum += d[j];
    123             if(d[j]< Gas_Min)
    124             {
    125                 Gas_Min = d[j];
    126             }
    127         }
    128 
    129         if(Sta[i-N].ifis)
    130         {
    131             Sta[i-N].Avg = sum /N;
    132             Sta[i-N].MIN = Gas_Min;
    133         }
    134     }
    135 
    136     bool AllNot = true;
    137     double MaxLen= -1;
    138     double MinAvd = INF;
    139     int Gas_index;
    140     for(i = 1 ;i <=  M ;i++)
    141     {
    142         if(Sta[i].ifis)
    143         {
    144             AllNot = false;
    145             if(MaxLen < Sta[i].MIN)
    146             {
    147                 MaxLen = Sta[i].MIN;
    148                 MinAvd = Sta[i].Avg;
    149                 Gas_index = i;
    150             }
    151             else if( MaxLen == Sta[i].MIN && MinAvd > Sta[i].Avg)
    152             {
    153                 MinAvd = Sta[i].Avg;
    154                 Gas_index = i;
    155             }
    156         }
    157     }
    158 
    159     if(AllNot) printf("No Solution
    ");
    160     else
    161     {
    162         printf("G%d
    %0.1lf %0.1lf
    ",Gas_index,Sta[Gas_index].MIN,Sta[Gas_index].Avg);
    163     }
    164 }
  • 相关阅读:
    PHP学习——数组处理函数(一)
    PHP与MySQL的连接
    c语言:<tchar.h>
    PHP数组(二)
    PHP数组(一)
    PHP基础知识(三)
    Git和GitHub
    PHP基础知识(二)
    js 全选和反选(复选框)
    python 字典之删除
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/4292033.html
Copyright © 2011-2022 走看看