1072 Gas Station(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 (≤10​3), 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 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个加油站,求加油站P,使其到所有村庄的距离不超过D,若有多个解,则选择到村庄的最短距离最远的,若仍有多个解,则选择到村庄的平均距离最近的。选择符合条件的最小索引的加油站。
加油站总数不超过10个,因此对每个加油站采用Dijkstra算法即可。关键在于选择符合题意的加油站,可将所有解用数组存储,再进行判断。我的方法是直接在循环中判断,对每个加油站设置三个变量:

  • d_max:到各村庄的最远距离。
  • d_min:到各村庄的最近距离。
  • d_avg:到各村庄的平均距离。

因此,只要在d_max<D的基础上,选择d_min最远的,再在这一基础上,选择d_avg最小的加油站即可。(涉及的变量有点多,理清思路即可~)

AC代码

#include <cstdio>
#include <iostream>
#include <string>
#include <vector> 
using namespace std;

const int maxv = 1020, INF = 1000000;
struct node{
	int v, dis;
	node(int a, int b):v(a),dis(b){}
};
vector<node> Adj[maxv];
bool vis[maxv];
int d[maxv];
double d_avg, d_min;
int n, m, k, D;

void Dijkstra(int s){
	fill(vis, vis + maxv, false);
	fill(d, d + maxv, INF);
	d[s] = 0; //源点到自身距离为0
	for(int i = 1; i <= n + m; i++){
		int u = -1, min = INF;
		for(int j = 1; j <= n + m; j++){ //鎵惧埌鏈€灏忕殑d[u] 
			if(vis[j] == false && d[j] < min){
				u = j;
				min = d[u];
			}
		}
		if(u == -1) return;
		vis[u] = true;
		for(int j = 0; j < Adj[u].size(); j++){
			int v = Adj[u][j].v; 
			if(vis[v] == false && d[v] >  d[u] + Adj[u][j].dis) 
				d[v] = d[u] + Adj[u][j].dis;
		} 
	} 

}

int input(){
	int t = 0;
	string str;
	cin>>str;
	for(int i = 0; i < str.size(); i++){
		if(str[i] == 'G') continue;
		t = t * 10 + str[i] - '0';
	}
	if(str[0] == 'G') return t + n;
	return t;
}

int main(){ 
	cin>>n>>m>>k>>D;
	int u, v, dis;
	for(int i = 0; i < k; i++){
		u = input();
		v = input();
		scanf("%d", &dis);
		Adj[u].push_back(node(v, dis));
		Adj[v].push_back(node(u, dis));
	}
	double min_avg = INF, max_min = 0; //最短平均距离及对应最短距离 
	double d_max = 0; //保证距离不超过D 
	int minp; 
	for(int i = 1; i <= m; i++){
		d_avg = 0.0, d_min = INF, d_max = 0.0;
		Dijkstra(n + i);
		for(int j = 1; j <= n; j++){
			d_avg += d[j];
			if(d[j] < d_min) d_min = d[j]; 
			if(d_max < d[j]) d_max = d[j];
		}
		d_avg /= n;
		//cout<<d_avg<<endl;
		if(d_max <= D && (d_min > max_min || 
			d_min == max_min && d_avg < min_avg)){
			minp = i;
			min_avg = d_avg;
			max_min = d_min;
		}
	}
	if(min_avg == INF)
		cout<<"No Solution";
	else{
		cout<<"G"<<minp<<endl;
		printf("%.1f %.1f", max_min, min_avg);
	cout<<endl;
	}
	return 0;
}
发布了110 篇原创文章 · 获赞 0 · 访问量 1282

猜你喜欢

转载自blog.csdn.net/qq_43072010/article/details/105272162