POJ 3159 Candies or difference constraint (stack SPFA)

Source: Internet
Author: User

Http://poj.org/problem? Id = 3159

Question:

N Children divide candy. You must satisfy their requirements (a B x means B cannot exceed a x Candy) and the gap between Candy numbers 1 and n is the biggest.

Ideas:

Well, let me reveal it first. The first day is a candy-sharing child, the squad leader! (Bulletin Board private hatred..., bully the children on the N number ~ Okay, I'm kidding)

Well, this question requires the shortest path. Why is it the shortest? You used to play longest ~

Because this question requires the greatest. The triangle inequalities in the figure are: d [v]-d [u] <= w (u, v); d [v] <= d [u] + w (u, v); that is, the relaxation condition is: if (d [v]> d [u] + w (u, v )) d [v] <= d [u] + w (u, v); through constant relaxation, the value of d keeps decreasing until all conditions are met, that is to say, when conditions are met, it is the greatest ~

Create a diagram, B-a <= x and then spfa, but the question is actually stuck in the queue .. Look at the discuss people use stack, and then I changed it. That's it ......

In addition, super source points cannot be created for this question.

Set the candy assigned by the first child to s [I], then s [I]> = 0

The above is B-a <= x. Because this is the shortest path, we need to change it to a smaller number, that is,-s [I] <= 0, then, if you add a vertex, for example, 0, it should be from I to 0, and it will be useless ....

#include
 
  #include
  
   #include
   
    using namespace std;const int MAXN=30000+10;const int MAXM=350000;const int INF=-999999;int n,m,head[MAXN],len,dis[MAXN];bool vis[MAXN];struct edge{int to,val,next;}e[MAXM];void add(int from,int to,int val){e[len].to=to;e[len].val=val;e[len].next=head[from];head[from]=len++;}void spfa(){int s=1;stack
    
      q;q.push(s);vis[s]=true;dis[s]=0;while(!q.empty()){int cur=q.top();q.pop();vis[cur]=false;for(int i=head[cur];i!=-1;i=e[i].next){int id=e[i].to;if(dis[id] > dis[cur] + e[i].val){dis[id]=dis[cur]+e[i].val;if(!vis[id]){q.push(id);vis[id]=true;}}}}}int main(){while(~scanf("%d%d",&n,&m)){for(int i=1;i<=n;i++){head[i]=-1;dis[i]=-INF;vis[i]=false;}len=0;for(int i=0;i
     
      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.