poj 3159 Candies

Candies
Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 29377 Accepted: 8135

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 and M not exceeding 30 000 and 150 000 respectively. 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 AB 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 2
1 2 5
2 1 4

Sample Output

5

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

提示

题意:

飞鼠是班长,老师给了飞鼠一些糖果,班上有n(1<=n<=30000)个小孩,有m(1<=m<=150000)条信息,每条信息表示第a个小孩的糖果数不会比第b个小孩多c个糖果。求出第1个到第n个小孩最多相差多少个糖果。

思路:

差分约束系统基础题,不等式有:

a[i]-a[i-1]<=c.

用spfa求最短路(用最小来更新)就行了。值得注意的是用队列会TLE,把队列改成栈会快很多。

Dijkstra需要有优先队列优化。下面只给出spfa,Dijkstra表示不会(%>_<%)。

示例程序

Source Code

Problem: 3159		Code Length: 1122B
Memory: 2148K		Time: 579MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
#define MAX 1000000007
struct
{
    int v,c,next;
}w[150000];
int h[30000],q[200000],numw;
void insert(int u,int v,int c)
{
    w[numw].v=v;
    w[numw].c=c;
    w[numw].next=h[u];
    h[u]=numw;
    numw++;
}
int spfa(int n)
{
    int i,v[30000],d[30000],pos,pos1,top=0;
    for(i=0;30000>i;i++)
    {
        v[i]=0;
        d[i]=MAX;
    }
    d[0]=0;
    q[top]=0;
    top++;
    v[0]=1;
    while(top!=0)
    {
        pos=q[top];
        v[pos]=0;
        for(i=h[pos];i!=-1;i=w[i].next)
        {
            pos1=w[i].v;
            if(d[pos1]>d[pos]+w[i].c)
            {
                d[pos1]=d[pos]+w[i].c;
                if(v[pos1]==0)
                {
                    q[top]=pos1;
                    top++;
                    v[pos1]=1;
                }
            }
        }
        top--;
    }
    return d[n-1];
}
int main()
{
    int n,m,i,u,v,c;
    numw=0;
    memset(h,-1,sizeof(h));
    scanf("%d %d",&n,&m);
    for(i=1;m>=i;i++)
    {
        scanf("%d %d %d",&u,&v,&c);
        insert(u-1,v-1,c);
    }
    printf("%d",spfa(n));
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值