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 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 G
1 to G
M.
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
结尾无空行
Gusha!!!
测试点4太恶心了吧(╯‵□′)╯︵┻━┻
我还是没找到哪里不对,网上的错误什么数组开小了,忽略了两位数我都没犯!!!四舍五入也没错!!!
但就是过不去啊啊啊啊啊啊!!!
#include <iostream>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int edge[1020][1020]={0};
vector<int> dist;
vector<int> house_dist;
void Dijtstra(int start,int total_candidate,int total_point,int range)
{
int total=total_candidate+total_point;
int visited[1020]={0};
int d[1020];
fill(d,d+1020,inf);
for(int i=1;i<=total;i++)
{
d[i]=edge[start][i];
}
visited[start]=1;
int _min=inf;
int u=-1;
int min_dist=inf,sum_dist=0;
for(int i=0;i<total;i++)
{
_min=inf;
u=-1;
for(int j=1;j<=total;j++)
{
if(visited[j]==0&&_min>d[j])
{
_min=d[j];
u=j;
}
}
if(u==-1)
{
break;
}
visited[u]=1;
for(int j=1;j<=total;j++)
{
if(visited[j]==0&&edge[u][j]!=inf&&d[u]+edge[u][j]<d[j])
{
d[j]=d[u]+edge[u][j];
}
}
}
for(int i=1;i<=total_point;i++)
{
sum_dist+=d[i];
if(min_dist>d[i])
min_dist=d[i];
if(d[i]>range)
{
dist.push_back(-1);
house_dist.push_back(inf);
return;
}
}
dist.push_back(min_dist);
house_dist.push_back(sum_dist);
}
int main()
{
int total_point,total_candidate,line_num,range;
cin>>total_point>>total_candidate>>line_num>>range;
//fill(edge,edge+1020,inf);
for(int i=0;i<=total_point+total_candidate;i++)
{
for(int j=0;j<=total_point+total_candidate;j++)
edge[i][j]=inf;
edge[i][i]=0;
}
for(int i=0;i<line_num;i++)
{
string a,b;
int weight;
cin>>a>>b>>weight;
int p1,p2;
if(a[0]=='G')
{
a=a.substr(1);
p1=stoi(a)+total_point;
}
else
p1=a[0]-'0';
if(b[0]=='G')
{
b=b.substr(1);
p2=stoi(b)+total_point;
}
else
p2=b[0]-'0';
edge[p1][p2]=weight;
edge[p2][p1]=weight;
}
dist.push_back(0);
house_dist.push_back(0);
for(int i=1;i<=total_candidate;i++)
{
Dijtstra(i+total_point,total_candidate,total_point,range);
}
int _min=1;
for(int i=2;i<dist.size();i++)
{
if(dist[i]>dist[_min])
{
_min=i;
}
else if(dist[i]==dist[_min])
{
if(house_dist[i]<house_dist[_min])
_min=i;
}
}
if(dist[_min]>0)
{
cout<<"G"<<_min<<endl;
float sum=house_dist[_min]*1.0/total_point;
printf("%.1f %.1f",dist[_min]*1.0,sum);
}
else
cout<<"No Solution"<<endl;
return 0;
}