hdu5361 In Touch 神奇的dij,神奇的写法
来源:互联网 发布:mac删除windows后内存 编辑:程序博客网 时间:2024/06/06 06:13
In Touch
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 784 Accepted Submission(s): 217
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 .
The1 -st soda is their leader and he wants to know the minimum cost needed to reach i -th soda (1≤i≤n) .
The
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 integern (1≤n≤2×105) , the number of soda.
The second line containsn 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
The second line contains
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
152 0 0 0 13 1 1 0 51 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
2015 Multi-University Training Contest 6
dij算法求最短路的性质,与spfa不同已更新过的点的最短距离已经确定了。这道题用dij正合适,sq里是已更新过的点,se里是没更新过的点,为了使点不太多,每次更新se里的点时,把此点从se中删除,空间复杂度仅O(n).
#include<iostream>#include<cstdio>#include<set>#define inf 1000000000000000#define ll long longusing namespace std;const int maxn=200005;int n;int l[maxn];int r[maxn];ll c[maxn];ll dist[maxn];set<int>se;struct Node{ int id; ll w; Node(int a,ll b):id(a),w(b){} bool operator<(const Node& u)const { if(w+c[id]==u.w+c[u.id]) { return id<u.id; } return w+c[id]<u.w+c[u.id]; }};set<Node>sq;int main(){ int t; scanf("%d",&t); while(t--) { se.clear(); sq.clear(); 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("%I64d",&c[i]); } for(int i=1;i<=n;i++) dist[i]=inf; dist[1]=0; sq.insert(Node(1,0)); for(int i=2;i<=n;i++) { se.insert(i); } set<int>::iterator it; set<int>::iterator it2; while(!sq.empty()) { Node u=*sq.begin(); sq.erase(sq.begin()); it=se.lower_bound(u.id-r[u.id]); while(it!=se.end() && *it<=u.id-l[u.id]) { if(dist[*it]>u.w+c[u.id]) { dist[*it]=u.w+c[u.id]; sq.insert(Node(*it,dist[*it])); it2=it++; //删除的巧妙写法 se.erase(it2); } } it=se.lower_bound(u.id+l[u.id]); while(it!=se.end() && *it<=u.id+r[u.id]) { if(dist[*it]>u.w+c[u.id]) { dist[*it]=u.w+c[u.id]; sq.insert(Node(*it,dist[*it])); it2=it++; se.erase(it2); } } } printf("0"); for(int i=2;i<=n;i++) { if(dist[i]==inf) { printf(" -1"); } else { printf(" %I64d",dist[i]); } } puts(""); } return 0;}
0 0
- hdu5361 In Touch 神奇的dij,神奇的写法
- HDU5361 In Touch (dij)
- HDU5361 In Touch
- c++神奇的写法(一)
- hdu5361(2015多校6)--In Touch(变形的dijkstra)
- HDU5361 In Touch(线段树 + 最短路)
- 神奇的的调色板
- 神奇的*号
- 一组神奇的图片
- 神奇的一天
- 神奇的预言
- 神奇的jsfl!
- 神奇的VIM
- 神奇的猴子Mono
- 神奇的xx宏
- 神奇的视觉艺术
- 神奇的上班路上
- 神奇的视觉艺术
- 2015年北京大学软件工程学科优秀大学生夏令营上机考试---B:整数奇偶排序
- C# Web开发学习----记录学习过程与心得体会
- hdu 4813 Hard Code
- 代码优化
- 递归的学习(全排列)
- hdu5361 In Touch 神奇的dij,神奇的写法
- select、poll、epoll之间的区别
- HDU2150-Pipe-折线相交(叉乘)
- 对遗传算法理解
- 使用 Virtual Box (mac 版)安装 xp 虚拟机剪切板不能共享,屏幕不能自动调整的解决办法
- 九度oj 1112
- 聊聊我的高效编程之路
- UBI error: ubi_io_write: error -5 while writing 512 bytes to PEB 60:512, wri
- 1039. 到底买不买(20)