码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3016 线段树区间更新+spfa

时间:2015-07-17 17:48:07      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

Man Down

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1836    Accepted Submission(s): 665


Problem Description
The Game “Man Down 100 floors” is an famous and interesting game.You can enjoy the game from 
http://hi.baidu.com/abcdxyzk/blog/item/16398781b4f2a5d1bd3e1eed.html
技术分享

We take a simplified version of this game. We have only two kinds of planks. One kind of the planks contains food and the other one contains nails. And if the man falls on the plank which contains food his energy will increase but if he falls on the plank which contains nails his energy will decrease. The man can only fall down vertically .We assume that the energy he can increase is unlimited and no borders exist on the left and the right.

First the man has total energy 100 and stands on the topmost plank of all. Then he can choose to go left or right to fall down. If he falls down from the position (Xi,Yi),he will fall onto the nearest plank which satisfies (xl <= xi <= xr)(xl is the leftmost position of the plank and xr is the rightmost).If no planks satisfies that, the man will fall onto the floor and he finishes his mission. But if the man’s energy is below or equal to 0 , he will die and the game is Over.

Now give you the height and position of all planks. And ask you whether the man can falls onto the floor successfully. If he can, try to calculate the maximum energy he can own when he is on the floor.(Assuming that the floor is infinite and its height is 0,and all the planks are located at different height).
 

 

Input
There are multiple test cases.

For each test case, The first line contains one integer N (2 <= N <= 100,000) representing the number of planks.

Then following N lines representing N planks, each line contain 4 integers (h,xl,xr,value)(h > 0, 0 < xl < xr < 100,000, -1000 <= value <= 1000), h represents the plank’s height, xl is the leftmost position of the plank and xr is the rightmost position. Value represents the energy the man will increase by( if value > 0) or decrease by( if value < 0) when he falls onto this plank.
 

 

Output
If the man can falls onto the floor successfully just output the maximum energy he can own when he is on the floor. But if the man can not fall down onto the floor anyway ,just output “-1”(not including the quote)
 

 

Sample Input
4
10 5 10 10
5 3 6 -100
4 7 11 20
2 2 1000 10
 

 

Sample Output
140
 

 

Source
 
 
题目意思:
一个人初始能量为100,从最高的木板下落,每次下路一定在木板的边界。每个木板有一个价值,当人到达这个木板的时候可以获得这个价值(价值有正有负),若在中途能量变为0或负数则输出-1,否则输出到达地面后剩余最大的能量。
 
思路:
先按木板高度从小到大排序,依次插入线段树中,并获得覆盖之前线段树中木板标号,建图,spfa跑一遍。
 
代码:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <vector>
  6 #include <queue>
  7 #include <cmath>
  8 #include <set>
  9 using namespace std;
 10 
 11 #define N 100005
 12 #define ll root<<1
 13 #define rr root<<1|1
 14 #define mid (a[root].l+a[root].r)/2
 15 
 16 
 17 int n;
 18 vector<int>ve[N];
 19 
 20 struct Line{
 21     int h, l, r, val;
 22 }b[N];
 23 
 24 bool cmp(Line a,Line b){
 25     return a.h<b.h;
 26 }
 27 
 28 struct node{
 29     int l, r, val;
 30     bool lazy;
 31 }a[N*4];
 32 
 33 void build(int l,int r,int root){
 34     a[root].l=l;
 35     a[root].r=r;
 36     a[root].lazy=false;
 37     a[root].val=0;
 38     if(l==r) return;
 39     build(l,mid,ll);
 40     build(mid+1,r,rr);
 41 }
 42 
 43 void update(int l,int r,int val,int root){
 44     if(a[root].l==l&&a[root].r==r){
 45         a[root].lazy=true;
 46         a[root].val=val;
 47         return;
 48     }
 49     if(r<=a[ll].r) update(l,r,val,ll);
 50     else if(l>=a[rr].l) update(l,r,val,rr);
 51     else{
 52         update(l,mid,val,ll);
 53         update(mid+1,r,val,rr);
 54     }
 55 }
 56 
 57 int query(int p,int root){
 58     if(a[root].l==a[root].r&&a[root].l==p){
 59         return a[root].val;
 60     }
 61     if(a[root].lazy){
 62         a[ll].lazy=a[rr].lazy=true;
 63         a[root].lazy=false;
 64         a[ll].val=a[rr].val=a[root].val;
 65     }
 66     if(p<=a[ll].r) return query(p,ll);
 67     else return query(p,rr);
 68 }
 69 
 70 bool visited[N];
 71 int dis[N];
 72 
 73 void spfa(int root){
 74     queue<int>Q;
 75     Q.push(root);
 76     memset(visited,false,sizeof(visited));
 77     visited[root]=true;
 78     for(int i=0;i<=n;i++) dis[i]=-999999999;
 79     dis[root]=100+b[root].val;
 80     int i, u, v;
 81     while(!Q.empty()){
 82         u=Q.front();Q.pop();visited[u]=false;
 83         for(i=0;i<ve[u].size();i++){
 84             v=ve[u][i];
 85             if(v&&dis[v]<dis[u]+b[v].val&&dis[u]+b[v].val>0){
 86                 dis[v]=dis[u]+b[v].val;
 87                 if(!visited[v]){
 88                     Q.push(v);visited[v]=true;
 89                 }
 90             }
 91             else if(!v&&dis[v]<dis[u]){
 92                 dis[v]=dis[u];
 93             }
 94         }
 95     }
 96 }
 97 
 98 main()
 99 {
100     int i, j, k;
101     int maxh, minh;
102     while(scanf("%d",&n)==1){
103         maxh=-1;
104         minh=999999999;
105         for(i=1;i<=n;i++) {
106             scanf("%d %d %d %d",&b[i].h,&b[i].l,&b[i].r,&b[i].val);
107             maxh=max(maxh,b[i].r);
108             minh=min(minh,b[i].l);
109         }
110         build(minh,maxh,1);
111         sort(b+1,b+n+1,cmp);
112         for(i=0;i<=n;i++) ve[i].clear();
113         int l, r;
114         for(i=1;i<=n;i++){
115             l=query(b[i].l,1);
116             r=query(b[i].r,1);
117             if(l==r) ve[i].push_back(l);
118             else{
119                 ve[i].push_back(l);
120                 ve[i].push_back(r);
121             }
122             update(b[i].l,b[i].r,i,1);
123         }
124         spfa(n);//n为最高木板标号,0为地面 
125         if(dis[0]>0) printf("%d\n",dis[0]);
126         else printf("-1\n");
127     }
128 }

 

HDU 3016 线段树区间更新+spfa

标签:

原文地址:http://www.cnblogs.com/qq1012662902/p/4654668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!