POJ——3159Candies(差分约束SPFA+前向星+各种优化)
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 28071 | Accepted: 7751 |
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 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, Band 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
本来看一道组合数学问题的,莫名其妙看到差分约束了(不明觉厉,这是什么算法!??),一看用到了我喜欢的SPFA,就研究了一下,原来SPFA还可以这么用。
这题比较坑爹,用优先队列spfa超时,自带queue也超时,用栈的spfa才1400+MS险过,后来用数组模拟了个栈。1100+MS。还有用到一个叫前向星的数据结构,也是用来储存图的,可以代替vector的邻接表,然而这东西还是有一点点麻烦的,看了一篇关于这个东西的博客之后才懂了一点。
向前星主要思想:用一个结构体数Edge[MAX_E]组记录每一条边的长度dx(边权),每一条边的终点to,每一条边的起点对应上一条边所在的结构体数组中的下标(有一点pre的意思),综上,可以得到一个结构体数组Edge[N],其中每一个元素都是一条边,其包含了边的终点,边权,边的起点对应的上一条边所在的下标。然后这样会发现还是缺少了什么——一个给出线索的数组,就是说你如何对一个起点进行遍历?显然上面的结构体数组是没有记录自身起点的数据而只有上一条边起点数据,无法得知到底从哪里开始,因此需要再来一个head数组来记录所有起点对应的最后出现的那条边所在Edge[MAX_E]中的位置i,这样一来就可以用head开头而后用pre来进行遍历,不过实践起来还是需要更多的理解的。
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N=30010; struct info
{
int to;
int dx;
int pre;
};
info E[5*N];
int heads[5*N];
int d[N],vis[N];
int Q[N],rear; inline void init()
{
MM(E);
memset(heads,-1,sizeof(heads));
memset(d,INF,sizeof(d));
memset(vis,0,sizeof(vis));
rear=0;
}
inline void add(const int &s,const int &t,const int &dx,int &cnt)
{
E[cnt].to=t;
E[cnt].dx=dx;
E[cnt].pre=heads[s];
heads[s]=cnt++;
}
inline int Scan()
{
int res = 0, ch, flag = 0; if((ch = getchar()) == '-')
flag = 1; else if(ch >= '0' && ch <= '9')
res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9' )
res = res * 10 + ch - '0'; return flag ? -res : res;
} int main(void)
{
int n,m,a,b,c,i,j,ori,cnt;
while (~scanf("%d%d",&n,&m))
{
init();
cnt=0;
for (i=0; i<m; i++)
{
a=Scan();
b=Scan();
c=Scan();
add(a,b,c,cnt);
}
d[1]=0;
vis[1]=1;
Q[rear++]=1;
while (rear)
{
int now=Q[rear-1];
rear--;
vis[now]=0;
for(i=heads[now]; i!=-1; i=E[i].pre)
{
int v=E[i].to;
if(d[v]>d[now]+E[i].dx)
{
d[v]=d[now]+E[i].dx;
if(!vis[v])
{
Q[rear++]=v;
vis[v]=1;
}
}
}
}
printf("%d\n",d[n]);
}
return 0;
}
POJ——3159Candies(差分约束SPFA+前向星+各种优化)的更多相关文章
- poj Layout 差分约束+SPFA
题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...
- POJ 1201 差分约束+SPFA
思路: 差分约束,难在建图.(我是不会告诉你我刚学会SPFA的...) 把每个区间的ai–>bi连一条长度为ci的边. k–>k+1连一条长度为0的边. k+1–>k连一条长度为-1 ...
- 【poj3169】【差分约束+spfa】
题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
- # [Poj 3107] Godfather 链式前向星+树的重心
[Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...
- POJ——1364King(差分约束SPFA判负环+前向星)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11946 Accepted: 4365 Description ...
- POJ 3159 Candies(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
- POJ 1364 / HDU 3666 【差分约束-SPFA】
POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c — sum[a]<=sum[a+b+1]−c−1 ...
- poj 3169 Layout(差分约束+spfa)
题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...
随机推荐
- Fragment 创建及替换
1.Fragment的产生与介绍 Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应 ...
- 【读书笔记】构建之法(CH4~CH6)
从chapter4至chapter6,围绕着构建过程的合作讨论构建之法,而合作与个人工作的区别却以一个微妙的问题为开端:阅读别人的代码有多难? 两人合作:(驾驶员与领航员) 合作要注意代码风格规范与设 ...
- uvm_sequence_item——sequence机制(一)
让子弹飞一会 UVM框架,将验证平台和激励分开,env以下属于平台部分,test和sequence属于激励,这样各司其职.我们可以将sequence_item 比喻成子弹,sequencer 类比成弹 ...
- Xmind几个有用的技巧
Xmind是一个很好的思维导图工具,是学习研究总结的好帮手. Xmind功能很丰富,这里只是简要列出几个比较有用的技巧. 1.善用属性 选中一个xmind元素(专业名词叫[主题])后,一般在右下角会出 ...
- 将sql 查询结果导出到excel
在平时工作中经常会遇到,sql 查询数据之后需要发送给业务人员,每次都手工执行脚本然后拷贝数据到excel中,比较耗时耗力,可以考虑自动执行查询并将结果邮件发送出来. 分两步实现: 1.执行查询将结果 ...
- react中的setState的使用和深入理解
前端框架从MVC过渡到MVVM.从DOM操作到数据驱动,一直在不断的进步着,提升着, angular中用的是watcher对象,vue是观察者模式,react就是state了,他们各有各的特点,没有好 ...
- C++ C++ 值传递、指针传递、引用传递详解
这一篇博客写的不错: https://www.cnblogs.com/dingxiaoqiang/p/8012578.html
- vue for循环中常见问题 之 求和(合计)
例:求后台返回数据this.dataInfo 中某个字段(item.totalSum)的和,只需添加computed,然后模板中直接可以使用totalSumAll (不需要再data中声明) comp ...
- ios多线程NSThread
1.简介: 1.1 iOS有三种多线程编程的技术,分别是: 1..NSThread 2.Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue ...
- MAC实现睡眠和休眠唤醒
因为苹果默认为休眠文件加密,Clover 是无法解密的.所以需要经过一些设置才能破除这无节操的加密文件sleepimage.在这之前不得不提下EmuVariableUefi-64.efi 这个驱动.我 ...