• pat1072. Gas Station (30)


    1072. Gas Station (30)

    时间限制
    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
    

    提交代码

    居民点集合A,加油站预备点集合B。就是在B中找到一点加油点C,满足

    1.C到A中任一点的距离不大于Ds。

    2.C到A中任一点的距离的最小值要是 B集合中任意满足1的加油点到A中任一点距离的最小值 的最大值。

    不满足以上两点,即输出“No Solution”。

      1 #include<cstdio>
      2 #include<stack>
      3 #include<algorithm>
      4 #include<iostream>
      5 #include<stack>
      6 #include<set>
      7 #include<map>
      8 #include<vector>
      9 #include<cstring>
     10 using namespace std;
     11 map<int,vector<pair<int,int> > > edge;
     12 bool vis[1015];
     13 int dist[1015];
     14 int trans(string s,int n){
     15     int sum=0,i=0;
     16     if(s[0]=='G'){
     17         i++;
     18         for(;i<s.length();i++){
     19             sum*=10;
     20             sum+=s[i]-'0';
     21         }
     22         sum+=n;
     23     }
     24     else{
     25         for(;i<s.length();i++){
     26             sum*=10;
     27             sum+=s[i]-'0';
     28         }
     29     }
     30     return sum;
     31 }
     32 int main(){
     33     //freopen("D:\INPUT.txt","r",stdin);
     34     int n,m,k,Ds;
     35     scanf("%d %d %d %d",&n,&m,&k,&Ds);
     36     int i,j,l;
     37     string p1,p2;
     38     int dis,u,v;
     39     for(i=0;i<k;i++){
     40         cin>>p1>>p2;
     41         scanf("%d",&dis);
     42         u=trans(p1,n);
     43         v=trans(p2,n);
     44 
     45         //cout<<"u: "<<u<<endl;
     46         //cout<<"v: "<<v<<endl;
     47 
     48         edge[u].push_back(make_pair(v,dis));
     49         edge[v].push_back(make_pair(u,dis));
     50     }
     51     int count=n+m,finminEgdedis=-1,minEgdedis,fintotaldis,totaldis,amount,cur,finp;
     52     vector<pair<int,int> >::iterator it;
     53     for(i=n+1;i<=count;i++){        //dijstra
     54         memset(dist,-1,sizeof(dist));
     55         memset(vis,false,sizeof(vis));
     56         cur=i;
     57         dist[cur]=0;
     58         totaldis=0;
     59         amount=n;//居民居住点个数
     60         minEgdedis=-1;
     61         for(j=1;j<count;j++){
     62             vis[cur]=true;//标记
     63             for(it=edge[cur].begin();it!=edge[cur].end();it++){//邻接表
     64                 if(!vis[it->first]&&(dist[it->first]==-1||dist[it->first]>dist[cur]+it->second)){
     65                 //不在结果集合内且可以更新
     66                 //或刚与和结果集合相连,可以更新
     67                     dist[it->first]=dist[cur]+it->second;
     68                 }
     69             }
     70             int mindis=-1,minnum=-1;
     71             for(l=1;l<=count;l++){//比较,找最小值
     72                 if(!vis[l]&&dist[l]>0&&(mindis==-1||dist[l]<mindis)){
     73                     mindis=dist[l];
     74                     minnum=l;
     75                 }
     76             }
     77             cur=minnum;//准备下一轮
     78             if(cur<=n){//剪枝
     79                 if(dist[cur]>Ds){//存在居民房不在范围内,不满足条件
     80                     break;
     81                 }
     82 
     83                 //cout<<i<<"  "<<j<<" "<<amount<<endl;
     84 
     85                 amount--;
     86                 totaldis+=dist[cur];
     87                 if(minEgdedis==-1||dist[cur]<minEgdedis){
     88                     minEgdedis=dist[cur];
     89                     //cout<<cur<<endl;
     90                 }
     91             }
     92             if(amount==0){//剪枝
     93                 //cout<<amount<<endl;
     94                 break;
     95             }
     96         }
     97         if(amount==0){//居民点统计完毕
     98 
     99             if(finminEgdedis==-1){//第一次
    100                 finminEgdedis=minEgdedis;//最短边
    101                 fintotaldis=totaldis;
    102                 finp=i;
    103                 //cout<<finp<<"  "<<finminEgdedis<<"  "<<fintotaldis<<endl;
    104             }
    105             else{
    106                 if(finminEgdedis<minEgdedis){
    107                     finminEgdedis=minEgdedis;
    108                     fintotaldis=totaldis;
    109                     finp=i;
    110                     //cout<<finp<<"  "<<finminEgdedis<<"  "<<fintotaldis<<endl;
    111                 }
    112                 else{
    113                     if(finminEgdedis==minEgdedis&&totaldis<fintotaldis){
    114                         fintotaldis=totaldis;
    115                         finp=i;
    116                         //cout<<finp<<"  "<<finminEgdedis<<"  "<<fintotaldis<<endl;
    117                     }
    118                 }
    119             }
    120         }
    121     }
    122     if(finminEgdedis==-1){
    123         printf("No Solution
    ");
    124     }
    125     else{
    126         printf("G%d
    ",finp-n);
    127         //cout<<finminEgdedis<<" "<<fintotaldis<<endl;
    128         printf("%.1lf %.1lf",finminEgdedis*1.0,fintotaldis*1.0/n);//printf使用
    129     }
    130     return 0;
    131 }

  • 相关阅读:
    git提交代码
    python把&#DDDDDD转换为中文
    mac下载安装airtest
    mac安装指定版本的python
    python操作habse
    pyspark操作数据库
    Scrapy_redis爬虫项目
    python实现对列表元素是字典的排序
    postman使用
    图书推荐
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4782030.html
Copyright © 2020-2023  润新知