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 (<= 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求加油站到各个村庄的最短距离,把加油站转换成数字,也当村庄使用

求出来最短路径,累加求和,求平均值,然后判断输出

坑的就是题目(还是自己英语水)!!!!!输出的是加油站和各个村庄的最小距离中的最大距离,然后相同的话输出平均值小的

我第一次直接根据平均值输出。样例都不对!谷歌翻译下来,题目理解错了。

后面的错就是村庄的转换上,假如样例1,G1转换成5,G2转换成6  我自以为是的直接转换方法是G[1]-'0'+n;

其实也可能有G11,G12.。。范围没在意。。


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
typedef pair<int,int> P;
const int INF=1<<29;
int map[1001+200][1001+200],v[1001+200],len[1001+200];
int n,m,k,ds;
double out_min=0,out_avg=INF;
int out=0;
int toint(string s){
	if(s[0]=='G'){
		int sum=0;
		for(int i=1;i<s.size();i++){
			sum=sum*10+s[i]-'0';
		}
		return sum+n;
	}
	int sum=0;
	for(int i=0;i<s.size();i++){
		sum=sum*10+s[i]-'0';
	}
	return sum;
}
void Dijkstra(int x){
	priority_queue<P,vector<P>,greater<P> >que;
	for(int i=1;i<=n+m;i++){
		len[i]=INF;
		v[i]=0;
	}
	len[x]=0;
	que.push({len[x],x});
	while(que.size()){
		P p=que.top();
		que.pop();
		int num=p.second;
		if(v[num]) continue;
		v[num]=1;
		for(int i=1;i<=n+m;i++){
			if(map[i][num]!=INF&&len[i]>len[num]+map[num][i]){
				len[i]=len[num]+map[num][i];
				que.push({len[i],i});
			}
		}
	}
	int mi=INF;
	double sum=0;
	for(int i=1;i<=n;i++){
		if(len[i]>ds) return;
		if(len[i]<mi) mi=len[i];
	//	cout<<len[i]<<" ";
		sum+=1.0*len[i];
	}
//	cout<<endl;
	double avg=1.0*sum/n;
//cout<<x<<" "<<avg<<" "<<sum<<" "<<mi<<endl;
	if(mi>out_min){
		out=x;
		out_avg=avg;
		out_min=mi;
	}
	else if(mi==out_min&&avg<out_avg){
		out=x;
		out_avg=avg;
		out_min=mi;
	}
	/*if(avg>out_avg){
		out=x;
		out_avg=avg;
		out_min=mi;
	}*/
}
int main(){
	
	cin>>n>>m>>k>>ds;
	for(int i=1;i<=n+m;i++){
		for(int j=1;j<=n+m;j++)
		map[i][j]=INF;
	} 
	for(int i=0;i<k;i++){
		char s1[4],s2[4];
		int length;
		cin>>s1>>s2>>length;
		int num1=toint(s1),num2=toint(s2);
		map[num1][num2]=map[num2][num1]=length;
	}
	for(int i=n+1;i<=n+m;i++){
		Dijkstra(i);
	}
	if(out==0) cout<<"No Solution";
	else
	printf("G%d\n%.1f %.1f",out-n,out_min,out_avg);
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值