【最短路】【spfa】hdu6071 Lazy Running
给你一个4个点的环,问你从2号点出发, 再回到2号点,长度>=K的最短路是多少。环上的边长度不超过30000。

跑出来所有dis(2,j)以后,然后for一遍j,根据dis(2,j)+t*2*w>=K,解出来对于每个j而言最小的t,然后尝试更新答案即可。如果dis(2,j)已经大于等于K了,那直接用其尝试更新答案。
跟CDOJ1633很像。
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll ans,K;
int W,T,a[4];
typedef pair<int,int> Point;
queue<Point>q;
bool inq[4][60010];
int dis[4][60010];
void spfa()
{
memset(dis,0x7f,sizeof(dis));
memset(inq,0,sizeof(inq));
q.push(make_pair(1,0)); inq[1][0]=1; dis[1][0]=0;
while(!q.empty()){
Point U=q.front();
Point V=make_pair((U.first+1)%4,(U.second+a[U.first])%(2*W));
if(dis[V.first][V.second]>dis[U.first][U.second]+a[U.first]){
dis[V.first][V.second]=dis[U.first][U.second]+a[U.first];
if(!inq[V.first][V.second]){
q.push(V);
inq[V.first][V.second]=1;
}
}
V=make_pair((U.first+3)%4,(U.second+a[(U.first+3)%4])%(2*W));
if(dis[V.first][V.second]>dis[U.first][U.second]+a[(U.first+3)%4]){
dis[V.first][V.second]=dis[U.first][U.second]+a[(U.first+3)%4];
if(!inq[V.first][V.second]){
q.push(V);
inq[V.first][V.second]=1;
}
}
q.pop();
inq[U.first][U.second]=0;
}
}
int main(){
// freopen("1005.in","r",stdin);
// freopen("1005.out","w",stdout);
scanf("%d",&T);
for(;T;--T){
ans=9000000000000000000ll;
scanf("%lld",&K);
for(int i=0;i<4;++i){
scanf("%d",&a[i]);
}
W=min(a[1],a[0]);
spfa();
for(int i=0;i<2*W;++i){
if(dis[1][i]<2000000000){
// printf("%d: (%d)\n",i,dis[1][i]);
if(dis[1][i]<K){
ans=min(ans,(ll)dis[1][i]+(ll)(2*W)*((K-(ll)dis[1][i])/(ll)(2*W)+(ll)((K-(ll)dis[1][i])%(ll)(2*W)!=0)));
}
else{
ans=min(ans,(ll)dis[1][i]);
}
}
}
printf("%lld\n",ans);
}
return 0;
}
【最短路】【spfa】hdu6071 Lazy Running的更多相关文章
- 2017 Multi-University Training Contest - Team 4 hdu6071 Lazy Running
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6071 题目: Lazy Running Time Limit: 2000/1000 MS (J ...
- hdu-6071 Lazy Running
In HDU, you have to run along the campus for 24 times, or you will fail in PE. According to the rule ...
- HDU 6071 Lazy Running (同余最短路)
Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- 多校4 lazy running (最短路)
lazy running(最短路) 题意: 一个环上有四个点,从点2出发回到起点,走过的距离不小于K的最短距离是多少 \(K <= 10^{18} 1 <= d <= 30000\) ...
- HDU 6071 Lazy Running (同余最短路 dij)
Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- hdu 6071 Lazy Running 最短路建模
Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) P ...
- HDU 6071 - Lazy Running | 2017 Multi-University Training Contest 4
/* HDU 6071 - Lazy Running [ 建模,最短路 ] | 2017 Multi-University Training Contest 4 题意: 四个点的环,给定相邻两点距离, ...
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- HDU 6071 同余最短路 spfa
Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
随机推荐
- DotNETCore 学习笔记 MVC视图
Razor Syntax Reference Implicit Razor expressions <p>@DateTime.Now</p> <p>@DateTim ...
- perl中的默认变量与Z/map介绍
use v6; =begin pod @*ARGS 命令行参数, 不含脚本名 $*PROGRAM-NAME:当前运行脚本的相对路径 $*PROGRAM:当前运行脚本的文件名称 $*CWD:当前工作路径 ...
- ifa_local 和 ifa_address
ifa_local 和 ifa_address区别联系: 1. 在配置了支持广播的接口上,与IFA_LOCAL一样,同样表示本地ip地址: 2. 对于点对点链路,IFA_ADDRESS表示的是对端的地 ...
- C C++ 常被人问的问题分析
正文 - 开始了, 直接扯淡 以下都是自己面试中遇到的常见的问题.如有不妥的地方就当见笑了. 哈哈 1. 谈谈你们服务器的架构吧. 分析: 假如这是第一个问题, 你可以走了. 可能各方面原因他不想 ...
- Android IPC
1. 什么是Android IPC IPC:inter-process Commnication跨进程的通信,多进程之间的通信,不同的操作系统有不同的通信方式,Android继承自Linux,但其IP ...
- 生命周期(vue的钩子函数)
生命周期图示 创建前,创建后,挂载前,挂载后,更新前,更新后,销毁前,销毁后 beforeCreate:function(){ console.log('1-beforeCreate 组件还未被创建' ...
- dev....把pivotgridview和chart一起导出
首先~: 命名空间: using DevExpress.XtraPrinting;using DevExpress.XtraCharts.Native;using DevExpress.XtraPri ...
- [New learn]讲解Objective-c的block知识
1.简介 OC的Block感觉就是C中饿函数指针,提供回调功能,但是OC中的block比C的函数指针要更加强大,甚至可以访问本地变量和修改本地变量. block在oc中是一个对象,它可以像一般的对象那 ...
- nginx升级步骤
今天应开发的需求,需要在Nginx增加一个模块,并不能影响现有的业务,所以就必须要平滑升级Nginx,好了,不多说了 1:查看现有的nginx编译参数 /usr/local/nginx/sbin/ng ...
- display:inline、block、inline-block三者之间的区别
1. display:block就是将元素显示为块级元素. block元素的特点: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度:(<d ...