Candies POJ - 3159 (差分约束+最短路)

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

Candies

POJ - 3159

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.

首先如果你不知道差分约束系统是什么请看下面链接

差分约束系统详解

http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html

code:

/**
题意:给n个人分配糖果,给出m组数据,每组数据包含A,B,c三个数
意思是B的糖果数比A多的个数不能超过c,也就是Numb - Numa <= c;
最后求n比1最多可以多多少个糖果

解题思路:
这是一道查分约束题,不妨将糖果数当做距离,把两个人之间不能超过的
那个差值看成有向边AB的权值,则根据题目的意思我们就可以得到
dis[B] - dis[A] <= w(A,B),那么这个问题就可以转化成判断
dis[B] > dis[A] + w(A,B),如果为真,就更新使得dis[B] = dis[A] + w(A,B)
这样就变成了求最短路
spfa + stack 使用邻接表
**/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 30010;
const int MAXE = 150010;
const int INF = 0x3f3f3f3f;
int first[MAXN];//储蓄每个顶点的第一条边的编号
int vis[MAXN];//标记数组

int Q[MAXN];//栈模拟数组
int dis[MAXN];//距离
struct Edge{
    int to;
    int v;
    int next;
}edge[MAXE];
int tol;
void add(int a,int b,int v){//加边
    edge[tol].to = b;
    edge[tol].v = v;
    //这两句为邻接表的关键语句
    edge[tol].next = first[a];//先更新第一条边的下一条边,把下一条边更新成当前的第一条边
    first[a] = tol;//然后在更新第一条边

    tol++;
}
void spfa(int start,int n){
    int top = -1;
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[start] = 0;
    vis[start] = 1;
    Q[++top] = start;
    while(top > -1){
        int u = Q[top--];
        vis[u] = 0;
        int i;
        for(i = first[u]; i != -1; i = edge[i].next){
            int v = edge[i].to;
            if(dis[v] > dis[u] + edge[i].v){
                dis[v] = dis[u] + edge[i].v;
                if(!vis[v]){
                    vis[v] = 1;
                    Q[++top] = v;
                }
            }
        }
    }
}
int main(){
    int n;
    int M;
    int a,b,c;
    while(~scanf("%d%d",&n,&M)){
        tol = 0;//给每条边编号
        memset(first,-1,sizeof(first));
        while(M--){
            scanf("%d%d%d",&a,&b,&c);
            //b-a <= c
            add(a,b,c);
        }
        spfa(1,n);
        cout << dis[n] << endl;
    }
    return 0;
}


短路算法.pdf 这是短路径的算法,刚学的,有所不足敬请谅解。 立即下载

相关推荐

poj 3159 Candies(优先队列 dijkstra+堆)

题目链接 http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 26118   Accepted: 7147 Description During the kinderga

ACMan 539

No.8 - POJ1135 迪杰斯特拉 短路 非负权

Dijkstra算法: // ShellDawn // POJ1135 // No.8 #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #define MM(x) memset(x,0,sizeof(x)) using namespace std;...

ShellDawn的博客 200

初步 poj3159Candies

终于放假了,考试爆炸,算了,还不如学学算法,Dijkstra`s算法。。。 先给题目链接好吧,poj3159Candies:http://poj.org/problem?id=3159   题目意思抄一下: 有N个孩子(N&lt;=3000)糖果。有M个关系(M&lt;=150000)每个关系形如: A B C 表示第B个学生比第A个学生多到的糖果数目,不能超过C 求第N个学生...

A_Pathfinder的博客 454

poj3159 Candies 短路&差分约束

题目链接:http://poj.org/problem?id=3159 题意:n个点,m对A,B,C,表示B的值减A的值不超过C,求1和n的大差值(后来我才知道这叫差分约束)。 析:开始拿到这题有点懵比,没读懂,后来自己在草稿上画了画感觉就是求1点到n点的短路。 如下图: 后来写了spfa+queue就交了上去T了一发,想了想,没招了,看了disscus发现说用堆栈可以,可是我不

Na_OH的博客 498

poj3159 Candies差分约束短路

题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人多比1号人多几个糖果 思路:对应短路模型,在松弛完短路后则变为  d[v] 这个和上面的 B - A 吐槽1:用spfa+queue超时..得改用stack,用刘汝佳的紫书上的dijkstra+优先队列模板也会超时,要改用数组实现的邻接表可以过 吐槽2: 想了一下觉

围巾的ACM博客 2814

POJ 3159 Candies (图论,差分约束系统,短路

POJ 3159 Candies (图论,差分约束系统,短路) 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...

dipinzhu4111的博客 177

【解题报告】 POJ - 3159 Candies(差分约束短路

原题地址 其实这个题目思考起来和实现起来并不难 ,但是为了码一下发现的dalao的博客,还是写一下题解 dalao博客: 夜深人静写算法(四) - 差分约束 真的写的非常好啊!可以说是帮我又把短路和建图的知识复习了一遍,吹爆dalao,简单的逛了一下,他其他的文章包括dp、线段树、树状数组等等都写的非常认真非常仔细,还附题集,每一题还点出了重点是什么,可以说是非常良心了!决定了,以后就跟着这个d...

Jiengup_的博客 377

POJ - 3159 Candies短路+差分约束思想)

题目链接 题意:n个小孩,每个小孩a都认为自己应该比小孩b少得x颗糖果,为了满足每个小孩,第一个小孩多比第n个小孩多得几颗糖果 析:差分约束,跑一个短路即可,如果不了解, 点击这里: 差分约束 #include <set> #include <map> #include <cmath> #include <stack> #include <queue> #include <string> #include <vect

weixin_46220615的博客 121

Candies POJ - 3159短路+差分约束

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 t...

weixin_30367945的博客 151

POJ - 3159 Candies —— 差分约束短路

题意: 一群人糖果,有m个限制关系每个关系表明B所拥有的糖果数不能比A多C,问第一个人和第二个人的糖果多能差多少。 思路: 差分约束,对于每个限制关系,B-A&lt;=C,那么就从A到B建一条边,跑短路即可 本来是很简单的差分约束,但是就是死活过不了,必须用vecter优化,  v.clear();    v.resize(n+1); 学到了... #include &lt;io...

Lngxling的博客 524

POJ3159 Candies差分约束+短路

Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 38621   Accepted: 10891 Description During the kindergarten days, flymouse was the monitor of his class. Occasio...

逐梦者 385

POJ 3159 Candies(短路 差分约束)

Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 31848 Accepted: 8896 DescriptionDuring the kindergarten days, flymouse was the monitor of his class. Occasionally the

Twillz的博客 378

POJ 3159 Candies(spfa短路差分约束

题意:幼儿园俩小朋友不和..一个编号是1,一个编号是n,给出m个关系,就是A小朋友多比B小朋友多C个糖果,问n多比1多多少个糖果... n、m很大...开始我想着是求大路,但是spfa没法求因为这题是有环的...后来一想,直接边权为c不就是大了么..求一下短路就行了吧...因为求大路可能对于某些条件来说产生冲突...为了保证条件的合理性,只能选择短路...接下来 TLE...nm范

439

poj 3159 Candies(差分约束原理 短路)

题意:不懂差分约束的请看我转载的一篇差分约束详解http://blog.csdn.net/kirito_j/article/details/52132170 看完那个链接你们就懂了,在型如x-y CandiesTime Limit:1500MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu SubmitStatusPr

Kirito_j的博客 518

短路练习11/poj/3159差分约束系统】Candies

没看题解前完全不知道题是啥意思,完全不能把这道题跟短路联系在一块0.0 题意:现在有一些小朋友,有许多的糖果,给出m组关系;例如1 2 5表示1号小朋友不想让2号小朋友比他多超过5个糖果,2 1 4表示2号小朋友不想让1号小朋友比他多超过4个糖果, 然后列为关系式:a2-a1<=5,a1-a2<=4;求1号小朋友和n号小朋友多能相差多少糖果; 思路:差分约束系统是什么东东我看了一晚上也没怎么弄明白。就知道这道题是差分约束的裸模板; 看不懂就把它当成短路就行,例如样例,把样例变成a2<=

Accepted 379

POJ 3159 Candies差分约束短路

Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Description During the kindergarten days, flymouse was the monitor of his cla...

weixin_34228617的博客 224
上一篇: Agri-Net POJ - 1258 (最小生成树)
下一篇: Telephone Lines POJ - 3662(二分+最短路)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值