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 2Sample Output 1:
G1 2.0 3.3Sample Input 2:
2 1 2 10 1 G1 9 2 G1 20Sample Output 2:
No Solution
对每个加油站待定位置进行Dijkstra最短路径计算,使用AB集合吸收已确定的结点,用于计算加油站与最近结点的距离以及与所有结点平均距离,若比全局变量的方案更优,更新全局变量。有两个地方可以剪枝,一是未吸收集合UD到原点的最短路径超过给定的最大距离Ds,二是UD中的结点都是加油站待定点。/*2015.7.29cyq*/ #include <iostream> #include <vector> #include <string> #include <algorithm> #include <fstream> #include <sstream> using namespace std; //ifstream fin("case1.txt"); //#define cin fin const int MAX=2147483647; int N,M,K,Ds; int station=0; double minLen=0,totalLen=0; struct node{ int num; int length; node(int n,int l):num(n),length(l){} }; bool cmp(const node &a,const node &b){ return a.length<b.length; } void dijkstra(int start,const vector<vector<int> > &roads){ int count=0; vector<node> AB,UD;//AB为已确定集合,UD为未确定集合 for(int i=1;i<=N+M;i++){ if(i!=start) UD.push_back(node(i,MAX)); } node cur(start,0); AB.push_back(cur); while(!UD.empty()){ for(auto it=UD.begin();it!=UD.end();it++){ if(roads[cur.num][(*it).num]){//有路 int tmp=roads[cur.num][(*it).num]+cur.length; if(tmp<(*it).length){ (*it).length=tmp; } } } sort(UD.begin(),UD.end(),cmp); cur=UD[0]; if(cur.length>Ds)//剪枝,最短路径超过给定范围,直接结束 return ; UD.erase(UD.begin()); AB.push_back(cur); if(cur.num<N+1) count++; if(count==N)//剪枝,剩下的未吸收的点都是加油站,可直接略过 break; } //用AB集合算出最小路程和总路程 double sum=0; double minL=MAX; for(auto it=AB.begin();it!=AB.end();it++){ if((*it).num<N+1){//N+1以上为加油站 sum+=(*it).length; if(minL>(*it).length) minL=(*it).length; } } //判断在start建加油站是否比原有方案更优 if(minL>minLen){ station=start; minLen=minL; totalLen=sum; }else if(minL==minLen){ if(sum<totalLen){ station=start; totalLen=sum; } } } int str2int(const string &s){ stringstream ss; int res; if(s[0]=='G'){//加油站编号为N+1到N+M ss<<s.substr(1); ss>>res; res+=N; }else{ ss<<s; ss>>res; } return res; } int main(){ cin>>N>>M>>K>>Ds; vector<vector<int> > roads(N+M+1,vector<int>(N+M+1,0)); string s1,s2; int a,b; while(K--){ cin>>s1>>s2; a=str2int(s1); b=str2int(s2); cin>>roads[a][b]; roads[b][a]=roads[a][b]; } for(int i=N+1;i<=N+M;i++){ dijkstra(i,roads); } if(station==0) printf("No Solution\n"); else{ printf("G%d\n",station-N); printf("%.1f %.1f\n",minLen,totalLen/N); } return 0; }