【最短路】【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 ...
随机推荐
- springmvc4处理get和post请求中文乱码问题
1.在springmvc4处理get和post请求的问题 参看大牛博客连接:https://blog.csdn.net/qq_41665356/article/details/80234392
- js 作用域链&内存回收&变量&闭包
闭包主要涉及到js的几个其他的特性:作用域链,垃圾(内存)回收机制,函数嵌套,等等 一.作用域链:函数在定义的时候创建的,用于寻找使用到的变量的值的一个索引,而他内部的规则是,把函数自身的本地变量放在 ...
- E题hdu 1425 sort
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 sort Time Limit: 6000/1000 MS (Java/Others) M ...
- 阅读关于DuReader:百度大规模的中文机器阅读理解数据集
很久之前就得到了百度机器阅读理解关于数据集的这篇文章,今天才进行总结!.... 论文地址:https://arxiv.org/abs/1711.05073 自然语言处理是人工智能皇冠上的明珠,而机器阅 ...
- Bitmap算法应用实例(转)
关于数据库查询工作,例如有如下数据库表 要想统计所有90后的程序员该怎么做呢?用一条求交集的SQL语句即可: Select count(distinct Name) as 用户数 from table ...
- 广度优先算法(BFS)与深度优先算法(DFS)
一.广度优先算法BFS(Breadth First Search) 基本实现思想 (1)顶点v入队列. (2)当队列非空时则继续执行,否则算法结束. (3)出队列取得队头顶点v: (4)查找顶点v的所 ...
- 用vue实现登录页面
vue和mui一起完成登录页面(在hbuilder编辑器) <!DOCTYPE html> <html> <head> <meta charset=" ...
- Winform利用委托进行窗体间的传值
在form1.cs中 1.委托的定义 //定义一个委托 public delegate void AddUsrEventHandler(object sender, AddUsrEventHandle ...
- Homebrew安装gradle及配置myeclipse
brew install gradle 对,你没看错.就只有一行命令搞定. 然后验证安装 nicknailodeMacBook-Pro:~ nicknailo$ gradle -v --------- ...
- 五十八 数据库访问使用SQLite
SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. Python就 ...