poj3159 Candies (differential constraint)

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud

Candies
Time Limit: 1500MS Memory Limit: 131072K

Description

During The Kindergarten days, Flymouse is the monitor of his class. Occasionally the head-teacher brought the kids of Flymouse ' s Class A large bag of candies and had flymouse distribute them . All the kids loved candies very much and often compared the numbers for candies they got with others. A kid A could had the idea that though it might is the case that another kid B is better than him in some aspect and ther Efore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B di D no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain About Flymouse ' s biased distribution.

Snoopy shared class with Flymouse at that time. Flymouse always compared the number of he candies with that of Snoopy ' s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the Head-teacher, what is the largest difference he could make out of it?

Input

The input contains a single test cases. The test cases starts with a line with both integers N and M not exceeding and respectivel Y. N is the number of kids in the class and the kids were numbered 1 through N. Snoopy and Flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and C in order, meaning that kid A believed that kid B should never get over C candies more than he did.

Output

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

Sample Input

2 21 2 52 1 4

Sample Output

5

Hint

32-bit signed integer type is capable of doing all arithmetic. Test instructions: There are n personal number 1 to n,m requirements, each request is ui,vi,wi. The number of people who represent the numbering UI can only be less wi than the number of people who are numbered VI. The number of people who are asked to find numbers n can be as many as a few candies than the number 1. Analysis: Set X[i] to represent the number of sweets to be divided by people numbered I. You can get the following formula x[vi]-x[ui]<=wi; according to the formula, the map is a forward edge with a weight of wi from the UI to VI. The shortest path is then solved. Note: This problem SPFA if the queue will be tle, the stack can be AC. Of course, because the weight of the problem is all positive, it can be used Dijkstra
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5#include <stack>6#include <cstdlib>7 using namespacestd;8 #defineREP (a,x) for (int a=0; a<x; a++)9 #defineMaxe 200010Ten #defineMaxp 30010 One #defineINF 0x7fffffff A #defineMP (A, B) Make_pair (A, B) -typedef pair<int,int>PII; - structnode{ the     intV,d,next; - }edge[maxe]; - intE=0; - intHEAD[MAXP]; + voidinit () { -E=0; +REP (I,MAXP) head[i]=-1; A } at voidAdd_edge (intUintVintd) - { -edge[e].v=v; -Edge[e].d=D; -edge[e].next=Head[u]; -head[u]=e; ine++; - } to intVIS[MAXP],DIS[MAXP]; + voidSPFA () - { theREP (I,MAXP) vis[i]=0; *REP (I,MAXP) dis[i]=i==1?0: INF; $     //queue<int>q;Panax Notoginsengstack<int>Q; -Q.push (1); thevis[1]=1; +      while(!Q.empty ()) { A         //int X=q.front (); the         intx=q.top (); + Q.pop (); -          for(intt=head[x];t!=-1; t=edge[t].next) $         { $             inty=edge[t].v; -             intD=edge[t].d; -             if(dis[y]>dis[x]+d) { thedis[y]=dis[x]+D; -                 if(!Vis[y]) {Wuyi Q.push (y); thevis[y]=1; -                 } Wu             } -         } Aboutvis[x]=0; $     } - } - voidDijkstraints) - { AREP (I,MAXP) vis[i]=0; +REP (I,MAXP) dis[i]=i==s?0: INF; thepriority_queue<pii,vector<pii>,greater<pii> >Q; - Q.push (MP (dis[s],s)); $      while(!q.empty ()) the     { thePII p=q.top (); the Q.pop (); the         intx=P.second; -         if(Vis[x])Continue; invis[x]=1; the          for(intt=head[x];t!=-1; t=edge[t].next) the         { About             inty=edge[t].v; the             intD=edge[t].d; the             if(!vis[y]&&dis[y]>dis[x]+d) the             { +dis[y]=dis[x]+D; - Q.push (MP (dis[y],y)); the             }Bayi         } the     } the  - } -  the intMain () the { the     intM,n; the      while(SCANF ("%d%d", &n,&m)! =EOF) { -         intu,v,w; the init (); the REP (i,m) { thescanf"%d%d%d",&u,&v,&W);94 Add_edge (u,v,w); the         } the         //Dijkstra (1); the SPFA ();98printf"%d\n", Dis[n]); About     } -     return 0;101}
code June

poj3159 Candies (differential constraint)

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.