PAT (Advanced Level) 1072. Gas Station (30)
枚举一下选的位置,每次算一下就可以了。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=+;
int n,m,k,ds;
struct Edge
{
int u,v;
int dis;
}e[*maxn];
vector<int>g[maxn];
int tot;
int flag[maxn],dis[maxn];
double ans1,ans2;
int ans;
int MIN; int get(char *s)
{
int num=;
if(s[]!='G')
{
for(int i=;s[i];i++)
num=num*+s[i]-'';
}
else
{
for(int i=;s[i];i++)
num=num*+s[i]-'';
num=num+n;
}
return num;
} void SPFA(int s)
{
queue<int>Q;
memset(flag,,sizeof flag);
for(int i=;i<=n+m;i++) dis[i]=INF;
Q.push(s); flag[s]=; dis[s]=;
while(!Q.empty())
{
int head=Q.front(); Q.pop(); flag[head]=;
for(int i=;i<g[head].size();i++)
{
int id=g[head][i];
if(dis[head]+e[id].dis<dis[e[id].v])
{
dis[e[id].v]=dis[head]+e[id].dis;
if(flag[e[id].v]==)
{
flag[e[id].v]=;
Q.push(e[id].v);
}
}
}
}
} int main()
{
scanf("%d%d%d%d",&n,&m,&k,&ds); tot=;
for(int i=;i<=k;i++)
{
char u[],v[]; int dis;
scanf("%s%s%d",u,v,&dis);
e[tot].u=get(u),e[tot].v=get(v);
e[tot].dis=dis;
g[get(u)].push_back(tot),tot++; e[tot].u=get(v),e[tot].v=get(u);
e[tot].dis=dis;
g[get(v)].push_back(tot),tot++;
} ans1=1.0*INF;
ans2=1.0*INF;
MIN=; for(int i=n+;i<=n+m;i++)
{
SPFA(i);
int sum=,MIN_NOW=INF;
for(int j=;j<=n;j++)
{
if(dis[j]>ds) {sum=-;break;}
else
{
sum=sum+dis[j];
MIN_NOW=min(MIN_NOW,dis[j]);
}
}
if(sum==-) continue; if(MIN_NOW>MIN)
{
MIN=MIN_NOW;
ans2=1.0*sum/n;
ans1=1.0*MIN;
ans=i;
} else if(MIN_NOW==MIN&&1.0*sum/n<ans2)
{
ans2=1.0*sum/n;
ans=i;
}
}
if(MIN==) printf("No Solution\n");
else
{
printf("G%d\n",ans-n);
printf("%.1lf %.1lf\n",ans1,ans2);
}
return ;
}
PAT (Advanced Level) 1072. Gas Station (30)的更多相关文章
- 【PAT甲级】1072 Gas Station (30 分)(Dijkstra)
题意: 输入四个正整数N,M,K,D(N<=1000,M<=10,K<=10000)分别表示房屋个数,加油站个数,路径条数和加油站最远服务距离,接着输入K行每行包括一条路的两条边和距 ...
- 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise
题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...
- pat 甲级 1072. Gas Station (30)
1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...
- PAT 甲级 1072 Gas Station (30 分)(dijstra)
1072 Gas Station (30 分) A gas station has to be built at such a location that the minimum distance ...
- PAT Advanced 1072 Gas Station (30) [Dijkstra算法]
题目 A gas station has to be built at such a location that the minimum distance between the station an ...
- PAT 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- 1072. Gas Station (30)
先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...
- 1072. Gas Station (30) 多源最短路
A gas station has to be built at such a location that the minimum distance between the station and a ...
- 1072 Gas Station (30)(30 分)
A gas station has to be built at such a location that the minimum distance between the station and a ...
随机推荐
- 高效程序猿之 VS2010快速生成代码模板
本文转载,原文地址 http://edi.wang/Post/2011/12/8/efficient-code-monkey-1-vs2010-generate-code-template 一只程序猿 ...
- hdu_5762_Teacher Bo(鸽笼原理)
题目链接:hdu_5762_Teacher Bo 题意: 给你n个点,问你能否找到两对点的曼哈顿距离相等 题解: 最开始看到这题,看数据以为要向nlogn的复杂度发展,结果经验误导了自己,我们仔细观察 ...
- 运行出第一个程序Hello World、第二个程序网页浏览器
很长时间没有发博客记录我的奋斗历程了,原因不外乎遇到了对我而言“巨大的”困难. 经历了长期的找不到合适的教材.找不到Xcode资源.运行不出例程的痛苦以后,近日终于走上正轨. 现在补发一个过去2个月来 ...
- .htaccess重写URL讲解
使用ThinkPHP和Laravel等框架的都知道,所以的请求都需要经过index.php文件入口,无论你的URI是什么. 当然除了访问的是静态文件或者访问路径的文件真实存在,例如你访问xxx.com ...
- apache启动报错:Cannot load php5apache2_2.dll into server
错误信息: httpd.exe: Syntax error on line 178 of D:/Program Files/httpd-2.4.20-x64-vc14-r2 /Apache24/con ...
- Ajax及select级联
cascade.jsp(Test/WebContent/jsp/cascade.jsp): <%@ page language="java" contentType=&quo ...
- weblogic 密码加密
先启动域:
- 虚拟机centos分区
在计算机上安装 Linux 系统,对硬盘进行分区是一个非常重要的步骤,下面介绍几个分区方案. 方案 1 / :建议大小在 5GB 以上. swap :即交换分区,建议大小是物理内存的 1~2 倍. 方 ...
- C++调用C#之C# COM控件
C#做界面真的是比C++方便多了,所以尝试了一下,使用C++做核心功能(例如绘图),然后用C#来做节目(例如对话框),考虑到以后可能不能使用.net,使用DLL做一个隔离层,隔离C++和C#,方便以后 ...
- DrawerLayout和ActionBarDrawerToggle
说明:DrawerLayout的作用就是左侧滑出.右侧滑出菜单.需要和ActionBarDrawerToggle来配合使用.如果对ActionBar不理解,请先熟悉ActionBar. 1.首先配置D ...