lazy running(最短路)

题意: 一个环上有四个点,从点2出发回到起点,走过的距离不小于K的最短距离是多少 \(K <= 10^{18} 1 <= d <= 30000\)

看完这道题,觉得这是个智力题,想了一想,无从下手啊

每次总是看完题解,就豁然开朗

其实之前做过一个类似的题,题目

但是根本想不到第一步, 选2w来转换啊,而且也不知道正确性啊

这类题要仔细理解一番

#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int> using namespace std;
const LL inf = 1e18;
const int N = 6e4 + 10;
int g[4][4];
int mod;
LL K;
LL dis[4][N];
int inq[4][N];
void spfa(){
for(int i = 0;i < 4;i++){
for(int j = 0;j < mod;j++) {
dis[i][j] = inf,inq[i][j] = 0;
}
}
queue<P> q;
dis[1][0] = 0,inq[1][0] = 0;
q.push(P(1,0));
while(!q.empty()){
P cur = q.front();q.pop();
for(int i = -1;i <= 2;i+=2){
int x = (cur.first + i + 4)%4, y = (cur.second + g[cur.first][x])%mod;
if(dis[x][y] > dis[cur.first][cur.second] + g[cur.first][x]){
dis[x][y]= dis[cur.first][cur.second] + g[cur.first][x];
if(!inq[x][y]){
inq[x][y] = 1;
q.push(P(x,y));
}
}
}
}
}
int main()
{
int T;
cin>>T;
while(T--){
cin>>K;
for(int i = 0;i < 4;i++) {
scanf("%d",&g[i][(i+1)%4]);
g[(i+1)%4][i] = g[i][(i+1)%4];
}
mod = 2 * min(g[0][1],g[1][2]);
spfa();
LL ans = 2e18;
for(int i = 0;i < mod;i++){
LL d = K - dis[1][i];
ans = min(ans, dis[1][i] + (d + mod - 1) / mod * mod);
}
printf("%lld\n",ans);
}
return 0;
}

最新文章

  1. 20个高级Java面试题汇总
  2. 你需要知道的Sass插值
  3. Swift动画编程指南-01 简介
  4. 20145218 《Java程序设计》第三周学习总结
  5. java生态环境
  6. SQL Server 数据库安全
  7. The income statement
  8. android NDK 实用学习(一)-获取java端类及其类变量
  9. 初学Node.js第一天
  10. POJ3349: Snowflake Snow Snowflakes(hash 表)
  11. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW _TASK flag.
  12. 协程的作用 Python
  13. 高效率dc升壓轉換器 應用技巧談 功率設計
  14. Android学习笔记--Broadcast, BroadcastReceiver(广播)
  15. SQL_修改表结构
  16. Byte Array to Hexadecimal String
  17. 【css】圆角 +文本阴影
  18. python之路(10)类的内置函数
  19. PCL点云分割(3)
  20. centos linux查看硬盘型号

热门文章

  1. POJ2409 Let it Bead(Polya定理)
  2. dicom和dicomdir
  3. tcp客户端socket
  4. 聊聊我这两年都在忙什么,IT技术男如何转型!
  5. 区分js中的null,undefined,&quot;&quot;,0和false
  6. auto用法
  7. [Bzoj4408]神秘数(主席树)
  8. NumPy库入门
  9. python基础之列表、元组和字典
  10. Oozie 安装及 examples app 的使用