您的位置:首页 > 其它

pat 1072 Gas Station

2015-11-21 23:20 337 查看


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


#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define N 1015

double connect

;
double _distance
;
int visited
;
int n, m, k;

struct GG
{
int i;
double minn;
double avg;

bool operator < (const GG& a) const{
if(minn != a.minn) {
return minn > a.minn;
} else if(avg != a.avg) {
return avg < a.avg;
} else return i < a.i;
}
};

double dijstla(int t) {
visited[t] = 1;

for(int i = 1; i <= n+m; i++) {
if(connect[t][i] > 0.0)
_distance[i] = connect[t][i];
}

for (int i = 0; i < n; ++i)
{
int minn = 0x7fffffff, index = -1;

for (int j = 1; j <= n+m; ++j)
{
if(_distance[j] > 0.0 && visited[j] == 0) {
if(_distance[j] < minn) {
minn = _distance[j];
index = j;
}
}
}

if(minn == 0x7fffffff) break;
visited[index] = 1;

for (int j = 1; j <= n+m; ++j)
{
if(visited[j] == 1) continue;

if(connect[index][j] <= 0.0) continue;

if(_distance[j] > 0.0) {
if(_distance[j] > _distance[index] + connect[index][j])
_distance[j] = _distance[index] + connect[index][j];
} else if(_distance[j] == 0.0) {
_distance[j] = _distance[index] + connect[index][j];
}
}
}

double maxx = 0.0;
for (int i = 1; i <= n; ++i)
{
if(_distance[i] > 0.0) {
if(_distance[i] > maxx)
maxx = _distance[i];
} else if(_distance[i] == 0.0) {
maxx = 0.0;
break;
}
}

return maxx;
}

double avg_distance() {
double sum = 0.0;

for (int i = 1; i <= n; ++i)
{
sum += _distance[i];
}

return sum/n;
}

double min_distance() {
double minn = 0x7fffffff;

for (int i = 1; i <= n; ++i)
{
if(_distance[i] > 0.0) {
if(_distance[i] < minn)
minn = _distance[i];
}
}

return minn;
}
int str2int(string a) {
int i = 0, be=0;
if(a[0] == 'G') i = 1;

if(true) {
int tmp = 0;
for (int j = i; j < a.length(); ++j)
{
tmp *= 10;
tmp += a[j]-'0';
}
be = (i==1?n:0)+tmp;
}

return be;
}
int main() {

double ds;
vector<GG> vec;

scanf("%d %d %d %lf", &n, &m, &k, &ds);

string a, b;
int be, ed;
double dist;

for (int i = 0; i < k; ++i)
{
cin >> a >> b >> dist;

be = str2int(a);
ed = str2int(b);
connect[be][ed] = dist;
connect[ed][be] = dist;
}

for (int i = 1; i <= m; ++i)
{
memset(_distance, 0.0, sizeof(_distance));
memset(visited, 0, sizeof(visited));

double tmp = dijstla(n+i);
double avg = avg_distance();
double minn = min_distance();
GG g;
g.i=i;
g.avg = avg;
g.minn = minn;
if(tmp <= ds && tmp > 0)
vec.push_back(g);
//printf("%lf %lf %lf\n", tmp, avg, minn);
}

if(vec.size() > 0) {
sort(vec.begin(), vec.end());
printf("G%d\n", vec[0].i);
printf("%.1lf %.1lf\n", vec[0].minn, vec[0].avg);
} else {
printf("No Solution\n");
}
return 0;
}


开始时,最后一个测试点过不了。是因为没有考虑到节点数大于10的时候,用原来的方法将字符串转换为数字便会出现错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: