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
实在很无奈了 ,因为输入的处理问题,最后一个测试点 运行时错误或者wa 自己在G这块没有好好考虑清楚难受
还有就是........ 跑M次的dij 求出来即可
还有就是题目我真的看不太懂 本以为只要输出avg最小的,相同的话id最小就可以了并且在范围内
结果发现还要先判断输出最小的距离最大。。。。这边我真的没有想到.....一直在纠结是不是自己的dj跑错了
好吧只能说要好好审题
代码
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int maze[1025][1025];
int di[1025],vis[1025];
int n, m, k,ds,dis;
struct node
{
double minn,avg;
int id;
}s[15];
vector<node>vec;
bool cmp(node a, node b)
{
if(a.minn != b.minn)
return a.minn > b.minn; //选出居住点中距离最大的
if (a.avg != b.avg)
return a.avg < b.avg;
else
return a.id < b.id;
}
void dij(int x)
{
fill(vis,vis + 1025,0);
fill(di,di+1025,INF);
vis[x] = 1;
for(int i = 1; i <= n + m; i++) //最后的那些Gi不用管
di[i] = maze[x][i];
for(int i = 1; i <= n + m; i++)
{
//寻找最小的
int minn = INF,u = 0;
for(int j = 1; j <= n + m; j++ )
{
if(vis[j] == 0 && di[j] < minn)
{
minn = di[j];
u = j;
}
}
if(minn == INF)
break;
vis[u] = 1;
for(int j = 1; j <= n + m ; j++)
{
if(!vis[j] && di[u] + maze[u][j] < di[j])
{
di[j] = di[u] + maze[u][j];
}
}
}
return;
}
int main()
{
char s1[5],s2[5];
scanf("%d %d %d %d",&n,&m,&k,&ds);
for(int i = 1; i <= n+m; i++ )
{
for(int j = 1; j <= n+m; j++)
maze[i][j] = INF;
}
for(int i = 1; i <= k; i++)
{
char p1[5],p2[5];
int dis;
int a = 0,b = 0;
scanf("%s %s %d",p1,p2,&dis);
if(p1[0] != 'G')
{
int j = 0;
while(p1[j] != '\0')
a = a * 10 + (int)(p1[j++] - '0');
}
else
{
int j = 1;
while(p1[j] != '\0')
a = a * 10 + (int)(p1[j++] - '0');
a = n + a;
}
if(p2[0] != 'G')
{
int j = 0;
while(p2[j] != '\0')
b = b * 10 + (int)(p2[j++] - '0');
}
else
{
int j = 1;
while(p2[j] != '\0')
b = b * 10 + (int)(p2[j++] - '0');
b = b + n;
}
maze[a][b] = maze[b][a] = dis;
} //包括如果有重边的情况都处理了
for(int i = 1; i <= m; i++)//然后现在跑M次的dij
{
dij(i + n); //表示是Gi
int u;
double sum = 0 ;
int minn = INF;
for( u = 1; u <= n; u++)
{
if(di[u] > ds)
break;
sum += di[u];
minn = min(minn, di[u]);
}
if (u == n + 1)
{
node kk;
kk.avg = double(sum * 1.0 / n);
kk.minn = double(minn);
kk.id = i;
vec.push_back(kk);
}
}
if(vec.size() == 0)
{
printf("No Solution\n");
return 0;
}
sort(vec.begin(),vec.end(),cmp);
printf("G%d\n",vec[0].id);
printf("%.1lf %.1lf",vec[0].minn,vec[0].avg);
return 0;
}