PAT1072. Gas Station
来源:互联网 发布:慧心莲禅服 淘宝 编辑:程序博客网 时间:2024/05/16 15:18
最多只有一个加油站符合,所以把所有加油站存起来是不必要的,保存一个当前加油站即可
#include<cstdio>#include<algorithm>#include<numeric>using namespace std;const int N=1020,M=0x0fffffff;int n,m,k,limit;int dis[N][N],site,gtotal,gmin;void check(int k){ int dist[N]; bool used[N]; fill_n(used,N,false); fill_n(dist,N,M); dist[k]=0; while(true) { int t=-1; for(int mmin=M,i=0;i<n+m;++i) if(!used[i]&&dist[i]<mmin) mmin=dist[t=i]; if(-1==t)break; used[t]=true; for(int i=0;i<n+m;++i) if(dis[t][i]!=M&&(dist[i]>dist[t]+dis[t][i])) dist[i]=dist[t]+dis[t][i]; } if(*max_element(dist,dist+n)<=limit) { int a=accumulate(dist,dist+n,0); int b=*min_element(dist,dist+n); if(b>gmin) {gmin=b;gtotal=a;site=k;} else if(b==gmin) { if(a<gtotal) {gtotal=a;site=k;} else if(a==gtotal&&k<site) site=k; } }}int no(){ char s[7]; scanf("%s",s); if(s[0]=='G') return atoi(s+1)+n-1; return atoi(s)-1;}int main(){ fill_n(*dis,N*N,M); scanf("%d%d%d%d",&n,&m,&k,&limit); while(k--) { int a=no(),b=no(); scanf("%d",&dis[b][a]); dis[a][b]=dis[b][a]; } for(int i=n;i<m+n;++i) check(i); if(!site) printf("No Solution\n"); else printf("G%d\n%d.0 %.1f\n", site+1-n,gmin,(double)gtotal/n); return 0;}
0 0
- PAT1072. Gas Station
- pat1072. Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- dumpbin的使用
- linux获取系统时间
- XML - JAXB示例
- [十进制小数] 与 [二进制小数] 互相转换
- form表单的onsubmit()不起作用的问题
- PAT1072. Gas Station
- 教你如何将hadoop Eclipse 整合
- 购物网站中购物车的简单实现
- ecshop二次开发之商品属性选择性遍历
- Git历险记(四)
- RandomAccessFile【1】
- NSTableView透明化
- C语言基于链表的栈
- <unix高级编程>笔记 chapter1