http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=267#problem/C

 Candies

Time Limit:1500MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

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 could 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 should never get a certain number of candies fewer than B did 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 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 could make out of it?

Input

The input contains a single test cases. The test cases starts with a line with two integers N M N N. snoopy and flymouse were always numbered 1 and N. Then follow M AB c A B 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 2
1 2 5
2 1 4
  • 1.
  • 2.
  • 3.

Sample Output

5
  • 1.

这题的条件很简单就是

db-da<=x;

但是这题必须用栈的思想去解题,用队列去做会超时,我也不知道为什么??

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stack>
#define N 1000001
using namespace std;
int n,m,tt;
int head[30001];
struct node
{
    int x,y,z;
    int next;
} q1[150001];
int dis[30001],v[30001];
void add(int xx,int yy,int zz)
{
    q1[tt].x=xx;
    q1[tt].y=yy;
    q1[tt].z=zz;
    q1[tt].next=head[xx];
    head[xx]=tt;
    tt++;
}
void spfa()
{
    stack<int>q;
    for(int i=0;i<=n;i++)
    {
        dis[i]=N;
        v[i]=0;
    }
    v[1]=1;
    dis[1]=0;
    q.push(1);
    while(!q.empty())
    {
        int ff=q.top();
        q.pop();
        v[ff]=0;
        for(int i=head[ff];i!=-1;i=q1[i].next)
        {
            if(dis[q1[i].y]>dis[ff]+q1[i].z)
            {
                dis[q1[i].y]=dis[ff]+q1[i].z;
                if(v[q1[i].y]==0)
                {
                   q.push(q1[i].y);
                   v[q1[i].y]=1;
                }
            }
        }
    }
   printf("%d\n",dis[n]);
}
int main()
{
    int xx,yy,zz;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        tt=0;
        memset(head,-1,sizeof(head));
        while(m--)
        {
            scanf("%d%d%d",&xx,&yy,&zz);
            add(xx,yy,zz);
        }
        spfa();
    }
    return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.