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

    题目如下:

     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
    


    题目的要求比较繁琐,一定要认真读题,下面对题意进行概括。

    有N个住宅区、M个加气站,现在要求找出一个加气站,满足下面的要求:

    ①加气站到每个住宅区的距离都≤D

    ②加气站到每个住宅区的最短路径在满足①的所有加气站中最大。

    ③如果②出现了多个解,则找出加气站到所有住宅区距离之和最小的。

    综上所述,这道题的关键就是找出每个加气站到所有住宅区的最短距离,也就是单源最短路径问题,使用Dijkstra算法对每个加气站都进行计算,然后分别存储,最后按照上面的要求筛选即可。

    代码如下:

    #include <iostream>
    #include <vector>
    #include <stdio.h>
    #include <string>
    #include <sstream>
    #include <stdlib.h>
    #include <memory.h>
    
    using namespace std;
    
    #define INF 99999999
    
    struct Arc{
        double dist;
        int num;
    
        Arc(int n, double d){
            num = n;
            dist = d;
        }
    
    };
    
    int N,M,K,D;
    bool* visited;
    vector<vector<double> > minDists;
    vector<vector<Arc> > graph;
    
    int parseVertex(string ver){
        stringstream ss;
        ss << ver;
        int num;
        ss >> num;
        if(num != 0) return num;
        ss.clear();
        char c;
        ss >> c;
        ss >> num;
        return num + N;
    
    }
    
    void Dijkstra(int source){
        memset(visited,false,sizeof(bool)*(1 + N + M));
        minDists[source].resize(1 + N + M);
        for(int i = 1; i < minDists[source].size(); i++){
            minDists[source][i] = INF;
        }
        minDists[source][source] = 0;
        visited[source] = true;
        for(int i = 0; i < graph[source].size(); i++){
            Arc arc = graph[source][i];
            int num = arc.num;
            int dist = arc.dist;
            minDists[source][num] = dist;
        }
        double minDist = INF;
        int v;
        for(int k = 0; k < N + M; k++){
            minDist = INF;
            for(int i = 1; i < minDists[source].size(); i++){
                if(!visited[i] && minDists[source][i] < minDist){
                    minDist = minDists[source][i];
                    v = i;
                }
            }
            if(minDist == INF) break;
            visited[v] = true;
            for(int i = 0; i < graph[v].size(); i++){
                Arc arc = graph[v][i];
                int w = arc.num;
                double dist = arc.dist;
                if(visited[w]) continue;
                if(minDists[source][w] > minDists[source][v] + dist){
                    minDists[source][w] = minDists[source][v] + dist;
                }
            }
    
        }
    
    }
    
    int main()
    {
        cin >> N >> M >> K >> D;
        graph.resize(1 + N + M);
        visited = (bool*)malloc(1 + N + M);
        minDists.resize(1 + N + M);
        string source,dest;
        double dist;
        for(int i = 0; i < K; i++){
            cin >> source >> dest >> dist;
            int sourceInt = parseVertex(source);
            int destInt = parseVertex(dest);
            //printf("<%d %d %d>
    ",sourceInt,destInt,dist);
            graph[sourceInt].push_back(Arc(destInt,dist));
            graph[destInt].push_back(Arc(sourceInt,dist));
        }
        for(int i = 1; i <= M; i++){
            int gasNum = N + i;
            Dijkstra(gasNum);
        }
        int gasNum = -1;
        double gasSum;
        double maxMinDist = -1;
        double minDist = INF;
        double average;
        for(int i = N + 1; i < minDists.size(); i++){
            double sum = 0;
            minDist = INF;
            for(int j = 1; j <= N; j++){
                double dist = minDists[i][j];
                if(dist > D){
                    sum = -1;
                    break;
                }
                sum += dist;
                if(minDist > dist) minDist = dist;
            }
            if(sum == -1) continue;
            if(maxMinDist < minDist){
                gasNum = i - N;
                maxMinDist = minDist;
                gasSum = sum;
                average = sum / N;
            }else if(maxMinDist == minDist){
                if(sum < gasSum){
                    gasNum = i - N;
                    gasSum = sum;
                    average = sum / N;
                }
            }
        }
    
        if(gasNum != -1){
            printf("G%d
    %0.1f %0.1f
    ",gasNum,maxMinDist,average);
        }else{
            printf("No Solution
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    oracel与mysql 查询从创建时间到更新时间的3天内的数据
    微信创建菜单
    利用XStream实现实体类与xml的转换
    play freamwork 框架中 调用线程保存数据
    play freamwork 框架中 设置文件大小
    将javabean转为map类型,然后返回一个map类型的值
    Base64的加密,解密
    MD5加密
    实体类与CDATA类型的xml的转换的工具类
    解决oracle数据库中 ORA-01843: 无效的月份问题
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154076.html
Copyright © 2011-2022 走看看