Pat(Advanced Level)Practice--1072(Gas Station)
来源:互联网 发布:sql 别名 列名无效 编辑:程序博客网 时间:2024/09/21 09:19
Pat1072代码
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 2Sample Output 1:
G12.0 3.3Sample Input 2:
2 1 2 101 G1 92 G1 20Sample Output 2:
No Solution
#include<cstdio>#include<cstdlib>#define MAX 1015#define INF 0x0FFFFFFFusing 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);}elseprintf("No Solution\n");return 0;}结果最后一个case超时,毕竟是O(n^3)的时间复杂度,改用Dijkstra算法就可以AC掉了。
#include<cstdio>#include<cstdlib>#define MAX 1015#define INF 0x0FFFFFFFusing 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);}elseprintf("No Solution\n");return 0;}
- Pat(Advanced Level)Practice--1072(Gas Station)
- PAT (Advanced Level) Practise 1072 Gas Station (30)
- PAT (Advanced Level) Practise 1072 Gas Station (30)
- 【PAT】【Advanced Level】1072. Gas Station (30)
- pat 1072 Gas Station
- PAT 1072 Gas Station
- PAT (Advanced Level) 1072. Gas Station (30) Dijkstra最短路径+剪枝
- 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practice 1006
- Pat(Advanced Level)Practice--1025(PAT Ranking)
- Pat(Advanced Level)Practice--1075(PAT Judge)
- PAT 1072 Gas Station SPFA
- Pat(Advanced Level)Practice--1003(Emergency)
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- Pat(Advanced Level)Practice--1008(Elevator)
- Pat(Advanced Level)Practice--1010(Radix)
- Pat(Advanced Level)Practice--1015(Reversible Primes)
- Pat(Advanced Level)Practice--1024(Palindromic Number)
- Android Performance(3) Hierarchy Viewer
- 如何更好利用恢复软件恢复清除的数据
- 代码面试最常用的10大算法
- IT职场生存的一些建议
- what is SW framework
- Pat(Advanced Level)Practice--1072(Gas Station)
- C# winform 上传图片,显示图片和gridview中添加按钮
- Android 命名规范 (提高代码可以读性)
- JQuery属性操作方法
- 反编译工具dex2jar和jd_gui使用方法
- ships in 24hrs
- 在.net中序列化读写xml方法的总结
- IT人士常犯的17个职场错误
- 指针函数和函数指针有什么区别