zoukankan      html  css  js  c++  java
  • PAT 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~S~, 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

    题目大意:找出每个加油站到所有用户的最小距离,找出最小距离最大的加油站;
    注意点:这一题要维护的变量太多了, 所以取号变量名就比较重要了
    节点是否访问的状态是在循环内改变的,不应该在循环外面改变
    判断是否有解决方案,是判断离加油站最远的用户距离是否在加油站的服务范围之类
    好像犯了类似的错误啊, 最开始把起点和重点设置为长度为3的字符数组, 题中说用户可能有1000个,所以字符串应该开到5; 不知道为啥把字符数组转换成数字要出错, 用string比较方便, 测试点最多1000个不会超时;

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<vector>
     4 #include<cstring>
     5 using namespace std;
     6 int n, m, k, MaxDis;
     7 const int inf = 99999999;
     8 vector<vector<int> > G(1020, vector<int>(1020, inf));
     9 vector<int> vis(1020, false), dis(1020, inf);
    10 
    11 int index(string s){
    12   int ans=0;
    13   if(s[0]=='G'){
    14     s = s.substr(1);
    15     ans = n +stoi(s);
    16   }
    17   else ans = stoi(s);
    18   return ans;
    19 }
    20 int main(){
    21   scanf("%d%d%d%d", &n, &m, &k, &MaxDis);
    22   int i;
    23   for(i=0; i<k; i++){
    24     string a, b;
    25     int length, idx1, idx2;
    26     cin>>a>>b;
    27     scanf("%d", &length);
    28     idx1 = index(a);
    29     idx2 = index(b);
    30     G[idx1][idx2] = G[idx2][idx1] = length;
    31   }
    32   double finalMinDis = 0, finalAvg=inf;
    33   int gIdx;
    34   for(i=1; i <= m; i++){
    35     fill(vis.begin(), vis.end(), false);
    36       fill(dis.begin(), dis.end(), inf);
    37     dis[n+i] = 0;
    38     double minDis=inf, totalDis=0, maxDis=0;
    39     for(int j=1; j <= m+n; j++){
    40       int minn = inf, u = -1;
    41       for(int idx=1; idx <= m+n; idx++){
    42         if(!vis[idx] && dis[idx] < minn){
    43           minn = dis[idx];
    44           u = idx;
    45         }
    46       }
    47       if(u==-1) break;
    48       vis[u] = true;
    49       for(int v=1; v<=n+m; v++){
    50         if(!vis[v] && G[u][v]!=inf){
    51           if(dis[v]>dis[u]+G[u][v]){
    52             dis[v]=dis[u]+G[u][v];
    53           }
    54         }
    55       }
    56     }
    57     for(int s=1; s<=n; s++) {
    58         totalDis += dis[s];
    59         if(dis[s]>maxDis) maxDis = dis[s];
    60         if(dis[s]<minDis) minDis = dis[s];
    61     }
    62     double tempAvg = totalDis*1.0/n;
    63     if(minDis>finalMinDis && maxDis<=MaxDis){
    64       finalMinDis = minDis;
    65       finalAvg = tempAvg;
    66       gIdx = i;
    67     }else if(minDis==finalMinDis){
    68       if(tempAvg<finalAvg){
    69             finalMinDis = minDis;
    70         finalAvg = tempAvg;
    71         gIdx = i;
    72       }
    73     }
    74   }
    75   if(finalMinDis!=0) printf("G%d
    %.1f %.1f
    ", gIdx, finalMinDis, finalAvg);
    76   else printf("No Solution
    ");
    77   return 0;
    78 }


  • 相关阅读:
    随手练——几个递归小题目
    随手练——USACO 1.44 母亲的牛奶
    随手练——汉诺塔问题(递归典型)
    Linux搭建lamp(Apache+PHP+Mysql环境)centos7.2版详细教程
    Windows 环境下安装redis 及其PHP Redis扩展
    Windows系统如何安装Redis
    phpstorm配置成sublime的代码高亮逼格风格
    phpstorm-----实现实时编辑服务器代码
    如何在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作
    phpstorm取消自动保存,修改快捷键并标识修改的文件为星星标记
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9239537.html
Copyright © 2011-2022 走看看