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.
1.题意需要稍微费点时间理解一下,找一个加油站,到任意居民点的最小距离最大
2.算法:dijkstra or spfa 等最短路算法
floyd复杂度太高不适用本题
3.难点:加油站的输入是字符串形式,可以写个函数处理一下
G1=n+1
G2=n+2
n为居民点个数
这样做一下转化就好了
然后对每一个加油站使用一次spfa求出到所有点的最短路,对得出来的路径分析一下即可出答案
这种题目建议先画图出样例
距离可以用邻接矩阵代替,代码量会少很多
#include <bits/stdc++.h>
using namespace std;
#define FORP(i,a,b) for(int i=a;i<=b;i++)
#define mp(a,b) make_pair(a,b)
#define maxn 202000
#define debug(a) (cout<<"="<<a<<endl)
int n,l,m,k,d;
int ddd[1050][1050];
std::vector<int> v[1060];
int cal(string a){
int ans=0;
for(int i=0;i<a.size();i++){
if(a[i]=='G') continue;
ans=ans*10+a[i]-'0';
}
return ans;
}
int dis[1200];
bool inq[1200];
int tot;
bool ok;
int Min;
void spfa(int x){
memset(dis,0x3f,sizeof(dis));
memset(inq,0,sizeof(inq));
dis[x]=0;
inq[x]=1;
queue<int> q;
q.push(x);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=0;i<v[u].size();i++){
int to=v[u][i];
if(ddd[u][to]+dis[u]<dis[to]){
if(!inq[to]){
inq[to]=1;
q.push(to);
}
dis[to]=ddd[u][to]+dis[u];
}
}
inq[u]=false;
}
FORP(i,1,n){
tot+=dis[i];
Min=min(Min,dis[i]);
if(dis[i]>d){
ok=false;
return;
}
}
}
int main(){
memset(ddd,0x3f,sizeof(ddd));
ios::sync_with_stdio(false);
cin>>n>>m>>k>>d;
FORP(i,1,k){
string a,b;
cin>>a>>b;
int dist;cin>>dist;
int aa,bb;
if(a[0]=='G'){
aa=cal(a)+n;
}
else aa=cal(a);
if(b[0]=='G'){
bb=cal(b)+n;
}
else bb=cal(b);
ddd[aa][bb]=dist;
ddd[bb][aa]=dist;
v[aa].push_back(bb);
v[bb].push_back(aa);
}
int ans=0;
int gas=-1;
int mm=0;
for(int i=n+1;i<=n+m;i++){
ok=true;
tot=0;
Min=0x3f3f3f3f;
spfa(i);
if(ok){
if(Min>mm){
ans=tot;
gas=i;
mm=Min;
}
else if(Min==mm){
if(tot<ans){
ans=tot;
gas=i;
}
}
}
}
if(mm==0){
cout<<"No Solution"<<endl;
}
else{
cout<<"G"<<gas-n<<endl;
printf("%.1lf %.1lf\n",mm*1.0,ans*1.0/n);
}
return 0;
}