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 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, B and 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

Hint


32-bit signed integer type is capable of doing all arithmetic.


##题意:

求图中#1到#N的最短路.


##题解:

题是很裸的最短路,但是数据非常大.
队列形式的spfa会TLE.
这里需要用栈来优化spfa.
事实上,在不需要判断负环的情况下,栈实现spfa比队列要快. (涨姿势了)


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 200000
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int m,n,k;

int edges, u[maxn], v[maxn], w[maxn];

int first[maxn], next[maxn];

int dis[maxn];

void add_edge(int s, int t, int val) {

u[edges] = s; v[edges] = t; w[edges] = val;

next[edges] = first[s];

first[s] = edges++;

}

//queue q;

int Q[maxn];

bool inq[maxn];

int inq_cnt[maxn];

bool spfa(int s) {

int top = 0;

memset(inq, 0, sizeof(inq));

memset(inq_cnt, 0, sizeof(inq_cnt));

for(int i=1; i<=n; i++) dis[i] = inf; dis[s] = 0;

//while(!q.empty()) q.pop();

//q.push(s);

inq_cnt[s]++;

Q[top++] = s;

while(top) {
int p = Q[--top];
inq[p] = 0;
for(int e=first[p]; e!=-1; e=next[e]) if(dis[v[e]] > dis[p]+w[e]){
dis[v[e]] = dis[p] + w[e];
if(!inq[v[e]]) {
//q.push(v[e]);
Q[top++] = v[e];
inq[v[e]] = 1;
inq_cnt[v[e]]++;
//if(inq_cnt[v[e]] >= n) return 0;
}
}
} return 1;

}

int main(int argc, char const *argv[])

{

//IN;

/*
spfa+stack 用queue会TLE
*/ while(scanf("%d %d",&n,&m) != EOF)
{
edges = 0;
memset(first, -1, sizeof(first));
for(int i=1; i<=m; i++) {
int u,v,w; scanf("%d %d %d",&u,&v,&w);
add_edge(u,v,w);
} spfa(1); printf("%d\n", dis[n]);
} return 0;

}

POJ 3159 Candies (栈优化spfa)的更多相关文章

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

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

  2. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  3. poj 3159 Candies (dij + heap)

    3159 -- Candies 明明找的是差分约束,然后就找到这题不知道为什么是求1~n的最短路的题了.然后自己无聊写了一个heap,518ms通过. 代码如下: #include <cstdi ...

  4. POJ 3159 Candies(SPFA+栈)差分约束

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

  5. POJ 3159 Candies 还是差分约束(栈的SPFA)

    http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...

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

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

  7. SPFA/Dijkstra POJ 3159 Candies

    题目传送门 题意:n个人发糖果,B 比 A 多 C的糖果,问最后第n个人比第一个人多多少的糖果 分析:最短路,Dijkstra 优先队列优化可过,SPFA竟然要用栈,队列超时! 代码: /****** ...

  8. poj 3159 Candies(dijstra优化非vector写法)

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

  9. POJ 3159 Candies(spfa、差分约束)

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

随机推荐

  1. JS中字符串拼装 单双引号的处理 字符转义

    js中可能会用到动态追加元素,可能数据也是从后台传过来的,当然有两种思路, 1.在后台拼装好直接返回; 2.在前台js里面拼装, 如果拼装大量的html时可能单双引号就容易出问题;那么如何解决呢?最近 ...

  2. 【转载】React初学者入门须知

    http://www.oschina.net/news/75530/9-things-every-reactjs-beginner-should-know react.js入门学习 看了一遍,没什么特 ...

  3. UVa 1625 Color Length

    思路还算明白,不过要落实到代码上还真敲不出来. 题意: 有两个由大写字母组成的颜色序列,将它们合并成一个序列:每次可以把其中一个序列开头的颜色放到新序列的尾部. 对于每种颜色,其跨度定义为合并后的序列 ...

  4. Asp.Net操作FTP方法

    将用户上传的附件(文件.图片等)通过FTP方式传送到另外一台服务器上,从而缓解服务器压力 1.相关的文章如下: Discuz!NT中远程附件的功能实现[FTP协议] http://www.cnblog ...

  5. Jquery 弹出提示框输入插件 apprise 修改中文按钮以及使用说明

      apprise的使用非常简单,引入js脚本和css <script type="text/javascript" src="/js/apprise-1.5.fu ...

  6. codeforces 334A - Candy Bags

    忘了是偶数了,在纸上画奇数画了半天... #include<cstdio> #include<cstring> #include<cstdlib> #include ...

  7. db2数据库Date相关函数

    1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2' FROM SYSIBM ...

  8. Oracle中如何判断一个字符串是否含有汉字

    看到网友问,怎么查询表中某个字段数据是不是包含了全角字符啊? 这个问题涉及到几个函数:to_single_byte.length和lengthb,我之前做开发的时候研究的是如何判断一个字符串中是否包含 ...

  9. 图片鼠标滑过图片半透明(jquery特效)

    在做瑞祥之旅的过程,有一个部分是材料体系,材料体系下面.预览效果

  10. window.history

    作者:zccst 旧版: forword() backword() go(number) HTML5中新增了 onhashchange  浏览器兼容性较好,用得较多 pushState / repla ...