Candies(差分约束_栈+SPFA)

CandiesCrawling in process...
Crawling failed
Time Limit:1500MS    
Memory Limit:131072KB    
64bit IO Format:
%I64d & %I64u

Submit
Status

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

Hint

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

被坑哭了的一道题。上几天学的SPFA+前向星,这题果断用上,数据都对,然后高兴的交上去,TLE,然后吓哭了。后来又用了slf优化一下,还是TLE,这真的是哭死了。然后看了一下poj的讨论区,大牛们说要用栈,sad  又去看的栈,幸亏和队列的用法差不多。

题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c  三个数,A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。最后求n 比 1 最多多多少糖果。

思路:这是一题典型的差分约束题。不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,我们得到 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术,即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。即是满足题中的条件dis[B]-dis[A]<=w(A,B),由于要使dis[B] 最大,所以这题可以转化为最短路来求。(语言组织有点差,只好用了一下别的大牛的思路)

ps:博客里有一篇转载的最短路的,看不明白的可以去学学

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#define inf 9999999
using namespace std;
int head[30010];
int dis[30010];
int vis[30010];
int n,m,i;
int cnt;
struct node
{
    int v,w;
    int next;
} edge[150000];
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()
{
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    stack<int>G;//队列
    dis[1]=0;
    vis[1]=1;
    G.push(1);
    while(!G.empty())
    {
        int u=G.top();
        G.pop();
        vis[u]=0;
        for(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;
                    G.push(v);
                }
            }
        }
    }
    printf("%d\n",dis[n]);
}
int main()
{
    int u,v,w;
    while(~scanf("%d %d",&n,&m))
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        for(i=1; i<=m; i++)
        {
            scanf("%d %d %d",&u,&v,&w);
            add(u,v,w);
        }
        SPFA();

    }
    return 0;
}



时间: 2024-11-10 07:55:07

Candies(差分约束_栈+SPFA)的相关文章

hdu 3666(差分约束,手动栈解决超时问题)

THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8016    Accepted Submission(s): 2092 Problem Description You have been given a matrix CN*M, each element E of CN*M is positive

poj3159 Candies(差分约束,dij+heap)

poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d[v] – d[u] <= w. 再看题目给出的关系:b比a多的糖果数目不超过c个,即d[b] – d[a] <= c ,正好与上面模型一样, 所以连边a->b,最后用dij+heap求最短路就行啦. ps:我用vector一直TLE,后来改用前向星才过了orz... 1 #include&

POJ 3159 Candies 差分约束

链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 24852   Accepted: 6737 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the

poj 3159 candies (差分约束 spfa+stack)

http://poj.org/problem?id=3159 题意:一个班有n个人 每人分到若干糖果 且u的糖果数不能比v少w个 求第1个人与第n个人最大数量差 照着模板spfa+queue果断tle了 之后照着题解说的把queue改成stack就过了 但是还不明白为什么会快 而且如果用数组直接模拟会比stl更快 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>

poj3159 Candies(差分约束)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit: 131072K Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’

Layout---poj3169(差分约束+最短路spfa)

题目链接:http://poj.org/problem?id=3169 有n头牛站成一排 在他们之间有一些牛的关系比较好,所以彼此之间的距离不超过一定距离:也有一些关系不好的牛,希望彼此之间的距离大于等于一定距离: 关系好的有ml个(A B D)表示A牛和B牛之间的距离<=D 关系不好的有md个(A,B,D)表示A牛和B牛之间的距离>=D,求在满足条件的排列中,求出牛1和牛n的最大距离: 如果距离可以是无限大输出-2,如果不存在这样的排列输出-1: 我们用d[i]表示第i头牛的位置,因为牛是按

poj1201 Intervals,差分约束问题,spfa

题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai,bi]这个区间的整数至少有ci个.如果存在这样的序列,请求出满足题目要求的最短的序列长度是多少.如果不存在则输出 -1. 输入:第一行包括一个整数n,表示区间个数,以下n行每行描述这些区间,第i+1行三个整数ai,bi,ci,由空格隔开,其中0<=ai<=bi<=50000 而且 1<=ci<=bi-ai+1. 输出:一行,输出满足要求的序列的长度的最小值.

[poj3159]Candies(差分约束+链式前向星dijkstra模板)

题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与dijkstra中的d[v]>=d[u]+C相同,即可求解. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream>

差分约束讲解

差分约束讲解 --by ysy 1.前置知识 ????????因为差分约束是基于\(spfa\)的一种解不等式,或等式组的技巧,所以差分约束的前置知识就是\(spfa\)和对不等式的简单小变换. 2.讲解 ?????????因为差分约束只是一个技巧,所以在这里我先讲解技巧,之后再讲解例题. 建图技巧 ?????????我们将不等式组分为两种:\(A \le B+val\)以及\(A\ge B+val\). ?????????现在讨论第一种\(A\le B+val\),我们在建图时\(B \rig