1072. Gas Station (30)

本文介绍了一种用于确定城市中最佳加油站位置的算法。该算法确保所有住宅都能获得服务的同时,使加油站与最近住宅的距离尽可能远。通过使用Dijkstra算法计算最短路径并评估不同候选位置,从而找出最佳方案。

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

解题思路:这边存在非数字的图编号,所以要把他们转化成数字编号,都整合到一张图中

例如:Sample1 中有1、2、3、4、G1、G2、G3共7个节点那就把G1、G2、G3转化成5、6、7即可到最后转化回来就可以

算法不难,一看便知求最短路径,就用Dijsktra算法求G1、G2、G3到各个顶点的最短距离储存在distanceL[ ][ ]这个二维数组中

再通过遍历找到每行中最小值,比较各行最小值,求出其中的最大值的行号就是第几个加油站,同时遍历的过程中求和即可得出平均值

AC参考代码:

#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
const int MAX = 999999999;
int N,M,K,D;
int arr[1015][1015];
int distanceL[15][1015];   //每个站点出发的到各个顶点的最短路径记录
int isVisited[1015];
void Dijkstra(int start,int disNum){    //查找最短路径
    int k;
    for(int i=1;i<M+N+1;i++){
        distanceL[disNum][i] = arr[start][i];
    }
    for(int i=1;i<M+N+1;i++){   //把未标记的节点集合放入已表记的节点集合中
        int minRoad = MAX;
        for(int j=1;j<M+N+1;j++){   //找到最短路径节点
           if(!isVisited[j] && distanceL[disNum][j]<minRoad){
                minRoad = distanceL[disNum][j];
                k = j;
            }
        }
        isVisited[k] = 1;
        for(int j=1;j<M+N+1;j++){   //更新新的最短路径数组
            if(distanceL[disNum][k]+arr[k][j]<distanceL[disNum][j]){
                distanceL[disNum][j] = distanceL[disNum][k]+arr[k][j];
            }
        }
    }
}
int char2int(char s[]){     //把字符串转化成整型
    double result = 0;
    int len = strlen(s);
    for(int i=0;i<len;i++){
        result += (s[i]-'0')*pow(10.0,len-i-1);
    }
    return result;
}
int char2intC(char s[]){    //G开头的字符串转化成整型
    double result = 0.0;
    int len = strlen(s);
    for(int i=1;i<len;i++){
        result += (s[i]-'0')*pow(10.0,len-i-1);
    }
    return result;
}
int main()
{
    cin>>N>>M>>K>>D;
    for(int i=0;i<M+N+1;i++){   //路径的默认初始化
        for(int j=0;j<M+N+1;j++){
            if(i==j){
                arr[i][j] = 0;
            }else{
                arr[i][j] = MAX;
            }
        }
    }
    for(int i=0;i<K;i++){
        char a[4],b[4];
        int length,index1,index2;
        cin>>a>>b>>length;
        if(a[0]!='G'){
            index1 = char2int(a);
        }else{
            index1 = char2intC(a)+N;
        }
        if(b[0]!='G'){
            index2 = char2int(b);
        }else{
            index2 = char2intC(b)+N;
        }
        arr[index1][index2] = length;   //初始化真实路径
        arr[index2][index1] = length;
    }//input the data correct
    
    for(int i=1;i<=M;i++){      //获取最短路径
        Dijkstra(N+i,i);
        memset(isVisited,0,sizeof(isVisited));
    }
    vector<int> minArr;
    vector<int> minArr1;
    double maxCK = -1;      //标记各个最短路径中的最长路径
    int minT = MAX;         //标记最长的最短路径总和
    int totalLength[M+1];
    memset(totalLength,0,sizeof(totalLength));
    for(int i=1;i<M+1;i++){
        int tempMin = MAX;
        int mark = 0;
        for(int j=1;j<=N;j++){  //找到最短路径
            if(distanceL[i][j]>D){  //无法做到能到达各个house
                tempMin = -2;
                mark = 1;
                break;
            }
            totalLength[i] += distanceL[i][j];  //计算总路长,相当于平均值
            if(distanceL[i][j]<tempMin){
               tempMin = distanceL[i][j];
            }
        }
        if(mark==1){    //遇见超出范围的station,跳出本次循环
            continue;
        }
        if(tempMin==maxCK){
            minArr.push_back(i);
        }else if(tempMin>maxCK){
            maxCK = tempMin;
            minArr.clear();
            minArr.push_back(i);
        }
    }
    if(minArr.size()==0){
        cout<<"No Solution";
        return 0;
    }
    if(minArr.size()>1){    //最短距离存在多个下的情况
        for(int j=0;j<minArr.size();j++){
            if(totalLength[minArr[j]]<minT){
                minArr1.clear();
                minT = totalLength[minArr[j]];
                minArr1.push_back(minArr[j]);
            }else if(totalLength[minArr[j]]==minT){
                minArr1.push_back(minArr[j]);
            }
        }
        if(minArr1.size()>1){   //最短平均距离相同的也存在多个情况
            sort(minArr1.begin(),minArr1.end());
            int endIndex = minArr1[0];
            cout<<"G"<<endIndex<<endl;
            cout<<fixed<<setprecision(1)<<maxCK<<" "<<(double)minT/N;
        }else{                  //最短平均距离刚好只有一个
            int endIndex = minArr1[0];
            cout<<"G"<<endIndex<<endl;
            cout<<fixed<<setprecision(1)<<maxCK<<" "<<(double)minT/N;
        }
    }else{              //最短距离的最大值只有一个
            minT = totalLength[minArr[0]];
            int endIndex = minArr[0];
            cout<<"G"<<endIndex<<endl;
            cout<<fixed<<setprecision(1)<<maxCK<<" "<<(double)minT/N;
    }
    return 0;
}


 

### 高德地图日志输出的意义 在 Android 开发过程中,当集成高德地图功能时,可能会遇到特定的日志输出。以下是针对 `GasStation` 和 `高德地图客户端已经安装` 这两类日志的具体解释。 #### 关于 'GasStation' 如果在调试或运行应用期间发现日志中有 `'GasStation'` 输出,则可能表示当前的地图操作涉及到了加油站的相关数据点。这通常发生在以下场景中: - 地图上加载了 POI(兴趣点)数据,而这些数据包含了加油站的信息。 - 应用正在查询与加油站点有关的服务或者路径规划[^1]。 这种日志可能是由高德地图 SDK 自动记录的行为,用于指示某些特定类型的地理特征被处理或展示给用户。它并不一定代表错误或警告;相反,它是正常工作流程的一部分,表明 SDK 正确解析并呈现了相关位置信息。 #### 关于 '高德地图客户端已经安装' 此条目明确指出设备上存在官方发布的高德地图应用程序实例。这一确认过程通常是通过检查目标软件包名来完成的——对于高德而言,默认情况下其主包名称为 `com.autonavi.minimap` 或其他变体形式[^2]。一旦检测到对应的应用存在于用户的移动终端里,便会触发上述消息打印至控制台作为反馈机制之一: ```java public static boolean isGaoDeMapInstalled(Context context){ try { ApplicationInfo info = context.getPackageManager() .getApplicationInfo("com.autonavi.minimap", PackageManager.GET_META_DATA); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } ``` 这段伪代码展示了如何利用 Android 提供的标准 API 来验证指定 APP 是否可用,并据此调整后续逻辑走向比如决定采用嵌入式视图还是跳转外部链接等形式提供服务[^3]。 综上所述,“高德地图客户端已经安装”的提示有助于开发者了解环境状态从而做出更优决策同时也让用户获得更好的体验效果。 ```java if(isGaoDeMapInstalled(context)){ Log.d("MAP_STATUS","高德地图客户端已经安装"); } else{ Toast.makeText(context,"请先下载安装高德地图!",Toast.LENGTH_SHORT).show(); } ``` 以上示例简单演示了基于前面提到方法的结果来进行基本交互设计的一种方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值