PAT (Advanced Level) 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
對每個加油站待定位置進行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;
}

發佈了142 篇原創文章 · 獲贊 4 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章