地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6071

题目:

Lazy Running

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

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 than K 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 than K.

 
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
 
思路:
  一道做过类似题的题目,在比赛时想了半个小时都没想出来,然后就做别的题去了。
  做过的题还不会做,好气啊
  记w为2和相邻节点路径的最小值,则如果存在一条从2出发,从2结束的路径(长度为x),那么x+w*2也必然是合法的。(这里的w取相邻节点路径的最小值是为了优化时间,不取最小值也可以)
  所以d[i][x]记为到达i节点且路径长度模2*w为x的最短路径长度。求出d[2][x]数组后就可以通过枚举x得到答案。
  那为什么这样做是对的呢?
  因为到达2的所有模2*w的剩余系的最短路径长度都被考虑了,所以答案必然是正确的。
 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int n,d12,d23,d34,d41;;
LL d[][],dk,p;
vector<PII>mp[];
void spfa(void)
{
queue<pair<LL,LL>>q;
for(int i=;i<=;i++)
for(int j=;j<p;j++)
d[i][j]=2e18;
q.push(MP(2LL,0LL));
while(q.size())
{
LL u=q.front().first,w=q.front().second;
q.pop();
if(w>dk*)continue;
for(auto v:mp[u])
if(d[v.first][(w+v.second)%p]>w+v.second)
{
d[v.first][(w+v.second)%p]=w+v.second;
q.push(MP(v.first,w+v.second));
}
}
}
int main(void)
{
int t;cin>>t;
while(t--)
{
memset(mp,,sizeof mp);
scanf("%lld%d%d%d%d",&dk,&d12,&d23,&d34,&d41);
mp[].PB(MP(,d12)),mp[].PB(MP(,d41));
mp[].PB(MP(,d12)),mp[].PB(MP(,d23));
mp[].PB(MP(,d23)),mp[].PB(MP(,d34));
mp[].PB(MP(,d34)),mp[].PB(MP(,d41));
p=2LL*min(d12,d23);
spfa();
LL ans=2e18;
for(int i=;i<p;i++)
if(d[][i]>=dk)
ans=min(d[][i],ans);
else
ans=min((dk-d[][i]+p-)/p*p+d[][i],ans);
printf("%lld\n",ans);
}
return ;
}

2017 Multi-University Training Contest - Team 4 hdu6071 Lazy Running的更多相关文章

  1. 2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. 2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】

    Dying Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  3. 2017 Multi-University Training Contest - Team 9 1003&&HDU 6163 CSGO【计算几何】

    CSGO Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  4. 2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  5. 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  8. 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 2017 Multi-University Training Contest - Team 1 1011&&HDU 6043 KazaQ's Socks【规律题,数学,水】

    KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. 如何在VS2010中更好的编写JavaScript代码

    VS2010默认的JavaScript代码编辑器相对简单.对于大家熟悉的代码折叠,代码结构.函数导航,代码高亮等都不支持,使用很不便.下面介绍下我发现的几个VS2010插件,具有哪些功能,如何安装和使 ...

  2. EF简单查询

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. 如何用C语言读写文件

    #include "stdio.h"#include <stdlib.h> main(){ FILE *fp1;//定义文件流指针,用于打开读取的文件 FILE *fp ...

  4. std::lock_guard

    /* std::lock_guard:更方便线程对于互斥量的上锁操作 std::lock_guard:在定义一个lock_guard时上锁,保证在析构或者异常时解锁 */ #include <i ...

  5. 基于Consul+Upsync+Nginx实现动态负载均衡

    基于Consul+Upsync+Nginx实现动态负载均衡 1.Consul环境搭建 下载consul_0.7.5_linux_amd64.zip到/usr/local/src目录 cd /usr/l ...

  6. 170310、Jenkins部署Maven多环境项目(dev、beta、prod)的参数设置

    使用Jenkins配置Git+Maven的自动化构建: http://blog.csdn.net/xlgen157387/article/details/50353317 在一个多开发和生产环境的项目 ...

  7. 160314、MVC设计模式

    MVC的由来 精彩内容 MVC模式最早由Trygve Reenskaug在1978年提出 ,是施乐帕罗奥多研究中心(Xerox PARC)在20世纪80年代为程序语言Smalltalk发明的一种软件设 ...

  8. 解决;R语言使用sqldf库是报错"Failed to connect to database: Error: Access denied for user '..'@'localhost' (using password: NO) Error in !dbPreExists : invalid argument type"

    原因:在使用sqldf时,不需要加载RMySQL库 解决方案:在控制台执行释放RMySQL库加载 detach("package:RMySQL", unload=T);

  9. Servlet------>jsp自定义标签5(标签体内容改为大写)

    5.把标签体内容改为大写(tld中的配置我就省略了,详细请看jsp自定义标签1) import java.io.IOException; import javax.servlet.jsp.JspExc ...

  10. CSDN BI Flume

    https://so.csdn.net/so/search/s.do?q=bytebuf&t=%20&u=