PAT Advanced Level 1072
来源:互联网 发布:警告本网站域名在美国 编辑:程序博客网 时间:2024/05/18 00:15
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1072
代码如下:
#include <stdio.h>#include <stdlib.h>#include <string.h>#define vmax 0x7fffffffint p[1011][1011];int dis[1011];int sign[1011];int n,m,k,ran;int no(char *s){ if(s[0]=='G') return atoi(s+1)+n; else return atoi(s);}int main(){ freopen("C:\\Users\\Devon\\Desktop\\input.txt","r",stdin); scanf("%d%d%d%d",&n,&m,&k,&ran); memset(p,-1,sizeof(p)); for(int i=0;i<k;i++){ char temp1[9]; int p1,p2; char temp2[9]; int temp; scanf("%s%s%d",temp1,temp2,&temp); p1=no(temp1); p2=no(temp2); p[p1][p2]=temp; p[p2][p1]=temp; } int retno=-1; int retm=0; double reta=0; int has=0; for(int i=n+1;i<=n+m;i++){ for(int k=0;k<=m+n;k++) dis[k]=vmax;//此处耗时,应该使用-1或者0 memset(sign,0,sizeof(sign)); dis[i]=0; while(1){ int temp=-1; int minDis=vmax; for(int k=1;k<=m+n;k++){ if(sign[k]==0&&dis[k]<minDis) minDis=dis[k],temp=k; } if(temp==-1) break;//先前对跳出循环的条件判断错误(很像但不正确),存在无效站点 sign[temp]=1; for(int k=1;k<=m+n;k++){ if(sign[k]==0&&p[temp][k]!=-1&&dis[temp]+p[temp][k]<dis[k]){ dis[k]=dis[temp]+p[temp][k]; } } } int minDis=vmax; int maxDis=0; int totalDis=0; for(int k=1;k<=n;k++){ totalDis+=dis[k]; if(dis[k]<minDis) minDis=dis[k]; if(dis[k]>maxDis) maxDis=dis[k]; } if(maxDis<=ran){ has=1; if(retm<minDis||(retm==minDis&&reta>totalDis*1.0/n)){ retno=i-n; retm=minDis; reta=totalDis*1.0/n; } } } if(has){ printf("G%d\n",retno); printf("%.1lf %.1lf\n",retm*1.0,reta); } else printf("No Solution\n"); return 0;}
0 0
- PAT Advanced Level 1072
- Pat(Advanced Level)Practice--1072(Gas Station)
- PAT Advanced Level 1077
- PAT Advanced Level 1078
- PAT Advanced Level 1079
- PAT Advanced Level 1080
- PAT Advanced Level 1010
- PAT Advanced Level 1016
- PAT Advanced Level 1018
- PAT Advanced Level 1039
- PAT Advanced Level 1060
- PAT Advanced Level 1026
- PAT Advanced Level 1029
- PAT Advanced Level 1030
- PAT Advanced Level 1033
- PAT Advanced Level 1034
- PAT Advanced Level 1038
- PAT Advanced Level 1045
- Effective C++:条款21:必须返回对象时别妄想返回其reference
- HDU1236 排名 题解
- “ASP.default_aspx”并不包含“DropDownList1_SelectedIndexChanged”的定义,其解决方法。
- hdu1815 2sat + 二分 + 建图<不错的题目>
- 手机变砖进入sw模式刷bin的问题
- PAT Advanced Level 1072
- 关于java中HashMap遍历效率问题
- VS中创建静态库&C/C++静态库的使用
- PAT Advanced Level 1076
- sqlite3数据类型
- 《python源码剖析》笔记 python中的List对象
- CC150第14章JAVA总结 to be continued
- 写在读完“Android消息处理机制”后
- 什么是脚本语言