poj 3159 Candies (dij + heap)】的更多相关文章

3159 -- Candies 明明找的是差分约束,然后就找到这题不知道为什么是求1~n的最短路的题了.然后自己无聊写了一个heap,518ms通过. 代码如下: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; ; ; struct Edge { int t, nx, c; } edge[M]; int eh[…
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 candies and had flymouse distribute them. All the kids…
分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u,v,c建单向边,一条从1到n的路径就是一个关于1和n的推广不等式a[n]-a[1]<=k(k为这条路的权) 所以找到所有不等式中最小k,就是求1到n的最短路,这就是差分约束 然后上代码: #include<cstdio> #include<cstring> #include&l…
题目链接:http://poj.org/problem?id=3159 题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c . 最后求n比1最多多多少糖果. 可以从条件中的 B-A<=c及B<=A+c最后要达成这个条件就是要当B>A+c时B=A+c即可 所以差不多就是求最短路.这题中还有一些优化比如 if(vis[u]) continue;这个避免了u点的重复查找 #include <…
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] 多多少糖? 差分约束问题, 就是求1-n的最短路,  队列实现spfa 会超时了,改为栈实现,就可以 有负环时,用栈比队列快 数组开小了,不报RE,报超时 ,我晕 #include <iostream> #include <cstdlib> #include <cstdio>…
Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 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 b…
http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让会神给讲的,用的dijkstra,看的网上很多用SPFA的. 关于SPFA:http://www.cnblogs.com/devtang/archive/2011/08/25/spfa.html http://blog.csdn.net/chenjiang492943457/article/deta…
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的尽量多. 比如现在ABCD四个小朋友,B的糖果不能超过A的5个,如果A的史努比,D是班长,那么班长最多比史努比多7个糖果,而不是5+4+1=9个. 因为如果是9个,就不满足D-A<=(D-C)+(C-A)<=7的条件. 不懂的可以翻一下算法导论,上面有差分约束的定义和证明,总之这是一个求最短路的问…
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A的糖果数<=C . 最后求n 比 1 最多多多少颗糖果. 解题思路:经典差分约束的题目,具体证明看这里<数与图的完美结合——浅析差分约束系统>. 不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,我们得到 dis[B]-dis[A]<=w(A,B).看到这里,我们可以联想到…
http://poj.org/problem?id=1511 求解从1去其他顶点的最短距离之和. 加上其他顶点到1的最短距离之和. 边是单向的. 第一种很容易,直接一个最短路, 然后第二个,需要把边反向建一次,跑一个最短路就好. ★.cin  cout 超时 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #…
http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯,我先揭发一下,1号是分糖果的孩子,班长大人!(公报私仇啊...,欺负N号的小朋友~ 好吧,我开玩笑的) 嗯,这题要求最短路径.为啥是最短?你以前都在玩最长呀~ 因为这题要求的是最大的.图的三角不等式有:d[v]- d[u]<=w(u,v);  d[v]<=d[u]+w(u,v); 即而松弛的条件为…
Language:Default Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 43021   Accepted: 12075 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse…
Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 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 b…
Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J 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 fl…
Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 25776   Accepted: 7033 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 b…
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 ofte…
<题目链接> 题目大意: 给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…
一个叫差分约束系统的东西.如果每个点定义一个顶标x(v),x(t)-x(s)将对应着s-t的最短路径. 比如说w+a≤b,那么可以画一条a到b的有向边,权值为w,同样地给出b+w2≤c,a+w3≤c.那么a到c的最大差就受这些不等式约束,对应着图中的最短路. 这个边多,不要用vector存,满了以后重新复制数据,会T的,没试过指定大小... #include<iostream> #include<cstdio> #include<vector> #include<…
题目链接: http://poj.org/searchproblem 题目大意: 飞天鼠是班长,一天班主任买了一大包糖果,要飞天鼠分发给大家,班里面有n个人,但是学生A认为学生B比自己多的糖果数目不应该大于c,如果不满足自己的条件,学生A就会向老师告状,在这个班级里面泰迪熊的编号是1,班长的编号是n,班长想和泰迪熊的糖果相差最大,问:在满足m个学生的要求后,班长与泰迪熊的糖果相差最大是多少? 解题思路: 差分约束系统,|Xa-Xb| <= c,我们假设Xa小于Xb,把糖果的最大差当成边权,因为要…
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 ofte…
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是"差别", 而是"做差"....) 思路 差分约束 差分约束顾名思义就是以"差值"作为约束条件的规划问题. 这个"差值"的特点使得这个问题可以转化为最短路问题(或最长路?) 由于SFPA(或Dijkstra)中的松弛操作: d[v] <…
题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa的松弛条件相对应,所以我们建一条a到b的边,权值c,然后跑最短路,求出所有差值最大的那个即为答案.应该算是基础的线性差分约束题. ps:队列超时,这里用栈. 关于差分约束可以点这里 #include<cstdio> #include<set> #include<map> #…
题目传送门 题意:n个人发糖果,B 比 A 多 C的糖果,问最后第n个人比第一个人多多少的糖果 分析:最短路,Dijkstra 优先队列优化可过,SPFA竟然要用栈,队列超时! 代码: /************************************************ * Author :Running_Time * Created Time :2015-9-1 19:18:52 * File Name :POJ_3159.cpp ************************…
题意:n个小孩,m个比较(给你两个孩子代号a,b.然后c意味着a比b最多只能少c个糖果),问1和n之间差距最大的糖果数量. 思路:这是一个差分约束思路 不懂得:传送门, 转化一下就是一个SPFA求最短路的问题了. 参照了kuangbin大神的模板. #include<cstring> #include<cmath> #include<cstdio> ; const int MAX=0x3f3f3f3f; int head[qq]; //每个结点的头指针. bool vi…
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... #include&…
题目链接:http://poj.org/problem?id=3159 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 k…
http://poj.org/problem?id=3159 Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 33328   Accepted: 9349 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of fly…
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 t…
来补一下自己很久以前那个很蒟蒻很蒟蒻的自己没有学懂的知识 差分约束,说白了就是利用我们在求最短路的一个\(relax\)操作时的判断的原理 \[dis[v]>dis[u]+disj(u,v)\] 然后题目中一般会给你一堆不等关系,我们就可以将他们转化成一个点一个点之间的约束关系 然后,这种东西就可以做啦 然后再来说一下这道题目 题目要求,对于给定的\(A,B,C\),使得 \[B-A<=C\] 然后我们发现,这不就是一道裸题吗 于是就可以愉快的码代码了 然后,再提醒一句,由于本题数据过于毒瘤,…
题意: 就是分糖果 然后A觉得B比他优秀  所以分的糖果可以比他多 但最多不能超过c1个, B又觉得A比他优秀.... 符合差分约束的条件 设A分了x个  B分了y个  则x-y <= c1 , 根据其它的关系可以找出c2 c3 ···· 如果不懂差分约束的请  点击 所以构成不等式组:x-y <= c1   x-y <= c2    x-y <=c3 因为这些条件要都符合 所以取最小的c  即最短路 #include <iostream> #include <c…