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

题意:

1、2、3、4四个点依次形成一个环,现在有个人从2结点出发,每次可以往它相邻的两个结点跑,求最后回到2结点并且不少于K的最短距离。

思路:

官方题解:

最后的答案可以表示为:$ans=p*(2w)+m$,这样一来,m的取值范围就是$(0<=m<2w)$,而因为m的不同,p值也会有所不同。所以我们用 d [ i ] [ m ]表示从起点出发,最后到达 i 点,距离对2w取模为m时的最小距离,这个计算一下最短路即可。

最后只需要枚举m,如果此时d [ 2 ] [ m ]不足K的话,那么就再加上2w补足即可,在所有的m中取最小值。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const ll INF = ;
const int maxn=1e6+; ll k;
ll m;
ll d[];
ll dis[][maxn];
vector<pll> G[]; struct HeapNode
{
int u; ll w;
HeapNode(int x, ll y) :u(x), w(y){}
bool operator <(const HeapNode& rhs) const{
return w>rhs.w;
}
}; void dijkstra()
{
priority_queue<HeapNode> Q;
for(int i=;i<;i++)
for(int j=;j<=m;j++) dis[i][j]=INF; Q.push(HeapNode(,));
while(!Q.empty())
{
HeapNode p=Q.top(); Q.pop();
int u=p.u;
ll w=p.w; for(int i=;i<G[u].size();i++)
{
int v=G[u][i].first;
ll new_w=G[u][i].second+w;
if(dis[v][new_w%m]>new_w)
{
dis[v][new_w%m]=new_w;
Q.push(HeapNode(v,new_w));
}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld",&k);
for(int i=;i<;i++) G[i].clear();
for(int i=;i<;i++)
{
scanf("%lld",&d[i]);
G[i].push_back(make_pair((i+)%,d[i]));
G[(i+)%].push_back(make_pair(i,d[i]));
}
m=*min(d[],d[]);
dijkstra(); ll ans=INF;
for(int i=;i<m;i++)
{
ll tmp=k-dis[][i];
if (tmp<=) ans=min(ans,dis[][i]);
else ans=min(ans,dis[][i]+(ll)ceil((long double)tmp/m)*m);
}
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 (同余最短路)

    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 (同余最短路 dij)

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

  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. 不推荐别的了,IDEA 自带的数据库工具就很牛逼!

    MySQL 等数据库客户端软件市面上非常多了,别的栈长就不介绍了, 其实 IntelliJ IDEA 自带的数据库工具就很牛逼,不信你继续往下看. 本文以 IntelliJ IDEA/ Mac 版本作 ...

  8. 【死磕 NIO】— Proactor模式是什么?很牛逼吗?

    大家好,我是大明哥. 上篇文章我们分析了高性能 IO模型Reactor模式,了解了什么是Reactor 模式以及它的三种常见的模式,这篇文章,大明再介绍另外一种高性能IO模型: Proactor. 为 ...

  9. 很牛逼的android真机调试,手机、平板、电视都可

    1.首先通过路由器,搭建局域网 2.手机开wifi,记住ip地址,平板开wifi,记住ip地址,电视开wifi,记住ip 3.然后再eclipse里面“窗口-首选项-android-ddms里面设置使 ...

随机推荐

  1. js-jquery-数组遍历

    一.原生方法支持 1.普通for循环 for(j = 0; j < arr.length; j++) { } 说明:性能很高,但是仍可以优化. 2.优化版for循环[推荐] for(j = 0, ...

  2. word 加载adobe acrobat ,保存word中的清晰图片

    1:先安装 adobe 并激活 https://blog.csdn.net/xintingandzhouyang/article/details/82558235 2:打开word,点击   文件&g ...

  3. ubuntu 安装ftp,配置,和java调用

    1:安装 ftp服务器 sudo apt-get install vsftpd 自动安装使用的是主机的用户名和密码:liyafei,1367xxx 访问方式,ftp://localhost:21,ft ...

  4. [vue]组件最佳实战

    [vue]全局组件和局部组件(嵌套+props引用父组件数据) [vue]组件篇 [vue]组件的创建(componet)和销毁(keep-alive缓存)和父子dom同步nextTick [vue] ...

  5. [py]flask蓝图的使用

    参考 flask挺挺轻巧的, 因此玩一玩它. 如果用它做大型点的项目, 就用到了它的蓝图组织项目. 一时半会不太清楚这玩意怎么用, 得撸一撸py基础了. 我搞了个movie小的flask栗子来用用蓝图 ...

  6. 20170809直接访问功能测试Postman

    20170809直接访问功能测试Postman 1 打开admin账户登录界面,打开F12,登录 admin账户,点击系统管理,用户设置,获得网址http://10.200.44.69:8080/cl ...

  7. Andrew Ng-ML习题答案1

    1.Linear Regression with Multiple Variables 转自:https://blog.csdn.net/mupengfei6688/article/details/5 ...

  8. 安装selenium python

    https://pypi.org/project/selenium/#files selenium-3.13.0-py2.py3-none-any.whl 安装成功后才能用 from selenium ...

  9. Summary: Lowest Common Ancestor in a Binary Tree & Shortest Path In a Binary Tree

    转自:Pavel's Blog Now let's say we want to find the LCA for nodes 4 and 9, we will need to traverse th ...

  10. 5Lambda表达式

    C++11中的Lambda表达式用于定义并创建匿名的函数对象,以简化编程工作.首先看一下Lambda表达式的基本构成: [函数对象参数](操作符重载函数参数)mutable或exception -&g ...