1072 Gas Station(54行代码+超详细注释)

分数 30

全屏浏览题目

切换布局

作者 CHEN, Yue

单位 浙江大学

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

代码长度限制

16 KB

时间限制

200 ms

内存限制

64 MB

注意:由于double只能表示近似值,故实际题中所给案例平均值实际所得为3.2499999...,四舍五入为3.2,故需要加上尾数1e-8,但pat上就算不加上也能过...(acwing上不行)

#include
using namespace std;
const int N=1020,INF=0x3f3f3f3f;
int n,m,k,ds,c,g[N][N],dist[N];
string a,b;
bool st[N];
int get(string s){
    if(s[0]=='G')return n+stoi(s.substr(1));//若是加油站则取数字部分加上n,即将加油站的编号映射成n+1到n+m 
    else return stoi(s);//否则直接转成数字 
}
void dijkstra(int S,int &d1,int &s1){
    memset(dist,0x3f,sizeof dist);
    memset(st,0,sizeof st);
    dist[S]=0;
    for(int i=0;i         int t=-1;
        for(int j=1;j<=n+m;j++)
            if(!st[j]&&(t==-1||dist[j]         st[t]=1;
        for(int j=1;j<=m+n;j++)
            if(dist[j]>dist[t]+g[t][j])
               dist[j]=dist[t]+g[t][j];
    }
    for(int i=1;i<=n;i++){//遍历n个房屋 
        if(dist[i]>ds){//若当前加油站到房屋的距离在范围之外则将最小距离置为负数并返回 
            d1=-INF;
            return ;
        }
    }
    d1=INF,s1=0;//否则初始化最小距离为正无穷,总距离为0 
    for(int i=1;i<=n;i++){
        if(dist[i]         s1+=dist[i];//统计距离总和
    }
}
int main(){
    cin>>n>>m>>k>>ds;
    memset(g,0x3f,sizeof g); 
    for(int i=0;i         cin>>a>>b>>c;
        int x=get(a),y=get(b);//将字符串转成数字,加油站编号为n+1到n+m 
        g[x][y]=g[y][x]=c;
    }
    int id=-1,mind=0,sum=INF;//分别为所选位置编号,最近房屋距离,平均距离 
    for(int i=n+1;i<=n+m;i++){
        int d1,s1;//表示每个加油站与最近房屋的距离和到所有房屋的总距离 
        dijkstra(i,d1,s1);
        if(d1>mind)id=i,mind=d1,sum=s1;//若当前加油站到最近房屋的距离更大则更新 
        else if(d1==mind&&s1     }
    if(id!=-1)printf("G%d\n%.1f %.1f",id-n,(double)mind,(double)sum/n+1e-8);//若有符合题意的解 
    else puts("No Solution");//否则 
    return 0;
}

你可能感兴趣的:(pat甲级,c++,算法,图论)