zoukankan      html  css  js  c++  java
  • 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 (≤), the total number of houses; M (≤), the total number of the candidate locations for the gas stations; K (≤), 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<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    #define  inf  0x3fffffff
    int e[maxn][maxn],n,m,k,d;
    int dist[maxn];
    bool vis[maxn];
    double sum[maxn];
    int main(){
        scanf("%d %d %d %d",&n,&m,&k,&d);
        fill(e[0],e[0]+maxn*maxn,inf);
        fill(dist,dist+maxn,inf);
        fill(vis,vis+maxn,false);
        fill(sum,sum+maxn,false);
        string a,b;
        int c,cnt=0;
        for(int i=0;i<k;i++){
            cin>>a>>b;
            cin>>c;
            int a1,b1;
            if(a[0]=='G'){
                a=a.substr(1);
                a1=a[0]-'0'+n;
            }
            else{
                a1=a[0]-'0';
            }
            if(b[0]=='G'){
                b=b.substr(1);
                b1=b[0]-'0'+n;
            }
            else{
                b1=b[0]-'0';
            }
            e[a1][b1]=e[b1][a1]=c;
        }
        for(int t=1;t<=m;t++){
            fill(dist,dist+maxn,inf);
            fill(vis,vis+maxn,false);
            dist[n+t]=0;
            for(int i=1;i<=n+m;i++){
                if(e[n+t][i]<inf){
                    dist[i]=e[n+t][i];
                }
                if(e[i][n+t]<inf){
                    dist[i]=e[i][n+t];
                }
            }
            for(int i=0;i<n+m;i++){
                int mind=inf,minv=-1;
                for(int j=1;j<=n+m;j++){
                    if(vis[j]==false&&dist[j]<inf){
                        if(dist[j]<mind){
                            mind=dist[j];
                            minv=j;
                        }
                    }
                }
                int u=minv;
                vis[u]=true;
                for(int j=1;j<=n+m;j++){
                    if(dist[u]+e[u][j]<dist[j]&&e[u][j]!=inf){
                        dist[j]=dist[u]+e[u][j];
                    }
                }
            }
    
            int flag=0;
            for(int i=1;i<=n;i++){
                if(dist[i]>d){
                    flag=1;
                    cnt++;
                    break;
                }
            }
            if(flag==0){
                int mindist=inf;
                printf("G%d
    ",t);
                sum[t]=0;
                for(int i=1;i<n;i++){
                    if(dist[i]<mindist){
                        mindist=dist[i];
                    }
                    sum[t]+=dist[i];
                }
                sum[t]=sum[t]/n;
                printf("%.1f %.1f
    ",mindist*1.0,sum[t]);
            }
        }
        if(cnt==m){
            printf("No Solution
    ");
        }
    
    
        return 0;
    }

     后续再改

  • 相关阅读:
    IOS 11 通讯录手机号「隐形字符」的 Bug
    本地添加const参数 防止短信接口恶意调用
    javascript阿拉伯数字 转 汉字中文数字
    js去掉数组的空字符串
    Chrome 清除某个特定网站下的缓存
    vue-cli中的babel配置文件.babelrc详解
    提交到github报错Please tell me who you are
    跨域问题
    js单线程、js任务队列、异步操作
    Java 异常
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14428044.html
Copyright © 2011-2022 走看看