Poj3159 -- candies (difference constraint)

Source: Internet
Author: User
Candies
Time limit:1500 Ms   Memory limit:131072 K
Total submissions:23131   Accepted:6224

Description

During the Kindergarten days, flymouse was 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 of candies they got with others. A kid a coshould had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he shoshould never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he wowould 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 his 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 was the largest difference he cocould make out of it?

Input

The input contains a single test cases. The test cases starts with a line with two integersNAndMNot exceeding 30 000 and 150 000 respectively.NIs the number of kids in the class and the kids were numbered 1 throughN. Snoopy and flymouse were always numbered 1 andN. Then followMLines each holding three IntegersA,BAndCIn order, meaning that kidABelieved that kidBShocould never get overCCandies 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

A difference constraint template clearly gives a B w a more than B w, so B-A <= W. Then, a directed edge is created from B to A, and the weight is W. The maximum difference value is obtained when the shortest is run from 1 to n.

When using queue, it will time out and only stack. It may be a data issue.

 

#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <stack>#define inf 0x3f3f3f3fusing namespace std;struct node{    int v,w,next;} edge[150010];int head[30001],dis[30001],vis[30001],cnt,n,m,s;void add(int u,int v,int w){    edge[cnt].v=v;    edge[cnt].w=w;    edge[cnt].next=head[u];    head[u]=cnt++;}void spfa(){    for(int i=1;i<=n;i++)    {        dis[i]=inf;        vis[i]=0;    }    dis[s]=0;    vis[s]=1;    stack<int >Q;    Q.push(s);    while(!Q.empty())    {        int u=Q.top();        Q.pop();        vis[u]=0;        for(int i=head[u];i!=-1;i=edge[i].next)        {            int v=edge[i].v;            if(dis[v]>dis[u]+edge[i].w)            {                dis[v]=dis[u]+edge[i].w;                if(!vis[v])                {                    vis[v]=1;                    Q.push(v);                }            }        }    }}int main(){    int i,j,u,v,w;    while(~scanf("%d%d",&n,&m))    {        s=1;        for(i=1;i<=n;i++)        head[i]=-1;        cnt=0;        for(i=0; i<m; i++)        {            scanf("%d%d%d",&u,&v,&w);            add(u,v,w);        }        spfa();        printf("%d\n",dis[n]);    }    return 0;}


 

Poj3159 -- candies (difference 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.