Gas station
A gas station have to being 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 is in its service range.
Now given the map of the city and several candidate locations for the gas station, you is supposed to give the best recom Mendation. If there is more than one solution, the output of the one with the smallest average distance to all the houses. If Such a solution is still not unique, the output of the one with the smallest index number.
Test instructions a very complicated question. I sort of sorted it out. That's pretty much it.
1. Figure out the distance from each gas station to all house.
2. All distances must be less than the maximum service distance of the gas station
3. Find a minimum distance from all distances
4. Take the maximum distance at the minimum distance per gas station
5. If the minimum distance is equal, the average distance is minimized.
6. If the average distance is the smallest, take the smallest number
Are the steps above for this problem almost dizzy? The algorithm should still be simple is the Dijkstra algorithm
The AC code is given below
1#include <stdio.h>2#include <stdlib.h>3 #defineMAX 10000004 5 6 intg[1011][1011];7 intN,m,k,ds;8 intdis[1011];9 intflag[1011];Ten floatsum[Ten]; One A voidDijkstraintv) - { - inti,j,pos,min; thedis[v]=0; - - for(i=1; i<=n+m;i++) - { +min=MAX; - for(j=1; j<=n+m;j++) + { A if(flag[j]!=1&& dis[j]<min) at { -min=Dis[j]; -pos=J; - } - } -flag[pos]=1; in for(j=1; j<=n+m;j++) - { to if(flag[j]!=1) + { - if(dis[pos]+g[pos][j]<Dis[j]) the { *dis[j]=dis[pos]+G[pos][j]; $ }Panax Notoginseng } - } the + } A } the intMain () + { - inti,j; $ ints3,s4,l,k,pos=0, t=0, flag1=0; $ intmin[Ten]; - floatAve,ave2,max; - Chars1[Ten],s2[Ten]; the -scanf"%d%d%d%d",&n,&m,&k,&Ds); Wuyi for(i=1; i<=n+m;i++) the { -dis[i]=MAX; Wu for(j=1; j<=n+m;j++) -g[i][j]=MAX; About } $ for(i=1; i<=k;i++) - { -scanf"%s%s%d",s1,s2,&L); - A if(s1[0]=='G') +S3=atoi (s1+1)+N; the Else -s3=atoi (S1); $ if(s2[0]=='G') theS4=atoi (s2+1)+N; the Else thes4=atoi (S2); theg[s3][s4]=g[s4][s3]=L; - } in the for(i=n+1; i<=n+m;i++) the { About Dijkstra (i); themin[t]=dis[1]; the for(j=1; j<=n;j++) the { + if(dis[j]>Ds) - { themin[t]=-1;Bayisum[t]=-1; the Break; the } - if(min[t]>Dis[j]) - { themin[t]=Dis[j]; the } thesum[t]+=Dis[j]; the } -t++; the for(j=1; j<=n+m;j++) the { thedis[j]=MAX; 94flag[j]=0; the } the } the for(i=0; i<m;i++)98 { About if(min[i]!=-1) - {101flag1=1;102 Break;103 104 } the }106max=-100000;107 for(i=0; i<m;i++)108 {109 if(flag1==0) the Break;111 if(max<min[i]&&min[i]!=-1) the {113max=Min[i]; thepos=i; theave=sum[pos]/N; the } 117 Else if(max==min[i]&&min[i]!=-1)118 {119ave=sum[pos]/N; -ave2=sum[i]/N;121 if(ave>ave2)122 {123max=Min[i];124pos=i; theAve=ave2;126 } 127 - }129 } the if(flag1==1)131printf"g%d\n%.1f%.1f\n", pos+1, Max,ave); the Else133printf"No solution\n");134}
PAT gas Station