PTA_PAT甲級_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 (≤10​4), the number of roads connecting the houses and the gas stations; and D​S, 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

題意:
N所居民房M個加油站待建點和K條無向邊,使所有居民房都在服務範圍DS內,同時使距離最近的居民房距離加油站的距離最遠,若有多個解則選平均距離最小的,平均距離相同選編號最小的

分析:
居民房編號即爲本身,加油站編號爲G後數字加N;對每個待建點使用Dijkstra得到所有居民房距離該點的最短距離;最後檢查是否有距離超過DS,並檢查是否需更新最短距離

代碼:

#include<iostream>
#include<cstdio>
#include<cstring> 
#include<algorithm>
using namespace std;

#define INF 1e9
#define MAXV 1020
int N, M, K, DS, G[MAXV][MAXV], d[MAXV];
bool vis[MAXV] = {false};

//Dijkstra算法求所有頂點到起點s的最短距離 
void Dijkstra(int s){
	memset(vis, false, sizeof(vis));
	fill(d, d+MAXV, INF);
	d[s] = 0;
	for(int i=0;i<N+M;i++){
		int u=-1, MIN=INF;//u爲使d[u]最小的頂點編號,MIN存放最小d[u]
		for(int j=1;j<=N+M;j++)//找到未訪問的頂點中d[]最小的
			if(vis[j]==false && d[j]<MIN){
				u = j;
				MIN = d[j];
			} 
		if(u==-1) return;//找不到說明不連通
		vis[u] = true;
		for(int v=1;v<=N+M;v++)
			if(vis[v]==false && G[u][v]!=INF)
				if(d[u]+G[u][v]<d[v]) d[v] = d[u] + G[u][v];
	} 
}

//將str[]轉換爲數字,若str是數字則返回本身,否則返回去掉G之後的數加N 
int getID(char str[]){
	int i=0, len=strlen(str), ID=0;
	while(i<len){
		if(str[i]!='G') ID = ID*10 + (str[i]-'0');
		i++;
	}
	if(str[0]=='G') return N+ID;
	else return ID;
}

int main()
{
    cin>>N>>M>>K>>DS;
    int u, v, w;
    char city1[5], city2[5];
    fill(G[0], G[0]+MAXV*MAXV, INF);
    for(int i=0;i<K;i++){
    	cin>>city1>>city2>>w;//以字符串讀入城市編號
		u = getID(city1);
		v = getID(city2);
		G[v][u] = G[u][v] = w; 
	}
	double ansDis=-1, ansAvg=INF;//最大最短距離,最小平均距離
	int ansID = -1;//最終加油站ID
	for(int i=N+1;i<=N+M;i++){//枚舉所有加油站 
		double minDis=INF, avg=0;//minDis爲最大的最近距離,avg爲平均距離
		Dijkstra(i);//進行Dijkstra算法,求出d數組
		for(int j=1;j<=N;j++){
			if(d[j]>DS){//存在距離大於DS的居民房則直接跳出 
				minDis = -1;
				break; 
			}
			if(d[j]<minDis) minDis = d[j];//更新最大的最近距離
			avg += 1.0*d[j]/N; 
		} 
		if(minDis==-1) continue;//存在距離大於DS的居民房,跳過該加油站
		if(minDis>ansDis){//更新最大的最近距離 
			ansID = i;
			ansDis = minDis;
			ansAvg = avg;
		}else if(minDis==ansDis && avg<ansAvg){
			ansID = i;
			ansAvg = avg;
		}
	} 
	if(ansID==-1) cout<<"No Solution"<<endl;
	else{
		cout<<"G"<<ansID-N<<endl;
		printf("%.1f %.1f\n",ansDis,ansAvg);
	}
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章