poj3159】的更多相关文章

http://poj.org/problem?id=3159 (题目链接) 题意 有n个小朋友,班长要给每个小朋友发糖果.m种限制条件,小朋友A不允许小朋友B比自己多C个糖果.问第n个小朋友最多比第1个小朋友多多少糖果. Solution 原来这就是所谓的差分约束..浅显易懂的博客,超详细的博客. 总结一下: >=,求最小值,做最长路: <=,求最大值,做最短路. 可能会觉得很奇怪,用线性规划的角度解释吧.其实我们需要求的就是(n)-(1)<=x或者(n)-(1)>=x,要保证满足…
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&…
题目链接:https://cn.vjudge.net/problem/POJ-3159 题意 给出一组不等式 求第一个变量和最后一个变量可能的最大差值 数据保证有解 思路 一个不等式a-b<=c,通过移项,实际上就是满足了a<=b+c 发现在整个约束系统中,a在下满足不等式的情况下求最大值,就是在求最短路 然而如果直接用BellmanFord(spfa)的话,还是会超时 这时得对Bellman做第二次优化,用stack代替queue 但是对于更多的图中,Dijsktra依然更优,所以没有必要太…
Candies POJ-3159 这里是图论的一个应用,也就是差分约束.通过差分约束变换出一个图,再使用Dijikstra算法的链表优化形式而不是vector形式(否则超时). #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include<queue>…
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <cmath> #include <algorithm> using namespace std; /** * This is a docu…
题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 a 多 c 个,也就是 d[b] - d[a] ≤ c,就可以建立 value 值为 c 的单向边 e(a,b) ,然后先定d[1] = 0 ,用最短路跑完得到的 d[n] 就是所求答案. #include<stdio.h> #include<string.h> #include<…
转载请注明出处: 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’…
不多说了.就是裸的模版题. 贴代码: <span style="font-family:KaiTi_GB2312;font-size:18px;">#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 30500 #define M 200000 #define…
https://vjudge.net/contest/66569#problem/K 相当于模板吧,第一次写spfa的 #include<iostream> #include<cstdio> #include<queue> #define ll long long using namespace std; ,inf=; ll d[N]; struct { int to,next,w; }e[N]; int H[N],x[N],y[N],z[N],V,E,num; voi…
题目链接:http://poj.org/problem?id=3159 题目很容易理解 就是简单的SPFA算法应用 刚开始用STL里的队列超时了,自己写了个栈,果断过,看来有时候栈还是快啊.... 代码: #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #define INF 0x3f3f3f3f #define maxn 30010 #define max_e…