原题地址: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的条件。

不懂的可以翻一下算法导论,上面有差分约束的定义和证明,总之这是一个求最短路的问题==。

其实为了理解题意,我也花了很久时间SF-_-。

知道是求最短路之后,做法就有很多了,BUT数据量较大,很多做法会超时。这里推荐两个算法,一个是优先队列优化的Dijkstra算法,AC代码如下(579MS):

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
struct CNode
{
int k;
int w;
bool operator<(const CNode& cmp) const
{
return w>cmp.w;
}
}; priority_queue<CNode> pq;
bool vis[];
int first[],vv[],ww[],nxt[];
const int inf=~(<<);
CNode p,q; int main()
{
// freopen("in.txt","r",stdin); memset(d,,sizeof(d)); int e=;
int n,m,u,v,w;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
nxt[e]=first[u],vv[e]=v,ww[e]=w,first[u]=e++;
} p.k=;
p.w=;
pq.push(p);
while(!pq.empty())
{
p=pq.top();
pq.pop(); if(vis[p.k])
continue;
vis[p.k]=true; if(p.k==n)
break;
for(int e=first[p.k];e;e=nxt[e]) if(!vis[vv[e]])
{
q.k=vv[e];
q.w=p.w+ww[e];
pq.push(q);
}
}
printf("%d\n",p.w);
}

另一个是栈优化的SPFA算法(532MS):

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std; int d[];
int stack[]; bool vis[];
int first[],vv[],ww[],nxt[];
const int inf=~(<<); int main()
{
// freopen("in.txt","r",stdin); memset(d,,sizeof(d)); int e=;
int n,m,u,v,w;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
nxt[e]=first[u],vv[e]=v,ww[e]=w,first[u]=e++;
}
memset(d,0x7f,sizeof(d)); int top=;
stack[++top]=;
vis[]=true;
d[]=; while(top)
{
int a=stack[top--];
vis[a]=false; for(int e=first[a];e;e=nxt[e]) if(d[vv[e]]>ww[e]+d[a])
{
d[vv[e]]=ww[e]+d[a];
if(!vis[vv[e]])
{
stack[++top]=vv[e];
vis[vv[e]]=true;
}
}
}
printf("%d\n",d[n]);
}

POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)的更多相关文章

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

    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...

  2. POJ 3159 【朴素的差分约束】

    好吧终于知道什么是“高大上”的差分约束了.嗷嗷 题意: 小朋友们分糖果,某个小朋友不想另外一个小朋友分到的糖果数比自己多N块以上. 求编号为N的小朋友最多比编号为1的小朋友多分多少块糖果. 思路: 差 ...

  3. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

  4. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  5. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  6. (简单) POJ 3159 Candies,Dijkstra+差分约束。

    Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...

  7. POJ 3159 Candies 【差分约束+Dijkstra】

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

  8. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  9. 图论--差分约束--POJ 3159 Candies

    Language:Default Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 43021   Accep ...

随机推荐

  1. myeclipse-8.6.0下载

    myeclipse老版本不分32位和64位,欢迎大家下载使用! 链接:http://pan.baidu.com/s/1dEJCxcl 密码:z1ga

  2. [Effective Objective-C 读书笔记] 第1章 几条基本写法 (2~5条)

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3575599.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  3. ACM学习

    转:ACM大量习题题库   ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库.   US ...

  4. DB2数据库中提高INSERT性能详解

        分类: Linux INSERT 处理过程概述 首先让我们快速地看看插入一行时的处理步骤.这些步骤中的每一步都有优化的潜力,对此我们在后面会一一讨论. 在客户机准备 语句.对于动态 SQL,在 ...

  5. php 一维数组排序,保留key值

    function sort_with_keyName($arr,$orderby='desc'){ //在内存的另一处 $a 复制内容与 $arr 一样的数组 foreach($arr as $key ...

  6. jquery与后台相互传递中文参数乱码

    前端.后台传递中文参数,乱码的情况: var a="参数乱码"; //编译两次        window.location.href=encodeURI(encodeURI(&q ...

  7. 解决VS2008打开假死或者打开设计模式假死的问题

    我昨天刚装完OFFICE 2010一直也没重启,vs2008是可以使用的.今早来到办公室,打开工程发现开了半天VS2008一般空白,查看应用程序显示正在运行,再查看进程发现里面多一个setup.exe ...

  8. jQuery手风琴广告展示插件

    效果说明:当鼠标移动到已折叠广告的标题后,折叠当前已展开的广告,并同步展开相应的折叠广告.这种Accordion效果,看似简单,但因为存在动画同步的问题,不能简单地用两个animate()来实现.必须 ...

  9. C# IO操作磁盘上的txt

    using System.IO; //写入并导出到磁盘 StreamWriter sw = new StreamWriter(@"H:\text.txt"); sw.WriteLi ...

  10. 【BZOJ】1925: [Sdoi2010]地精部落 DP+滚动数组

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1925 题意:输入一个数N(1 <= N <= 4200),问将这些数排列成折线 ...