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 (<= 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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++解决pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded in taxes by transferring the gas through this pipeline. Each two stations are connected by not more than one pipeline. The system was built by Soviet engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the gas arrives to the starting station number S and should be dispatched to the buyers on the final station number F. The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines. Unfortunately, the President did not consider that some pipelines ceased to exist long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible... Input The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F). Output If the desired route exists, you should output its profitability. Otherwise you should output "No solution".
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值