1072. Gas Station

来源:互联网 发布:java求质数 编辑:程序博客网 时间:2024/06/15 04:29

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 51 2 21 4 21 G1 41 G2 32 3 22 G2 13 4 23 G3 24 G1 3G2 G1 1G3 G2 2
Sample Output 1:
G12.0 3.3
Sample Input 2:
2 1 2 101 G1 92 G1 20
Sample Output 2:
No Solution
选择location的条件:
1、所有house到location的距离均在D内
2、house距离location的最小值最大
3、若上述解不唯一,选择平均距离最小的location
4、还不唯一则选择index最小的location

Dijkstra即可

#include<iostream>#include<vector>#include<algorithm>#include<string>#include<sstream>using namespace std;const int INF=0x7fffffff;int n,m,k,D;int dis[1020][1020];int maxd=INF;int minid=0;int s2i(string s){stringstream ss;int end;if(s[0]=='G'){ss<<s.substr(1);ss>>end;end+=n;}else{ss<<s;ss>>end;}return end;}bool dijkstra(int start){int cnt=0;vector<int>dist(m+n+1,INF);vector<bool>visit(m+n+1,false);dist[start]=0;while(cnt<n){int mind=INF;int minn;for(int i=1;i<=n+m;++i)if((!visit[i])&&dist[i]<mind){mind=dist[i];minn=i;}if(dist[minn]>D)return false;if(minn<=n)cnt++;visit[minn]=true;for(int i=1;i<=m+n;++i){if(visit[i]||dis[minn][i]==INF)continue;if(dist[i]>mind+dis[minn][i])dist[i]=mind+dis[minn][i];}}int sum=0;int mind=INF;for(int i=1;i<=n;++i){sum+=dist[i];if(dist[i]<mind)mind=dist[i];}if(mind<minid)return false;else if(mind==minid&&sum>=maxd)return false;maxd=sum;minid=mind;return true;}int main(){cin>>n>>m>>k>>D;for(int i=1;i<=n+m;++i)for(int j=1;j<=n+m;++j)dis[i][j]=dis[j][i]=INF;for(int i=0;i<k;++i){string s1,s2;int d;cin>>s1>>s2>>d;int s=s2i(s1),e=s2i(s2);dis[s][e]=dis[e][s]=d;}int solu=-1;for(int i=n+1;i<=n+m;++i)if(dijkstra(i))solu=i-n;if(solu==-1)cout<<"No Solution"<<endl;else{cout<<"G"<<solu<<endl;double mindis=minid;double avedis=(double)maxd/n;printf("%.1lf %.1lf\n",mindis,avedis);}return 0;}


0 0
原创粉丝点击