边数太多,单考虑到一个点到一片点的距离都是一样的.
如果取dist[v]+cost[v]最小的点更新出的最短路肯定是最小的,用一个set维护可以用来更新最短路的点(按dist[v]+cost[v]排序)用另一个set维护需要被更新的点,每当一个点被更新后就将这个点从需要被更新的set中去掉
每个点最多只会被更新一次
In Touch
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1085 Accepted Submission(s): 292
Problem Description
There are n soda living in a straight line. soda are numbered by
1,2,…,n
from left to right. The distance between two adjacent soda is 1 meter. Every soda has a teleporter. The teleporter of
i
-th soda can teleport to the soda whose distance between
i
-th soda is no less than
li
and no larger than
ri
. The cost to use
i
-th soda's teleporter is
ci
.
The 1 -st soda is their leader and he wants to know the minimum cost needed to reach i -th soda (1≤i≤n) .
The 1 -st soda is their leader and he wants to know the minimum cost needed to reach i -th soda (1≤i≤n) .
Input
There are multiple test cases. The first line of input contains an integer
T
, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤2×105) , the number of soda.
The second line contains n integers l1,l2,…,ln . The third line contains n integers r1,r2,…,rn . The fourth line contains n integers c1,c2,…,cn . (0≤li≤ri≤n,1≤ci≤109)
The first line contains an integer n (1≤n≤2×105) , the number of soda.
The second line contains n integers l1,l2,…,ln . The third line contains n integers r1,r2,…,rn . The fourth line contains n integers c1,c2,…,cn . (0≤li≤ri≤n,1≤ci≤109)
Output
For each case, output
n
integers where
i
-th integer denotes the minimum cost needed to reach
i
-th soda. If
1
-st soda cannot reach
i
-the soda, you should just output -1.
Sample Input
1 5 2 0 0 0 1 3 1 1 0 5 1 1 1 1 1
Sample Output
0 2 1 1 -1HintIf you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
Source
/* ***********************************************
Author :CKboss
Created Time :2015年08月09日 星期日 09时17分43秒
File Name :HDOJ5361.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long int LL;
const int maxn=200200;
const LL INF=0x3f3f3f3f3f3f3f3f;
int n;
int l[maxn],r[maxn],c[maxn];
LL dist[maxn];
struct Node
{
int id;
LL dis;
Node(){}
Node(int _id,LL _dis):id(_id),dis(_dis){}
bool operator<(const Node& node) const
{
if(dis+c[id]!=node.dis+c[node.id])
return dis+c[id]<node.dis+c[node.id];
return id<node.id;
}
void toString() const
{
printf("Node %d dis: %lld\n",id,dis);
}
};
set<Node> sq;
set<int> se;
void dijkstra()
{
memset(dist,63,sizeof(dist));
dist[1]=0;
sq.clear(); se.clear();
sq.insert(Node(1,0));
for(int i=2;i<=n;i++)
se.insert(i);
set<int>::iterator it,it2;
while(sq.size())
{
Node u=*sq.begin();
sq.erase(sq.begin());
/// left side
it=se.lower_bound(u.id-r[u.id]);
while(it!=se.end())
{
if(*it>u.id-l[u.id]) break;
if(dist[*it]>dist[u.id]+c[u.id])
{
dist[*it]=dist[u.id]+c[u.id];
sq.insert(Node(*it,dist[*it]));
it2=it++;
se.erase(it2);
}
}
/// right side
it=se.lower_bound(u.id+l[u.id]);
while(it!=se.end())
{
if(*it>u.id+r[u.id]) break;
if(dist[*it]>dist[u.id]+c[u.id])
{
dist[*it]=dist[u.id]+c[u.id];
sq.insert(Node(*it,dist[*it]));
it2=it++;
se.erase(it2);
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",l+i);
for(int i=1;i<=n;i++) scanf("%d",r+i);
for(int i=1;i<=n;i++) scanf("%d",c+i);
dijkstra();
for(int i=1;i<=n;i++)
{
if(dist[i]==INF) dist[i]=-1;
//printf("%lld%c",dist[i],(i==n)?10:32);
printf("%I64d%c",dist[i],(i==n)?10:32);
}
}
return 0;
}