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 AB 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

题意给你n个熊孩子,现在给熊孩子们分配蜡烛,这n个熊孩子会提出m个要求,每个要求由a,b,c三个整数表示,意思是b孩子的蜡烛最多不能比a孩子多c个。

问你最后满足这样的要求后,第一个孩子和最后一个孩子最多会多拿多少个蜡烛。

这个提一开始想复杂了,首先意识到的就是松弛操作和这个题的描述差不多。
d[b]-d[a]<=dis[a,b]与松弛操作if (d[b]>d[a]+dis[a,b] d[b]=d[a]+dis[a,b]一样!就是在a,b两点之间建一条长度为后来看了大家的题解说spfa+queue会超时,只能手写堆栈。

代码如下

 #include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
const int Maxn=;
const int Maxe=;
const int inf =0x3f3f3f3f;
int n,m;
int tot;
int head[Maxn];
bool vis[Maxn];
int d[Maxn];
int Q[Maxn];
struct Edge
{
int to;
int dis;
int nxt;
}edge[Maxe];
void add (int a,int b,int c)
{
edge[tot].to=b;
edge[tot].dis=c;
edge[tot].nxt=head[a];
head[a]=tot++;
}
void spfa (int start,int n)
{
int top=;
for (int v=;v<=n;++v){
if (v==start){
Q[top++]=v;
vis[v]=true;
d[v]=;
}
else{
vis[v]=false;
d[v]=inf;
}
}
while (top!=){
int u=Q[--top];//top是盖子的位置
vis[u]=false;
for (int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if (d[v]>d[u]+edge[i].dis){
d[v]=d[u]+edge[i].dis;
if (!vis[v]){
vis[v]=true;
Q[top++]=v;
}
}
}
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&n,&m)){
tot=;
memset(head,-,sizeof head);
for (int i=;i<m;++i){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
spfa(,n);
printf("%d\n",d[n]);
}
return ;
}

POJ 3159 Candies(spfa、差分约束)的更多相关文章

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

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

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

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

  3. POJ 3159 Candies(差分约束+最短路)题解

    题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...

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

    <题目链接> 题目大意: 给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c .最后求n 比 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(差分约束)

    http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...

  7. poj 3159 Candies (差分约束)

    一个叫差分约束系统的东西.如果每个点定义一个顶标x(v),x(t)-x(s)将对应着s-t的最短路径. 比如说w+a≤b,那么可以画一条a到b的有向边,权值为w,同样地给出b+w2≤c,a+w3≤c. ...

  8. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...

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

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

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

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

随机推荐

  1. hdu 1007 Quoit Design (经典分治 求最近点对)

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

  2. express中 使用session与cookie

    1.express如何使用session与cookie : https://www.jianshu.com/p/1839e482274e  或  https://www.cnblogs.com/chy ...

  3. C#面向对象笔记

    1.面向对象核心概念 (1)类是抽象,对象是实例,new一个对象会分配一块堆空间,对象指向该空间的地址,将对象赋值给另一个对象,只是将地址赋给它,指向的是同一块空间. e.g. class Car { ...

  4. wsl和windows相互访问文件夹

    How to access Windows folders from Bash on Ubuntu on Windows You'll find the Windows C:\ structure a ...

  5. Linux编程 多进程,多线程求解PI(圆周率)

    题目: 链接 多进程: #include <unistd.h> #include <stdio.h> #include <stdlib.h> #define n 1 ...

  6. phpstorm中sass编译时目录或内容包含中文字符报错

    ruby版本:ruby 2.4.1p111 (2017-03-22 revision 58053) [x64-mingw32] sass版本:Sass 3.4.24 (Selective Steve) ...

  7. 抽象类 抽象方法 abstract

    abstract: * abstract修饰类: 抽象类 * > 此类不能被实例化 * > 抽象类中一定要有构造器, 便于子类对象实例时调用(涉及子类对象实例化过程) * > 开发中 ...

  8. Python 进阶_OOP 面向对象编程_类和继承

    目录 目录 类 最简单的类 类方法 构造器 __init__ 创建一个类 实例化一个对象 调用实例的方法和属性 创建子类 使用 super 来调用父类的构造器 实例化子类对象 调用子类的属性和方法 类 ...

  9. jQuery:unbind方法的使用详解

    一.概述: unbind方法只能解绑用jQuery的bind方法以及用jQuery方法注册的事件处理程序.比如:$(‘a’).click(function(){})可以通过unbind解绑.用原生ad ...

  10. QTP read or write XML file

    'strNodePath = "/soapenv:Envelope/soapenv:Body/getProductsResponse/transaction/queryProducts/qu ...