码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-08-13 12:56:38      阅读:156      评论:0      收藏: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;
}

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

标签:

原文地址:http://blog.csdn.net/blue_skyrim/article/details/52198776

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!