Hdu 5361 In Touch (dijkatrs+优先队列)
题目链接:
题目描述:
有n个传送机排成一排,编号从1到n,每个传送机都可以把自己位置的东西传送到距离自己[l, r]距离的位置,并且花费c,问从1号传送机到其他传送机的最小花费是多少??
解题思路:
看了题解发现是dijkstra(求最短单源路径)+并查集(优化),刚开始的时候并不知道怎么用并查集优化,然后就提交了一个没有并查集的代码,果断TLE。然后开始搜代码,突然惊叹真是优化的太美妙。因为每次从队列中取出的都是最优的,所以更新dis的时候可以直接把这些点合并,下次再遇到这些点的时候没必要更新就直接跳过。(在这里要特别谢谢为我debug快要疯了的天I火聚聚)
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL INF = 1LL<<;
const int maxn = ;
struct node
{
int x;
LL dis;
node (int a, LL b):x(a),dis(b){}
friend bool operator < (node a, node b)
{
return a.dis > b.dis;
}
};
int l[maxn], r[maxn], c[maxn], father[maxn], n;
LL dis[maxn];
int Find (int x)
{
if (x != father[x])
father[x] = Find (father[x]);
return father[x];
}
void Merge (int x, int y)
{
int px = Find(x);
int py = Find(y);
if (px == py)
return ;
father[px] = py;
}
void dijkstra (int x)
{
priority_queue <node> Q;
dis[x] = c[x];
Q.push(node(x, dis[x]));
while (!Q.empty())
{
int p = Q.top().x;
Q.pop();
for (int i=-; i<=; i+=)
{
int L = p + l[p]*i;
int R = p + r[p]*i;
if (L > R)
swap(L, R);
L = max (L, );
R = min (R, n);
for (int j=L; j<=R; j++)
{
j = Find (j);
if ( j > R)
break;
if (dis[j] > dis[p] + c[j])
{
dis[j] = dis[p] + c[j];
Q.push(node(j, dis[j]));
}
Merge (j, j+);
}
}
}
}
int main ()
{
int t;
scanf ("%d", &t);
while (t --)
{
scanf ("%d", &n);
for (int i=; i<=n; i++)
scanf ("%d", &l[i]);
for (int i=; i<=n; i++)
scanf ("%d", &r[i]);
for (int i=; i<=n; i++)
scanf ("%d", &c[i]);
for (int i=; i<=n+; i++)
{
father[i] = i;
dis[i] = INF;
}
dijkstra ();
for (int i=; i<=n; i++)
{
if (dis[i] == INF)
dis[i] = c[i] - ;
printf ("%lld%c", dis[i]-c[i], i==n?'\n':' ');
}
}
return ;
}
Hdu 5361 In Touch (dijkatrs+优先队列)的更多相关文章
- HDU 5361 In Touch (2015 多校6 1009 最短路 + 区间更新)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:最短路.求源点到全部点的最短距离.但与普通最短路不同的是,给出的边是某点到区间[l,r]内随意 ...
- 2015 Multi-University Training Contest 6 hdu 5361 In Touch
In Touch Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu 5437 Alisha’s Party 优先队列
Alisha’s Party Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_sh ...
- HDU 5884 Sort(二分+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=5884 题意:有个屌丝设计了一个程序,每次可以将k个数组进行合并,代价为这k个数组总的长度之和.现在另外一个屌丝要 ...
- 拓扑排序 - hdu 1285(普通和优先队列优化)
2017-09-12 19:50:58 writer:pprp 最近刚开始接触拓扑排序,拓扑排序适用于:无圈图的顶点的一种排序, 用来解决有优先级别的排序问题,比如课程先修后修,排名等. 主要实现:用 ...
- 2015多校第6场 HDU 5361 并查集,最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...
- 2015多校第6场 HDU 5360 Hiking 贪心,优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意:给定n个人,现在要邀请这些人去远足,但每个人同意邀请的条件是当前已经同意去远足的人数c必须 ...
- HDU 5638 Toposort 拓扑排序 优先队列
Toposort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 Description There is a directed acycli ...
随机推荐
- Office EXCEL 2010如何取消宏密码保护
打开宏编辑器之后,右击VBA项目,然后属性,保护中去掉密码即可
- 搭建Maven私服(使用Nexus)
搭建私服能够做什么? 1.假设公司开发组的开发环境所有内网.这时怎样连接到在互联网上的Maven中央仓库呢? 2.假设公司常常开发一些公共的组件.怎样共享给各个开发组.使用拷贝方式吗?假设这样,公共库 ...
- Make 编译脚本上手
考察下面的示例代码: main.c #include <stdio.h> int main(){ printf("hello world!"); return 0; } ...
- A + B Problem II(杭电1002)
/*A + B Problem II Problem Description I have a very simple problem for you. Given two integers A an ...
- MD5加密解密帮助类
using System; using System.Security.Cryptography; using System.Text; namespace Maticsoft.DBUtility { ...
- Win32 Windows编程 七
定时器消息 1. WM_TIMER 依照定时器设置的时间段,自己主动向窗体发送一个定时器消息WM_TIMER.优先级比較低 定时器精度比較低.毫秒级别.消息产生时间也精度比較低 2 .消息和函数 WM ...
- [IT练习册]Python练习项目 思路
1.爬虫:爬取如下网站一年的内容. http://www.calvarymoravian.org/dailytext 2.蛇形棋: 开发一个类似蛇形棋的游戏.最好基于Web. 3.爬虫+通讯录: 从公 ...
- Java小白手记2:一些名词解释
看到<Java 征途:行者的地图> ,这是一篇有关java学习路径文章.对我等Java小白有指引作用.里面提到了一些基本的名词术语,有些我知道,有些不知道,再补上一些自己曾觉得模糊的,记录 ...
- 2016/3/27 PHP中include和require的区别详解
1.概要 require()语句的性能与include()相类似,都是包括并运行指定文件.不同之处在于:对include()语句来说,在执行文件时每次都要进行读取和评估:而对于require()来说, ...
- 60年代进程 80年代线程 IPC How the Java Virtual Machine (JVM) Works
小结: 1. To facilitate communication between processes, most operating systems support Inter Process C ...