In Touch
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 578 Accepted Submission(s): 160
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).
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)
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.
解题:利用set可以二分,进行dijkstra,思路是学习这位大神的,确实很赞,很厉害。。。。很奇妙
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 200010; 5 struct node{ 6 int id; 7 LL cost; 8 node(int x = 0, LL y = 0){ 9 id = x; 10 cost = y; 11 } 12 bool operator<(const node &t)const{ 13 if(cost == t.cost) return id < t.id; 14 return cost < t.cost; 15 } 16 }; 17 set<int>p; 18 set<node>q; 19 int L[maxn],R[maxn],n; 20 LL w[maxn],d[maxn]; 21 int main(){ 22 int kase; 23 scanf("%d",&kase); 24 while(kase--){ 25 scanf("%d",&n); 26 memset(d,-1,sizeof d); 27 p.clear(); 28 q.clear(); 29 d[0] = 0; 30 for(int i = 0; i < n; ++i){ 31 scanf("%d",L + i); 32 if(i) p.insert(i); 33 } 34 for(int i = 0; i < n; ++i) 35 scanf("%d",R + i); 36 for(int i = 0; i < n; ++i) 37 scanf("%I64d",w + i); 38 q.insert(node(0,w[0])); 39 while(q.size()){ 40 node cur = *q.begin(); 41 q.erase(q.begin()); 42 auto it = p.lower_bound(cur.id - R[cur.id]); 43 while(it != p.end() && *it <= cur.id - L[cur.id]){ 44 d[*it] = cur.cost; 45 q.insert(node(*it,cur.cost + w[*it])); 46 p.erase(it++); 47 } 48 it = p.lower_bound(cur.id + L[cur.id]); 49 while(it != p.end() && *it <= cur.id + R[cur.id]){ 50 d[*it] = cur.cost; 51 q.insert(node(*it,cur.cost + w[*it])); 52 p.erase(it++); 53 } 54 } 55 for(int i = 0; i < n; ++i) 56 printf("%I64d%c",d[i],i + 1 == n?' ':' '); 57 } 58 return 0; 59 }