Lazy Running

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 101    Accepted Submission(s): 40

Problem Description

In HDU, you have to run along the campus for 24 times, or you will fail in PE. According to the rule, you must keep your speed, and your running distance should not be less thanK meters.
There are 4 checkpoints in the campus, indexed as p1,p2,p3 and p4. Every time you pass a checkpoint, you should swipe your card, then the distance between this checkpoint and the last checkpoint you passed will be added to your total distance.
The system regards these 4 checkpoints as a circle. When you are at checkpoint pi, you can just run to pi−1 or pi+1(p1 is also next to p4). You can run more distance between two adjacent checkpoints, but only the distance saved at the system will be counted.

Checkpoint p2 is the nearest to the dormitory, Little Q always starts and ends running at this checkpoint. Please write a program to help Little Q find the shortest path whose total distance is not less thanK.

Input

The first line of the input contains an integer T(1≤T≤15), denoting the number of test cases.
In each test case, there are 5 integers K,d1,2,d2,3,d3,4,d4,1(1≤K≤1018,1≤d≤30000), denoting the required distance and the distance between every two adjacent checkpoints.

Output

For each test case, print a single line containing an integer, denoting the minimum distance.

Sample Input

1
2000 600 650 535 380

Sample Output

2165

Hint

The best path is 2-1-4-3-2.

Source

2017 Multi-University Training Contest - Team 4

比赛的时候就是蠢……

当时只剩下十几分钟的时候看到这题的……其实很简单的一道最短路,前提是想到了……

题目要求是只有四个点,然后连边成正方形,问从2号点出发,再回到2号点且走过的总距离大于K的最短的路径是多少。对于这个,我们考虑如果存在一条合法路径,那么我再走2*w也一定是合法路径,其中w表示与2相连的某条边的长度。即回到2之后再出去再回来,这样的路径一定合法。那么,如果走了某条路径回到了2,然后总距离不够的话,我们只需要加上几个2*w使得最后结果大于等于K即可。

那么如何使这个结果最小呢?我们考虑设置一个数组d[x][p]表示从2出发最后到达x点且费用对2*w取模结果为p时的最小花费。这样子原本的一个点就可以拆成2*w个点,这样跑一遍dijkstra即可求出d数组。之后,对于每一个对2*w取模后的数值,我们都可以计算把它补到大于K且距离K最近的花费,再在这些花费中取一个最小的即可。那么,这样子考虑为什么可以包含全部的而且最优的解呢?因为我们最后补的是2*w或者它的倍数,然后我把对2*w取模后的所有取值的最小花费都计算了一次,这样子得出来的最小花费一定包含所有的情况,即2*w的所有剩余系都被包括了,所以可以保证正确性。具体代码如下:

转载于http://blog.csdn.net/yasola/article/details/76684704;

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <map>
#include <set>
#include <list>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a)) const int MAXN=+;
const int MAXM=+; struct Node
{
int p;
LL dis;
Node(int p, LL d):p(p), dis(d){}
}; LL K, G[MAXN][MAXN], m, ans;
bool vis[MAXN][MAXM];
LL dist[MAXN][MAXM];//从1开始到达i,模m等于j的最短路 void spfa(int s)
{
queue<Node> que;
mem(vis, );
mem(dist, 0x3f);
que.push(Node(, ));
dist[][]=;
vis[][]=true;
while(!que.empty())
{
int u=que.front().p;
LL now_dis=que.front().dis;
vis[u][now_dis%m]=false;
que.pop();
for(int i=-;i<;i+=)
{
int v=(u+i+)%;
LL next_dis=now_dis+G[u][v], next_m=next_dis%m;
if(v==s)//形成环,更行答案
{
if(next_dis<K)
ans=min(ans, next_dis+((K-next_dis-)/m+)*m);
else ans=min(ans, next_dis);
}
if(dist[v][next_m]>next_dis)
{
dist[v][next_m]=next_dis;
if(!vis[v][next_m])
{
que.push(Node(v, next_dis));
vis[v][next_m]=true;
}
}
}
}
} int main()
{
int T_T;
scanf("%d", &T_T);
while(T_T--)
{
scanf("%lld", &K);
for(int i=;i<;++i)
{
scanf("%lld", &G[i][(i+)%]);
G[(i+)%][i]=G[i][(i+)%];
}
m=*min(G[][], G[][]);//最小环
ans=((K-)/m+)*m;//只使用最短的回路
spfa();
printf("%lld\n", ans);
} return ;
}

HDU 6071 Lazy Running (同余最短路)的更多相关文章

  1. HDU 6071 - Lazy Running | 2017 Multi-University Training Contest 4

    /* HDU 6071 - Lazy Running [ 建模,最短路 ] | 2017 Multi-University Training Contest 4 题意: 四个点的环,给定相邻两点距离, ...

  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) P ...

  4. HDU 6071 Lazy Running (最短路)

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

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

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

  6. 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 之间有路相连, 现在 ...

  7. HDU 6071 同余最短路 spfa

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

  8. 多校4 lazy running (最短路)

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

  9. 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 ...

随机推荐

  1. 2015/9/20 Python基础(16):类和实例

    面向对象编程编程的发展已经从简单控制流中按步的指令序列进入到更有组织的方式中,依靠代码块可以形成命名子程序和完成既定的功能.结构化的或过程性编程可以让我们把程序组织成逻辑快,以便重复或重用.创造程序的 ...

  2. Enterprise Architect13 : 去掉UML图页面右侧那一道竖线

    我们在使用Enterprise Architect 中,画用例图,时序图时,页面右侧有一条竖线,见下图: 如果页面元素太多,会超出竖线的范围,显得很不协调. 如果像去掉竖线,只需选择主菜单的Layou ...

  3. mybatis在Mapper的xml文件中的转义字符的处理

    XML转义字符 < < 小于号 > > 大于号 & & 和 &apos; ’ 单引号 " " 双引号 用转义字符进行替换 例如 SE ...

  4. 【usaco-Earthquake, 2001 Open】 0-1分数规划 & 最优比率生成树

    题意:给定n个点m条边,一开始这些边全都是断的,要修一些边使得n个点全部联通.修完一共可以得到F元,修一条边有成本di和时间ti,要使得 得到的钱数 / 总时间 这个比值最大. 参考资料: 红线内的内 ...

  5. 【BZOJ】1385 [Baltic2000]Division expression

    [算法]欧几里德算法 [题解]紫书原题 #include<cstdio> #include<algorithm> using namespace std; ; int T,t, ...

  6. JS之document例题讲解1(两张表之间数据转移、日期时间选择、子菜单下拉、用div做下拉菜单、事件总结)

    作业一:两个列表之间数据从一个列表移动到另一个列表 <div style="width:600px; height:500px; margin-top:20px"> & ...

  7. Intel call指令

    转载:http://blog.ftofficer.com/2010/04/n-forms-of-call-instructions/ 最近有一个需求,给你个地址,看看这个地址前面是不是一个CALL指令 ...

  8. C语言调用Cmd命令以及执行系统软件

    C语言调用Cmd命令以及执行系统软件 http://blog.csdn.net/qq_16814591/article/details/43676377

  9. redis线程安全性

    总体来说快速的原因如下: 1)绝大部分请求是纯粹的内存操作(非常快速) 2)采用单线程,避免了不必要的上下文切换和竞争条件 3)非阻塞IO 内部实现采用epoll,采用了epoll+自己实现的简单的事 ...

  10. 如何使用vux创建vue项目

    1.安装vue-cli,通过vue-v可以查明 安装vue-cli步骤 vue init airyland/vux2 projectPath(项目名字) 2.安装依赖模块 方法1:npm instal ...