# PAT甲级1072. Gas Station (30)

xiaoxiao2021-03-26  4

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

//Dijkstra #include <cstdio> using namespace std; #include <cstdlib> #include <queue> #include <vector> #include <cstring> const int MAX=1000+20; const int INF=(1<<30); int N,M,K,Ds; int Dis[MAX][MAX]; //邻接表。distance of roads connecting houses and stations int vis[MAX]={0}; int Len[MAX]; //Len[i][j]表示加油站i到house j的最短距离 int g=0;//候选加油站编号 struct cmp{ bool operator()(const int&a,const int&b){ return Len[g][a]>Len[g][b]; } }; int main(){ scanf("%d %d %d %d",&N,&M,&K,&Ds); /*初始化*/ for(int i=0;i<MAX;i++){ for(int j=0;j<MAX;j++){ if(i==j) Dis[i][j]=0; else Dis[i][j]=INF; } } for(int i=0;i<10;i++){ for(int j=0;j<MAX;j++){ Len[i][j]=INF; } } char P1,P2; int u,v; int D; for(int i=0;i<K;i++){ scanf("%s %s %d",P1,P2,&D); if(P1=='G') u=N+atoi(P1+1); else u=atoi(P1); if(P2=='G') v=N+atoi(P2+1); else v=atoi(P2); Dis[u][v]=D; Dis[v][u]=D; } /*Dijkstra*/ int index=-1; double minDis=-1.0,totalDis=0.0; for(g=1;g<=M;g++){ memset(vis,0,sizeof(vis)); Len[g][g+N]=0; priority_queue<int,vector<int>,cmp> PQ; PQ.push(g+N); while(!PQ.empty()){ int v=PQ.top(); // printf("v=%d\n",v); PQ.pop(); vis[v]=1; for(int w=1;w<=N+M;w++){ if(vis[w]==0&&Dis[v][w]<INF){ if(Len[g][v]+Dis[v][w]<Len[g][w]) { Len[g][w]=Len[g][v]+Dis[v][w]; PQ.push(w); // printf("w=%d\n",w); } } } } /*求加油站g到house的距离最小值*/ int minimum=MAX; int total=0;//加油站g到所有房子的总距离 int flag=1; for(int i=1;i<=N;i++){ if(Len[g][i]>Ds) { //有house不在station的服务范围内 flag=0; break; } if(Len[g][i]<minimum) { minimum=Len[g][i]; } total+=Len[g][i]; } /*看minimum是否是所有加油站中的最优*/ if(flag){ if(minimum>minDis){ minDis=minimum; index=g; totalDis=total; } else if(minimum==minDis){ if(total<totalDis) { index=g; totalDis=total; } } } } if(minDis==-1) printf("No Solution"); else{ printf("G%d\n",index); printf("%.1lf %.1lf",minDis,(totalDis+0.001)/N); } return 0; }