A1072. 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
  1 // hahaha.cpp : 定义控制台应用程序的入口点。
  2 //
  3
  4 #include "stdafx.h"
  5 #include <stdio.h>
  6 #include <iostream>
  7 #include <vector>
  8 #include <map>
  9 #include <string>
 10 #include <cstdio>
 11 #include <set>
 12 #include <algorithm>
 13 #include <string.h>
 14 using namespace std;
 15 const int maxn=1020;
 16 int n,m,k,ds;
 17
 18 int G[maxn][maxn];
 19
 20 int getid(char a[])
 21 {
 22  int len=strlen(a);
 23  int id=0;
 24  for(int i=0;i<len;i++)
 25  {
 26  if(a[i]!=‘G‘)
 27  {
 28    id=id*10+a[i]-‘0‘;
 29  }
 30  }
 31 if(a[0]==‘G‘)return id+n;
 32 else
 33  return id;
 34 }
 35 const int INF=1000000000;
 36 int d[maxn];
 37 int vis[maxn]={false};
 38 void dij(int s)
 39 {
 40   memset(vis,false,sizeof(vis));
 41   fill(d,d+maxn,INF);
 42   d[s]=0;
 43   for(int i=1;i<=m+n;i++)
 44   {
 45         int u=-1,min=INF;
 46         for(int j=1;j<=m+n;j++)
 47             {
 48                 if(d[j]<min && vis[j]==false)
 49                     {
 50                     u=j;
 51                     min=d[j];
 52                     }
 53             }
 54                 if(u==-1)return;
 55                 vis[u]=true;
 56                 for(int k=1;k<=n+m;k++)
 57                 {
 58                     if(vis[k]==false&&G[u][k]!=INF)
 59                     {
 60                         if(G[u][k]+d[u]<d[k])
 61                         {
 62                             d[k]=G[k][u]+d[u];
 63                         }
 64                     }
 65                 }
 66     }
 67 }
 68
 69
 70
 71 int main()
 72 {
 73     scanf("%d %d %d %d",&n,&m,&k,&ds);
 74
 75     fill(G[0],G[0]+maxn*maxn,INF);
 76     for(int i=0;i<k;i++)
 77     {
 78     char p1[15],p2[15];
 79     int dist;
 80     scanf("%s %s %d",p1,p2,&dist);
 81     int id1=getid(p1);
 82     int id2=getid(p2);
 83     G[id1][id2]=dist;
 84     G[id2][id1]=dist;
 85
 86     }
 87     //输入完毕
 88
 89     //处理图,对加油站处理,求平均距离,和最大的最近距离
 90     double ansdis=-1,ansavg=INF;
 91     int ansid=-1;
 92     for(int i=n+1;i<=n+m;i++)
 93     {
 94         double mindis=INF,avg=0;
 95         bool flag=false;
 96         double sum=0;
 97         dij(i);
 98         for(int j=1;j<=n;j++)
 99         {
100             if(d[j]>ds)
101             {
102             flag=true;
103             break;
104             }
105             if(d[j]<mindis)mindis=d[j];
106             sum+=d[j];
107         }
108         if(flag==true)continue;
109
110      avg=sum/n;
111      if(mindis>ansdis)
112      {
113      ansid=i;
114      ansdis=mindis;
115      ansavg=avg;
116      }
117      else if(mindis==ansdis&&avg<ansavg)
118      {
119         ansid=i;
120         ansavg=avg;
121      }
122 }
123
124      if(ansid==-1)printf("No Solution\n");
125      else
126          {
127          printf("G%d\n",ansid-n);
128          printf("%.1f %.1f\n",ansdis,ansavg);
129          }
130     return 0;
131 }
时间: 2024-08-02 04:23:16

A1072. Gas Station (30)的相关文章

pat1072. Gas Station (30)

1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 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. H

PAT 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 o

1072. Gas Station (30)

先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 选 编号小的. 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas station has to be built at such a location that the minimum distance

1072 Gas Station (30)(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

PAT甲题题解-1072. Gas Station (30)-dijkstra最短路

题意:从m个加油站里面选取1个站点,使得其离住宅的最近距离mindis尽可能地远,并且离所有住宅的距离都在服务范围ds之内.如果有很多相同mindis的加油站,输出距所有住宅平均距离最小的那个.如果平均值还是一样,就输出按照顺序排列加油站编号最小的. 分析:加油站之间也是彼此有路连接的,所以最短路径计算的时候也要把加油站算上,是双向边,所以边的数组大小得开两倍.加油站编号记为n+1~n+m,对这些点用dijkstra计算最短路径即可,这里顺便在dijkstra中进行了剪枝处理,不剪枝也没事. #

PAT (Advanced Level) 1072. Gas Station (30)

枚举一下选的位置,每次算一下就可以了. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; const int

PAT 1072 Gas Station[图论][难]

1072 Gas Station (30)(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

Gas Station

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an e

LeetCode: Gas Station 解题报告

Gas Station There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journ