PAT-A-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 (≤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

 


多个出发点的Dijkstra算法,给定了一种最优解的限定

将题目中1~N映射到0~N-1,G1~GM映射到N~N+M-1,保存图,然后将每个加油站作为出发点进行dijkstra算法,根据限定条件找到最优的解法。

#include <iostream>
#include <vector>                  //需要使用vector容器
#include <queue>                   //需要使用队列
#include <stdlib.h>                //需要使用atoi函数(字符指针所指向串转变为int)        
using namespace std;
int N,M,K,D;                       //第一行四个参数
#define MAX_N 1000                 //限定范围
#define MAX_M 10000 
struct Edge{                      //每个house/station看成一个节点
	int to;
	int weight;
};
int getIndex(char s[]){          //获得下标,因为我们把house映射为0-N-1,station映射为N-M-1
	if(s[0]=='G') return atoi(s+1)-1+N;
    else return atoi(s)-1;
}
int bestid=-1;                    //初始化三个结果条件bestid为目前最优的station下标
float bestTotal=0,bestmin=0;      //bestmin纪录当前station到house最短距离
void update(int id,int dist[]){
	float min=-1,total=0;
	for(int i=0;i<N;i++){
		if(dist[i]>D) return;     //到某一个house距离大于服务距离,这个station可以放弃
	    else if(min==-1||dist[i]<min) min=dist[i];   //station到
		total+=dist[i];           //加油站到所有house的距离和
	} 
	if(bestid==-1||min>bestmin||(min==bestmin&&total<bestTotal)){   //更新条件
		bestid=id;                                                  //第一个进来的更新
		bestmin=min;                                                //到居民家距离远的更新
		bestTotal=total;                                        //到居民家距离总和小的更新
	}                                                            //注意三个条件的顺序
}
Edge getEdge(int to,int weight){                                //创造节点
	Edge node;
	node.to=to;
	node.weight=weight;
	return node;
}

int main(){
	scanf("%d %d %d %d",&N,&M,&K,&D);
	vector< vector<Edge> > G(N+M);
	char s1[5],s2[5];
	for(int i=0,weight,u,v;i<K;i++){
		scanf("%s %s %d",s1,s2,&weight);
		u=getIndex(s1);
		v=getIndex(s2);
		G[u].push_back(getEdge(v,weight));
		G[v].push_back(getEdge(u,weight));
	}
	queue< Edge > pq;
	bool marked[MAX_N+MAX_M];
	int    dist[MAX_N+MAX_M];
	int l=N+M;
	for(int j=N,u,v;j<l;j++){
		fill(marked,marked+l,false);
		fill(dist,dist+l,10000);		
		pq.push(getEdge(j,0));
		dist[j]=0;
		while(!pq.empty()){
			u=pq.front().to;
			pq.pop();
			if(marked[u]) continue;
			for(Edge& e : G[u]){
				v=e.to;
				if(marked[v]) continue;
				if(dist[u]+e.weight<dist[v]){
					dist[v]=dist[u]+e.weight;
					pq.push(getEdge(v,dist[v]));
				}
			}
		}
		update(j-N,dist);
	}
	if(bestid==-1) printf("No Solution\n");
	else printf("G%d\n%.1f %.1f",bestid+1,bestmin,bestTotal/N);

	return 0;
}

PAT-A-1072 Gas Station 【Dijkstra】

上一篇:LeetCode 1478.安排邮筒 (1、滑动窗口)


下一篇:Adi People