1. 程式人生 > >【PAT】1072. Gas Station (30)【dijkstra演算法】

【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;
}