1072. Gas Station PAT 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 (<= 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

要点:输入路径时有可能是数字,也有可能是G+数字,利用scanf函数的返回值可以方便地建立图,也可以用stoi函数。对m个候选位置依次运行dijkstra算法,每个点运行完检查距离最远的dis,如果在服务范围内则判断最近的点是不是比之前候选点最近的点要远。


要注意输出的时候用了%.1lf,3.25输出了3.2,与样例不一样,但是提交可以通过。

测试了一下,用%.1lf输出3.95会直接输出4.0,应该是会四舍五入,但是3.25,3.55,3.65是直接舍掉了5,原因待查

dev c 5.1.1   gcc 4.9.2

cstdio会stdio.h不会 


#include<cstdio>
#include<cstring>
#include<algorithm>
#define ma 99999999
using namespace std;

int n,m,k,ds;
int map[1011][1011],vis[1011],dis[11][1011],ttdis[11];

typedef struct G{
	int t;
	int mididt;
	int maxdist;
	int ttdist;
}G;

bool cmp(G a,G b){
	if(a.maxdist<=ds&&b.maxdist<=ds){
		if(a.mididt!=b.mididt){
			return a.mididt>b.mididt;
		}
		else if(a.ttdist!=b.ttdist){
			return a.ttdist<b.ttdist;
		}
		else{
			return a.t<b.t;
		}
	}
	else if(a.maxdist<=ds){
		return true;
	}
	else if(b.maxdist<=ds){
		return false;
	}
	return true;
}

int s2i(char c[]){
	int i=strlen(c)-1;
	int x=1;
	int num=0;
	for(;i>=0;i--){
		num=num+(c[i]-'0')*x;
		if(i==1&&c[0]=='G'){
			num=num+1000;
			break;
		}
		x*=10;
	}
	return num;
}

void als(){
	int i,j,i0;
	fill(dis[0],dis[0]+11*1011,ma);
	for(i0=1;i0<=m;i0++){
		int g=i0+1000;
		fill(vis,vis+1011,0);
		dis[i0][g]=0;
		for(i=1;i<=1010;i++){
			int now=-1,nrd=ma;
			for(j=1;j<=1010;j++){
				if(dis[i0][j]<nrd&&vis[j]==0){
					nrd=dis[i0][j];
					now=j;
				}
			}
			if(now==-1)break;
			vis[now]=1;
			for(j=1;j<=1010;j++){
				if(dis[i0][j]>dis[i0][now]+map[now][j]&&vis[j]==0){
					dis[i0][j]=dis[i0][now]+map[now][j];
				}
			}
		}
	}
	G Gs[m+1];
	for(i=1;i<=m;i++){
		Gs[i].mididt=ma;
		Gs[i].maxdist=-1;
		Gs[i].t=i;
		for(j=1;j<=n;j++){
			Gs[i].ttdist+=dis[i][j];
			if(dis[i][j]<Gs[i].mididt)Gs[i].mididt=dis[i][j];
			if(dis[i][j]>Gs[i].maxdist)Gs[i].maxdist=dis[i][j];
		}
	}

	sort(Gs+1,Gs+m+1,cmp);
	if(Gs[1].maxdist>ds){
		printf("No Solution");
		return;
	}
	else{
		double avr;
		avr=(1.0*Gs[1].ttdist)/(1.0*n);
		avr=avr*10.0+0.5;
		int tt=avr;
		avr=tt;
		avr=avr/10.0;
		double t2=Gs[1].mididt;
		printf("G%d\n%.1lf %.1lf",Gs[1].t,t2,avr);
	}
}

int main(){
	fill(map[0],map[0]+1011*1011,ma);
	int i,j,p1,p2,dis;
	char p11[5],p22[5];
	scanf("%d %d %d %d",&n,&m,&k,&ds);
	for(i=0;i<k;i++){
		scanf("%s %s %d",&p11,&p22,&dis);
		p1=s2i(p11);
		p2=s2i(p22);
		map[p1][p2]=map[p2][p1]=dis;
	}
	als();
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值