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 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<cstring>
#include<algorithm>
using namespace std;
const int N=1020,INF=0x3f3f3f3f;
int n,m,k,D;
int g[N][N];
int dist[N];
bool st[N];
int get(string s)
{
if(s[0]=='G') return n+stoi(s.substr(1));
return stoi(s);
}
void dijkstra(int start,int &mind,int &sumd)
{
memset(dist,0x3f,sizeof dist);
memset(st,0,sizeof st);//因为枚举,所以每次都有进行初始化
dist[start]=0;
for(int i=0;i<n+m;i++)
{
int t=-1;
for(int j=1;j<=n+m;j++)
if(!st[j]&&(t==-1||dist[j]<dist[t]))
t=j;
st[t]=true;
for(int j=1;j<=n+m;j++)
dist[j]=min(dist[j],dist[t]+g[t][j]);
}
//求完最短路,再算
for(int i=1;i<=n;i++)
if(dist[i]>D)//有没有存在不在最大服务范围内
{
mind=-1; //有直接范围失败,
return;
}
mind=INF,sumd=0;
for(int i=1;i<=n;i++)
{
mind=min(mind,dist[i]); //取该加油站附件最近的村庄
sumd+=dist[i]; //加油站到所以村庄距离之和
}
}
int main()
{
cin>>n>>m>>k>>D;
memset(g,0x3f,sizeof g); //初始化图
while(k--)
{
string a,b;
int z;
cin>>a>>b>>z;
int x=get(a),y=get(b);
g[x][y]=g[y][x]=min(g[x][y],z); //避免重边,取最小的
}
int res=-1,mind=0,sumd=INF; //最优变量
for(int i=n+1;i<=n+m;i++)
{
int mind1,sumd1; //局部变量
dijkstra(i,mind1,sumd1);
if(mind1>mind) res=i,mind=mind1,sumd=sumd1; //条件1:mind1!=-1,表示在服务范围内,条件2:如果最近村庄更远,就选它
else if(mind1==mind&&sumd1<sumd) res=i,sumd=sumd1; //条件三:如果最近村庄距离相等,就选到所有村庄距离最小的
}
if(res==-1) printf("No Solution\n");
else printf("G%d\n%.1lf %.1lf\n",res-n,(double)mind,(double)sumd/n+1e-8);//3.25 保持成3.2499999999...,保留一位就变成了了3.2,所以加个很小的数变回来
return 0;
}
stoi是啥啊
string类型转int类型的函数