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 ...
随机推荐
- Apache Shiro 认证过程
3.1.1 示例 Shiro验证Subjects 的过程中,可以分解成三个不同的步骤: 1. 收集Subjects 提交的Principals(身份)和Credentials(凭证): 2. 提 ...
- 缓存2 动态缓存 memcached
01准备下载好memcached.exe文件-->放置c:\memcached目录中02准备下载好php_memcache.dll文件-->放置php\ext扩展目录中03配置 php.i ...
- Boxes in a Line(移动盒子)
You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to sim ...
- js阻止浏览器默认事件
1.阻止浏览器的默认行为 function stopDefault(e) { //如果提供了事件对象,则这是一个非IE浏览器 if(e && e.preventDefault) { / ...
- [转]关于Socket粘包问题
这两天看csdn有一些关于socket粘包,socket缓冲区设置的问题,发现自己不是很清楚,所以查资料了解记录一下: 一两个简单概念长连接与短连接:1.长连接 Client方与Server方先建立通 ...
- flex、As 3.0 小知识
<s:CheckBox id="checkBox" x="10" y="5" label="{new ObjectProx ...
- 使用SQLCMD在SQLServer执行多个脚本 转载
出处不明 概述: 作为DBA,经常要用开发人员提供的SQL脚本来更新正式数据库,但是一个比较合理的开发流程,当提交脚本给DBA执行的时候,可能已经有几百个sql文件,并且有执行顺序,如我现在工作的公司 ...
- 【翻译】Longest Palindromic Substring 最长回文子串
原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...
- 使用onclick跳转到其他页面。使用button跳转到指定url
1. onclick="javascript:window.location.href='aa.htm'" 2. onclick="location='URL'&quo ...
- php中header函数参数的 Cache-control:private,no-cache,must-revalidate,max-age 使用方法
网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为private.其作用根据 ...