1072. Gas Station (30)

来源:互联网 发布:mac 16g内存 虚拟机 编辑:程序博客网 时间:2024/05/17 01:25

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 51 2 21 4 21 G1 41 G2 32 3 22 G2 13 4 23 G3 24 G1 3G2 G1 1G3 G2 2
Sample Output 1:
G12.0 3.3
Sample Input 2:
2 1 2 101 G1 92 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
原创粉丝点击