【PAT】1072. Gas Station (30)【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 FORMAT

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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括4个正整数:N(<=10^3),房子的总数;M(<=10),加油站可能选择的位置数;K,(<=10^4),连接房子和加油站的道路数;和Ds,加油站的最远服务距离。假设所有房子都被编号为1-N,所有可能的选址位置被编号为G1到GM。
接着K行每行按照以下格式描述一条路:
P1 P2 Dist
P1和P2代表道路两端,可能是民居或是加油站位置;Distinct代表路的整数距离。

OUTPUT FORMAT

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”.

翻译:对于每组输入数据,输出一行最佳位置的编号。第二行输出民居最近距离和到所有民居的平均距离。一行内数字之间必须用空格隔开,并且保留1位小数。如果结果不存在,则输出“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模板题,主要就是模拟题目说明,把Gi转换成数字,我是让Gi在前面,民居在后面。注意这道题的条件为先取最近民居距离最远的,否则取平均距离最短的,否则取编号最小的,真的一不注意就会看错。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
int N,M,K,Dot,Ds;
double Min=0.0,Sum=INF,Ave=0.0;
int Des=0; 
struct Edge{
    int length,to;
    Edge(int l,int t):length(l),to(t){}
    bool operator<(const Edge &a)const{
        return length>a.length;
    }
};
vector<Edge> v[1020];
priority_queue<Edge> q;
int Judge(char s[]){
    int length=strlen(s),num=0;
    for(int i=0;i<length;i++){
        if(s[i]=='G')continue;
        if(s[i]>='0'&&s[i]<='9'){
            num=num*10+s[i]-'0';    
        }
    }
    if(s[0]!='G')num+=M;
    return num;
}
int d[1020];
void dijkstra(int s){
    for(int i=0;i<=Dot;i++)d[i]=INF;
    d[s]=0;
    q.push(Edge(0,s));
    double sum=0,Lmin=INF;
    while(!q.empty()){
        Edge temp=q.top();q.pop();
        int t=temp.to;
        if(d[t]<temp.length)continue;
        if(t>M&&Lmin>temp.length)Lmin=temp.length;
        for(int i=0;i<v[t].size();i++){
            Edge e=v[t][i];
            if(d[e.to]>d[t]+e.length){
                d[e.to]=d[t]+e.length; 
                q.push(Edge(d[e.to],e.to));
            }
        }
    }
    for(int i=M+1;i<=Dot;i++){
        if(d[i]>Ds)return ;
        else sum+=d[i];
    }
    if(Min<Lmin||(Min==Lmin&&Sum>sum)){
        Sum=sum;
        Ave=Sum*1.0/N;
        Des=s;
        Min=Lmin;
    }
}
int main(){
    scanf("%d%d%d%d",&N,&M,&K,&Ds);
    char a[5],b[5];
    int l;
    for(int i=0;i<K;i++){
        scanf("\n%s%s%d",a,b,&l);
        int n1=Judge(a);
        int n2=Judge(b);
        v[n1].push_back(Edge(l,n2));
        v[n2].push_back(Edge(l,n1)); 
    }
    Dot=N+M;
    for(int i=1;i<=M;i++){
        dijkstra(i);
    }
    if(Des==0)printf("No Solution\n");
    else printf("G%d\n%.1lf %.1lf\n",Des,Min,Ave);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值