原题地址: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. 14_Response对象

    [简述] Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象和代表响应的response对象. request和response对象既然代表请求和响应 ...

  2. C语言的奇技

    1.一个整型变量/字面值常量通常足够大,可以同时表示几个字符,所以有的C编译器允许字符常量/char及整型常量有多个字符.这表明当用' yes' 替代" yes" 时可能不会被发现 ...

  3. Windows phone 8 安装在 VMWare上错误的各种解决方案

    http://windowsasusual.blogspot.jp/2013/01/how-to-launch-windows-phone-8-emulator.html Hardware requi ...

  4. PHP输出

    样例: $a = true;$b = false;$c = 086;$d = 0x86;echo "$a hase value: ".$a; echo "<br/& ...

  5. linux安装mysql出现Could NOT find Curses (missing CURSES_LIBRARY CURSES_INCLUDE_PATH),提示解决方法

    [root@localhost mysql-5.5.11]# cmake . 出现以下错误提示: -- Could NOT find Curses (missing:  CURSES_LIBRARY ...

  6. Nginx+uWSIG+Django+websocket的实现

    1.Django+websocket django-websocket dwebsocket django-websocket是旧版的,现在已经没有人维护,dwebsocket是新版的,推荐使用dwe ...

  7. 【BZOJ】1044: [HAOI2008]木棍分割 二分+区间DP

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1044 Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, ...

  8. AngularJs项目

    AngularJs项目实践总结 今年3月接触AngularJs,并且在6月的项目中开始应用,从踩坑到填坑花了不少时间,根据项目中的实际应用情况总结了一些经验,如下: 一.UI控件选择 Angularj ...

  9. SQL Server 修改排序规则

    Net stop mssqlserver Setup /QUIET /ACTION=REBUILDDATABASE /instancename=mssqlserver /SQLSYSADMINACCO ...

  10. JavaScript跨站脚本攻击

    跨站脚本攻击(Cross-Site Scrpting)简称为XSS,指的是向其他域中的页面的DOM注入一段脚本,该域对其他用户可见.恶意用户可能会试图利用这一弱点记录用户的击键或操作行为,以窃取用户的 ...