C Dijkstra算法 多次使用 1072 Gas Station (30分)

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

解题
将G1——GM保存在N+1——N+M的下标位置;
对N+1——N+M分别进行Dijkstra算法计算;
提出每次计算后该加油站到各城市的最短距离,最长距离和平均距离;
首先满足最长距离小于服务范围Ds;
再找最短距离最大的加油站位置;
若最短距离相同则最后找平均距离最小的位置;
下标本来就从前往后找,所以无需再做判断;

定义了一个结构,导致空间占用比较大;

#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
#define MAXN 1020     //最大house数 
#define MAXM 11      //最大候选点数 
#define INFINITY 0x3fffffff 
//把Gi保存在N后面
int N,M,K,Ds;
int  Graph[MAXN+MAXM][MAXN+MAXM];

struct station{
	int  Maxdis=0;
	int Mindis=INFINITY;
	double Avrdis;
};
station Dijkstra(int n)  //起始下标为n 
{
	int dist[MAXN+MAXM];
	bool collected[MAXN+MAXM];
	fill(dist+1,dist+MAXN+MAXM+1,INFINITY);
	fill(collected+1,collected+1+MAXN+MAXM,0);
	
	dist[n]=0;
	
	while(1){
		//找到最小值 
		int min=INFINITY;
		int v=-1;
		for(int i=1;i<=N+M;i++)
		if(!collected[i]&&dist[i]<min)
			{
				min=dist[i];
				v=i;
			}
		//找到dist最小值
		if(v==-1) break;
		collected[v]=1;
		for(int i=1;i<=N+M;i++)
		{
			if(!collected[i]){
				if(dist[v]+Graph[v][i]<dist[i])
					dist[i]=dist[v]+Graph[v][i];	 
			}
		}		
	}
	station s;
	int sum=0;
	for(int i=1;i<=N;i++)      //判断到house的距离,不用判断到station的距离 
	{
		if(i==n) continue;
		if(s.Maxdis<dist[i]) s.Maxdis=dist[i];    //最大值 
		if(s.Mindis>dist[i]) s.Mindis=dist[i];    //最小值
		sum+=dist[i]; 
	}
	s.Avrdis=((double)sum/N);
	
	return s;
	 
}

int main()
{
		
	fill(Graph[0],Graph[0]+(MAXN+MAXM)*(MAXN+MAXM),INFINITY);
	cin>>N>>M>>K>>Ds;
	for(int i=0;i<K;i++)
	{
        string c1,c2;
        int s;
        cin>>c1>>c2>>s;
        int a,b;
        if(c1[0]=='G'){
            a=stoi(c1.substr(1))+N;
        }
        else a=stoi(c1);
        if(c2[0]=='G'){
            b=stoi(c2.substr(1))+N;
        }
        else b=stoi(c2);
        Graph[a][b]=s;
        Graph[b][a]=s;
    }                       //输入
    
	
	//Graph为各点距离
	//求每个加油站到居民区的最短距离 
	//Gi下标为i+N
	
	int maxD=-1;
	int minA=INFINITY;
	int result;
	station s1;
	
	for(int i=1;i<=M;i++){
		station s=Dijkstra(i+N);
		if(s.Maxdis>Ds) continue;    //最大值过大——找下一个点
		if(s.Mindis>maxD){      //找最小值最大的
			maxD=s.Mindis;
			result=i;
			s1=s;
		}
		if(s.Mindis==maxD) //若相等
		{
			if(s.Avrdis<s1.Avrdis) //找平均值最小的
			{	
				result=i;
				s1=s;
			}
		}
	}
	if(maxD==-1)
	printf("No Solution");
	else{
	printf("G%d\n%d.0 %.1f",result,s1.Mindis,s1.Avrdis);
}
	//首先满足最大距离不超过Ds——要得到最大值 
	//再找到G1-M中的最小距离的最大值——要得到最小值 
	//要得到平均值——平均值的最小值 
	//下标的最小值 
}

本题样例答案3.3和3.2都是正确的,无需四舍五入;
注意点
输入方式

	cin>>N>>M>>K>>Ds;
	for(int i=0;i<K;i++)
	{
        string c1,c2;
        int s;
        cin>>c1>>c2>>s;
        int a,b;
        if(c1[0]=='G'){
            a=stoi(c1.substr(1))+N;
        }
        else a=stoi(c1);
        if(c2[0]=='G'){
            b=stoi(c2.substr(1))+N;
        }
        else b=stoi(c2);
        Graph[a][b]=s;
        Graph[b][a]=s;
    }        

按string类型读取;
若下标0的位置为G,则下一位+N;
stoi也好用;

发布了105 篇原创文章 · 获赞 6 · 访问量 4958

猜你喜欢

转载自blog.csdn.net/BLUEsang/article/details/105461013