poj3159——Candies(差分约束+SPFA堆栈)

41 篇文章 0 订阅
26 篇文章 0 订阅

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 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 2
1 2 5
2 1 4
Sample Output

5

一开始没什么思路,题意都不太懂,网上看了下才知道是差分约束,又是个新知识点。
题意是a最多能忍受b比他多c个糖果,也就是b-a<=c,把这个等式转换成图上的边,a,b为两点,c为权值,等式转换成a+c<=b就可以看出这是条a至b的有向边。图建完就可以用求最短路的方法解出1到n最多能给的糖果数。
一般用SPFA,本题没有负权环路,不能用SPFA+QUENE,开小了会WA,开大了就TLE,只能用SPFA+STACK

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 1000010
#define mod 1000000007
using namespace std;
struct Edge
{
    int v,w,next;
};
Edge edge1[MAXN<<1];
int head1[MAXN],n,m,e,vis[MAXN],dis[MAXN];
int q[MAXN];
void add(Edge *edge,int *head,int u,int v,int w)
{
    edge[e].v=v;
    edge[e].w=w;
    edge[e].next=head[u];
    head[u]=e;
}
long long spfa(Edge *edge,int *head)
{
    /*memset(vis,0,sizeof(vis));
    for(int i=1; i<=n; ++i)
        dis[i]=INF;
    dis[u]=0;
    queue<int> q;
    q.push(u);
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=1;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].w;
            if(w+dis[u]<dis[v])
            {
                dis[v]=w+dis[u];
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return dis[n];*/
    int top=0;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;++i)
        dis[i]=INF;
    vis[1]=1;
    dis[1]=0;
    q[top++]=1;
    while(top!=0)
    {
        int u=q[--top];
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(!vis[v])
                {
                    vis[v]=1;
                    q[top++]=v;
                }
            }
        }
    }
    return dis[n];
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        e=0;
        int u,v,w;
        memset(head1,-1,sizeof(head1));
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            //v-u<=w
            add(edge1,head1,u,v,w);
            //即w+u<=v,权值+起点<=终点
            e++;
        }
        long long ans;
        ans=spfa(edge1,head1);
        printf("%I64d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值