题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361

题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距离i点大于等于l[i]并且小于等于r[i]的点,问从节点1到每个节点的最短距离是多少?不能到达的话,输出-1。

题解参考:http://blog.csdn.net/jtjy568805874/article/details/47341905

解法:这题的难点主要在于边的条数过多,不能像普通的最短路那样子跑。不过此题的特点在于对于每个点来说,从这个点出去能到的任何点这个过程的花费是相同的,都是cost[i]。于是假设到达该点的距离为dis[i]则从该点能到的任何点j的值都是dis[j]=min(dis[j],dis[i]+cost[i]);于是只要按照dis[i]+cost[i]排序,当前最先的第一个点能到的点的距离一定都是最近的,这些点在这之后都不会再被更新了。这样只要维护一个并查集压缩路径,或者维护一个set存可能还会被更新的点就好了。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5+10;
struct FastIO
{
static const int S = 1310720;
int wpos;
char wbuf[S];
FastIO() : wpos(0) {}
inline int xchar()
{
static char buf[S];
static int len = 0, pos = 0;
if(pos == len)
pos = 0, len = fread(buf, 1, S, stdin);
if(pos == len)
exit(0);
return buf[pos ++];
}
inline unsigned long long xuint()
{
int c = xchar();
unsigned long long x = 0;
while(c <= 32)
c = xchar();
for(; '0' <= c && c <= '9'; c = xchar())
x = x * 10 + c - '0';
return x;
}
inline long long xint()
{
long long s = 1;
int c = xchar(), x = 0;
while(c <= 32)
c = xchar();
if(c == '-')
s = -1, c = xchar();
for(; '0' <= c && c <= '9'; c = xchar())
x = x * 10 + c - '0';
return x * s;
}
inline void xstring(char *s)
{
int c = xchar();
while(c <= 32)
c = xchar();
for(; c > 32; c = xchar())
* s++ = c;
*s = 0;
}
inline double xdouble()
{
bool sign = 0;
char ch = xchar();
double x = 0;
while(ch <= 32)
ch = xchar();
if(ch == '-')
sign = 1, ch = xchar();
for(; '0' <= ch && ch <= '9'; ch = xchar())
x = x * 10 + ch - '0';
if(ch == '.')
{
double tmp = 1;
ch = xchar();
for(; ch >= '0' && ch <= '9'; ch = xchar())
tmp /= 10.0, x += tmp * (ch - '0');
}
if(sign)
x = -x;
return x;
}
inline void wchar(int x)
{
if(wpos == S)
fwrite(wbuf, 1, S, stdout), wpos = 0;
wbuf[wpos ++] = x;
}
inline void wint(long long x)
{
if(x < 0)
wchar('-'), x = -x;
char s[24];
int n = 0;
while(x || !n)
s[n ++] = '0' + x % 10, x /= 10;
while(n--)
wchar(s[n]);
}
inline void wstring(const char *s)
{
while(*s)
wchar(*s++);
}
inline void wdouble(double x, int y = 6)
{
static long long mul[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL, 1000000000000000LL, 10000000000000000LL, 100000000000000000LL};
if(x < -1e-12)
wchar('-'), x = -x;
x *= mul[y];
long long x1 = (long long) floorl(x);
if(x - floor(x) >= 0.5)
++x1;
long long x2 = x1 / mul[y], x3 = x1 - x2 * mul[y];
wint(x2);
if(y > 0)
{
wchar('.');
for(size_t i = 1; i < y && x3 * mul[i] < mul[y]; wchar('0'), ++i);
wint(x3);
}
}
~FastIO()
{
if(wpos)
fwrite(wbuf, 1, wpos, stdout), wpos = 0;
}
} io;
LL n, T, d[maxn], l[maxn], r[maxn], c[maxn], fa[maxn], ans, sum;
struct cmp{
bool operator ()(const LL &x, const LL &y) const{
return d[x]+c[x]>d[y]+c[y];
}
}; namespace DSU{
int fa[maxn];
void init(){
for(int i=1; i<=n+1; i++) fa[i]=i;
}
int find_set(int x){
if(x == fa[x]) return x;
else return fa[x] = find_set(fa[x]);
}
void union_set(int x, int y){
int fx = find_set(x), fy = find_set(y);
if(fx != fy){
fa[fx] = fy;
}
}
} using namespace DSU; void Dijstra(int x)
{
priority_queue<LL,vector<LL>,cmp>q;
d[x] = 0;
q.push(x);
while(q.size())
{
LL L,R,u = q.top(); q.pop();
L = u-r[u], R = u-l[u];
if(R>0){
for(LL j=max(L,1LL); ; j++){
j = find_set(j);
if(j>min(R,n)) break;
if(d[j] > d[u]+c[u]){
d[j] = d[u]+c[u];
q.push(j);
}
union_set(j,j+1);
}
}
L = u+l[u], R = u+r[u];
if(L<=n){
for(LL j=L;;j++){
j = find_set(j);
if(j>min(R,n)) break;
if(d[j]>d[u]+c[u]){
d[j] = d[u]+c[u];
q.push(j);
}
union_set(j,j+1);
}
}
}
return;
} int main()
{
int T;
T = io.xint();
while(T--)
{
n = io.xint();
sum = 0;
for(int i=1; i<=n; i++) l[i] = io.xint();
for(int i=1; i<=n; i++) r[i] = io.xint();
for(int i=1; i<=n; i++) c[i] = io.xint(), sum+=c[i];
init();
for(int i=1; i<=n; i++) d[i] = sum+1;
Dijstra(1);
for(int i=1; i<=n; i++){
if(d[i] == sum+1) d[i] = -1;
if(i<n) printf("%lld ", d[i]);
else printf("%lld\n", d[i]);
}
}
return 0;
}

2015多校第6场 HDU 5361 并查集,最短路的更多相关文章

  1. 2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354 题意:求删去每个点后图是否存在奇环(n,m<=1e5) 解法:很经典的套路,和这题一样:h ...

  2. 2015多校第6场 HDU 5355 Cake 贪心,暴力DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给你n个尺寸大小分别为1,2,3,…,n的蛋糕,要求你分成m份,要求每份中所有蛋糕的大小之 ...

  3. 2015多校第6场 HDU 5358 First One 枚举,双指针

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5358 题意:如题. 解法:观察式子发现,由于log函数的存在,使得这个函数的值域<=34,然后我 ...

  4. 2015多校第6场 HDU 5360 Hiking 贪心,优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意:给定n个人,现在要邀请这些人去远足,但每个人同意邀请的条件是当前已经同意去远足的人数c必须 ...

  5. 2015多校第6场 HDU 5353 Average 贪心,细节处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5353 题意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块.现在有三种操作:第i个人给 ...

  6. 2015多校第9场 HDU 5405 Sometimes Naive 树链剖分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5405 题意: 给你一棵n个节点的树,有点权.        要求支持两种操作: 操作1:更改某个节点的 ...

  7. 2015多校第8场 HDU 5382 GCD?LCM! 数论公式推导

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5382 题意:函数lcm(a,b):求两整数a,b的最小公倍数:函数gcd(a,b):求两整数a,b的最 ...

  8. 2015多校第7场 HDU 5378 Leader in Tree Land 概率DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:一棵n个节点的树.对其节点进行标号(1~n).求恰好存在k个节点的标号是其节点所在子树的最 ...

  9. 2015多校第7场 HDU 5379 Mahjong tree 构造,DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题意:一颗n个节点n-1条边的树,现在要给每个节点标号(1~n),要求:(1)每一层的兄弟节点的 ...

随机推荐

  1. BZOJ4310 跳蚤(后缀数组+二分答案)

    注意到答案一定是原串的子串,于是考虑造出SA,二分答案是第几小的子串.第k小子串很容易在SA上求出.之后计算使他成为最大子串至少要在几个位置切割,对每个字典序比答案大的后缀,找到所有合法切割位置(求l ...

  2. [洛谷P2057][SHOI2007]善意的投票

    题目大意:有$n(n\leqslant300)$个人,每个人可以选择$0$或$1$,每个人最开始有意愿,有$m(m\leqslant\dfrac{n(n-1)}2)$对好朋友.定义一次的冲突数为好朋友 ...

  3. P2805 [NOI2009]植物大战僵尸(最小割+拓扑排序)

    题意: n*m的矩阵,每个位置都有一个植物.每个植物都有一个价值(可以为负),以及一些它可以攻击的位置.从每行的最右面开始放置僵尸,僵尸从右往左行动,当僵尸在植物攻击范围内时会立刻死亡.僵尸每到一个位 ...

  4. 2018牛客多校第六场 G.Pikachu

    题意: 给出一棵n个点的树,每条边有边权.对这个树加边变成一个完全图.新加的边的权值为边上两点在树上的距离.求完全图上任意两点的最大流之和. 题解: 一共有C(n,2)个点对.假设当前求s到t之间的最 ...

  5. BZO4197 & 洛谷2150 & UOJ129:[NOI2015]寿司晚宴——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4197 https://www.luogu.org/problemnew/show/P2150 ht ...

  6. BZOJ1101 & 洛谷3455:[POI2007]ZAP——题解

    https://www.luogu.org/problemnew/show/3455#sub http://www.lydsy.com/JudgeOnline/problem.php?id=1101 ...

  7. JQuery学习六

    <JQuery cookie>插件 cookie是保存在浏览器上的内容,用户在这次浏览页面的时候向cookie中保存文本内容.下次再访问页面的时侯就可以取出来上次保存的内容.这样可以得到上 ...

  8. POJ 3107 树形dp

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6812   Accepted: 2390 Descrip ...

  9. <audio>标签HTML5音乐播放器

    <audio>标签:用于在文档中表示音频内容.利用它,你可以在你的个人网站上放一首你喜欢的歌.    <audio src="music.mp3">< ...

  10. 利用枚举算法实现todoList:把对应项添加的内容列表

    功能: 点击城市列表项,如果内容列表不存在,则插入点击项: 如果内容列表中已存在,则不插入,然后把内容列表中的对应项放到第一位. HTML代码: <!DOCTYPE html> <h ...