/*
HDU 6071 - Lazy Running [ 建模,最短路 ] | 2017 Multi-University Training Contest 4
题意:
四个点的环,给定相邻两点距离,问从2号点出发,2号点结束,距离大于k的最短距离
d(i,j) < 30000, k <= 1e18
分析:
设 r = min(d(1,2), d(2,3))
假设距离 k 可达,则 k+2*r 也可达(来回走的形式)
故求出所有 d[i][j] 满足 d[i][j]%2r == j 的最短距离
然后对于每一个d[2][j] 求出 d[2][j] + x*(2r) >= k 的第一个数
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL INF = (LL)(1e18)+5;
struct qnode {
int v; LL c;
qnode(int v = 0, LL c = 0) : v(v), c(c) {}
bool operator < (const qnode & r) const {
return c > r.c;
}
};
struct Edge {
int v; LL c;
Edge(int v = 0, LL c = 0) : v(v), c(c) {}
};
vector<Edge> edge[5];
bool vis[5][60005];
LL dist[5][60005];
priority_queue<qnode> que;
void Dijkstra(int r)
{
while (!que.empty()) que.pop();
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= 4; i++)
for (int j = 0; j <= r; j++) dist[i][j] = INF;
dist[2][0] = r;
que.push(qnode(2, 0));
while (!que.empty())
{
auto tmp = que.top(); que.pop();
int u = tmp.v;
LL w = tmp.c;
if (vis[u][w%r]) continue;
vis[u][w%r] = 1;
for (const auto& e : edge[u])
{
LL c = w + e.c;
if (!vis[e.v][c%r] && dist[e.v][c%r] > c)
{
dist[e.v][c%r] = c;
que.push(qnode(e.v, c));
}
}
}
}
void addedge(int u, int v, LL w)
{
edge[u].push_back(Edge(v, w));
edge[v].push_back(Edge(u, w));
}
LL k, d[5], r;
int main()
{
int t; scanf("%d", &t);
while (t--)
{
for (int i = 1; i <= 4; i++) edge[i].clear();
scanf("%lld", &k);
for (int i = 1; i <= 4; i++) scanf("%lld", &d[i]);
r = 2*min(d[1], d[2]);
addedge(1, 2, d[1]); addedge(1, 4, d[4]);
addedge(2, 3, d[2]); addedge(3, 4, d[3]);
Dijkstra(r);
LL ans = k+r;
for (int i = 0; i < r; i++)
{
if (dist[2][i] == INF) continue;
if (dist[2][i] >= k)
{
ans = min(ans, dist[2][i]);
}
else
{
LL s = k-dist[2][i];
LL l = ((s-1)/r+1) * r + dist[2][i];
ans = min(ans, l);
}
}
printf("%lld\n", ans);
}
}

  

HDU 6071 - Lazy Running | 2017 Multi-University Training Contest 4的更多相关文章

  1. hdu 6071 Lazy Running 最短路建模

    Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) P ...

  2. HDU 6071 Lazy Running (同余最短路 dij)

    Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)To ...

  3. HDU 6071 Lazy Running (同余最短路)

    Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)To ...

  4. HDU 6071 Lazy Running(很牛逼的最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=6071 题意: 1.2.3.4四个点依次形成一个环,现在有个人从2结点出发,每次可以往它相邻的两个结点跑,求最后回 ...

  5. HDU 6071 Lazy Running(最短路)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6071 [题目大意] 给出四个点1,2,3,4,1和2,2和3,3和4,4和1 之间有路相连, 现在 ...

  6. HDU 6071 Lazy Running (最短路)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6071 题解 又是一道虐信心的智商题... 首先有一个辅助问题,这道题转化了一波之后就会化成这个问题: ...

  7. HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9

    /* HDU 6170 - Two strings [ DP ] | 2017 ZJUT Multi-University Training 9 题意: 定义*可以匹配任意长度,.可以匹配任意字符,问 ...

  8. hdu 6301 Distinct Values (2018 Multi-University Training Contest 1 1004)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. hdu 5288 OO’s Sequence(2015 Multi-University Training Contest 1)

    OO's Sequence                                                          Time Limit: 4000/2000 MS (Jav ...

随机推荐

  1. HanLP-地名识别调试方法

    HanLP收词特别是实体比较多,因此特别容易造成误识别.下边举几个地名误识别的例子,需要指出的是,后边的机构名识别也以地名识别为基础,因此,如果地名识别不准确,也会导致机构名识别不准确. 类型1 数字 ...

  2. Java操作Excle(基于Poi)

    有一次有个同事问我会不会有java操作Excle,回答当然是不会了!感觉被嘲讽了,于是开始寻找度娘,找到个小例子,结果越写越有意思,最后就成就了这个工具类. import java.io.Buffer ...

  3. 这里除了安全,什么都不会发生!Docker镜像P2P加速之路

    1.1      问题: 在使用Docker运行容器化应用时,宿主机通常先要从Registry服务(如Docker Hub)下载相应的镜像(image).这种镜像机制在开发环境中使用还是很有效的,团队 ...

  4. asp.net core In Docker(Image)

    原文地址:https://www.cnblogs.com/stulzq/p/9059108.html 大家应该知道目前.NET Core(2.0)还是没有System.Drawing程序集,如果我们要 ...

  5. LeetCode:197.上升的温度

    题目链接:https://leetcode-cn.com/problems/rising-temperature/ 题目 给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日 ...

  6. JAVA栅栏和闭锁的区别

    闭锁:一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.即,一组线程等待某一事件发生,事件没有发生前,所有线程将阻塞等待:而事件发生后,所有线程将开始执行:闭锁最初 ...

  7. 转:Java Web 项目发布到Tomcat中三种部署方法

    首先整理项目文件,文件内包含jsp.js等和class编译后的文件及lib包,如: 第一种方法:在tomcat中的conf目录中,在server.xml中的,<host/>节点中添加:   ...

  8. JavaSpring【三、Bean】

    配置项 id bean的标识 class bean的类全名 scope bean的作用域 constructor-arg 构造注入 properties 设值注入 autowire 装配模式 lazy ...

  9. 1.开始认识flask

    1. pip安装flask包pip install flask2.对flask最基本的使用from flask import Flask # 导入flask包 app = Flask(__name__ ...

  10. Ubuntu安装Python 3.6之编译安装+使用PPA源安装

    下面分别详细介绍一下Ubuntu 14.04/16.04安装Python 3.6的两种方法: 方法一 自己编译安装: # 安装编译必需的软件包 sudo apt install build-essen ...