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 2Sample Output 1:
G1 2.0 3.3Sample Input 2:
2 1 2 10 1 G1 9 2 G1 20Sample Output 2:
No Solution
#include<cstdio> #include<cstdlib> #define MAX 1015 #define INF 0x0FFFFFFF using namespace std; int map[MAX][MAX]; int dis[MAX][MAX]; int Transform(char *str,int n)//转换坐标,将加油站的编号加到房子编号之后 { if(str[0]=='G') return atoi(str+1)+n-1; else return atoi(str)-1; } void Floyd(int totalnum) { int i,j,k; for(i=0;i<totalnum;i++) for(j=0;j<totalnum;j++) dis[i][j]=map[i][j]; for(k=0;k<totalnum;k++) { for(i=0;i<totalnum;i++) { for(j=0;j<totalnum;j++) { if(dis[i][j]>dis[i][k]+dis[k][j]) dis[i][j]=dis[i][k]+dis[k][j]; } } } } int main(int argc,char *argv[]) { int n,m,k,d; int i,j; scanf("%d%d%d%d",&n,&m,&k,&d); for(i=0;i<MAX;i++) for(j=0;j<MAX;j++) map[i][j]=INF; for(i=0;i<k;i++) { int x,y; char str[10]; scanf("%s",str); x=Transform(str,n); scanf("%s",str); y=Transform(str,n); scanf("%d",&map[x][y]); map[y][x]=map[x][y]; } double ansmaxdis=-1,ansavgdis=INF; int choice=-1; int totalnum=n+m; Floyd(totalnum); for(i=n;i<totalnum;i++) { double mindis=INF,avgdis=0; int flag=1; for(j=0;j<n;j++) { if(dis[i][j]<mindis) mindis=dis[i][j]; if(dis[i][j]>d) { flag=0; break; } avgdis+=dis[i][j]; } if(flag==0) continue; avgdis/=n; if(mindis>ansmaxdis) { ansmaxdis=mindis; ansavgdis=avgdis; choice=i; } else if(mindis==ansmaxdis) { if(avgdis<ansavgdis) { choice=i; ansavgdis=avgdis; } } } if(choice!=-1) { printf("G%d\n",choice+1-n); printf("%.1f %.1f\n",ansmaxdis,ansavgdis); } else printf("No Solution\n"); return 0; }结果最后一个case超时,毕竟是O(n^3)的时间复杂度,改用Dijkstra算法就可以AC掉了。
#include<cstdio> #include<cstdlib> #define MAX 1015 #define INF 0x0FFFFFFF using namespace std; int map[MAX][MAX]; int dis[MAX],visited[MAX]; int Transform(char *str,int n)//转换坐标,将加油站的编号加到房子编号之后 { if(str[0]=='G') return atoi(str+1)+n-1; else return atoi(str)-1; } void Dijkstra(int gas,int dis[],int totalnum) { int i,j,k; int min=INF; for(i=0;i<totalnum;i++) { dis[i]=map[gas][i]; visited[i]=0; } for(i=1;i<totalnum;i++) { min=INF; for(j=0;j<totalnum;j++) { if(!visited[j]&&dis[j]<min) { min=dis[j]; k=j; } } visited[k]=1; for(j=0;j<totalnum;j++) { if(!visited[j]&&dis[k]+map[k][j]<dis[j]) dis[j]=dis[k]+map[k][j]; } } } int main(int argc,char *argv[]) { int n,m,k,d; int i,j; scanf("%d%d%d%d",&n,&m,&k,&d); for(i=0;i<MAX;i++) for(j=0;j<MAX;j++) map[i][j]=INF; for(i=0;i<k;i++) { int x,y; char str[10]; scanf("%s",str); x=Transform(str,n); scanf("%s",str); y=Transform(str,n); scanf("%d",&map[x][y]); map[y][x]=map[x][y]; } double ansmaxdis=-1,ansavgdis=INF; int choice=-1; int totalnum=n+m; for(i=n;i<totalnum;i++) { double mindis=INF,avgdis=0; int flag=1; Dijkstra(i,dis,totalnum); for(j=0;j<n;j++) { if(dis[j]<mindis) mindis=dis[j]; if(dis[j]>d) { flag=0; break; } avgdis+=dis[j]; } if(flag==0) continue; avgdis/=n; if(mindis>ansmaxdis) { ansmaxdis=mindis; ansavgdis=avgdis; choice=i; } else if(mindis==ansmaxdis) { if(avgdis<ansavgdis) { choice=i; ansavgdis=avgdis; } } } if(choice!=-1) { printf("G%d\n",choice+1-n); printf("%.1f %.1f\n",ansmaxdis,ansavgdis); } else printf("No Solution\n"); return 0; }