Time Limit: 1500MS Memory Limit: 131072K

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.

Source

POJ Monthly–2006.12.31, Sempr

题意:发一些糖果,其中他们之间的糖果数目有一定的约束关系,u,v,w 表示a[v]<=a[u]+w,求a[n]-a[1]的最大值

分析:典型的差分约束问题,不过在求最短路的过程中,不能用queue,会超时(不造什么原因),用stack。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <vector>
#include <stack>
#include <iostream>
#include <algorithm> using namespace std; const int MaxN = 31000; const int MaxM = 151000; const int INF = 0x3f3f3f3f; typedef struct node
{
int v,w,next;
}Line ; Line Li[MaxM]; int Head[MaxN],top; int Dis[MaxN]; bool vis[MaxN]; int n,m; void AddEdge(int u,int v,int w)
{
Li[top].v = v; Li[top].w = w; Li[top].next = Head[u]; Head[u] = top++;
} void SPFA()//求最短路
{
stack<int>Q;//用stack memset(Dis,INF,sizeof(Dis)); memset(vis,false,sizeof(vis)); Dis[1] = 0 ; vis[1]=true; Q.push(1); while(!Q.empty())
{
int u = Q.top(); Q.pop(); for(int i = Head[u];i!=-1;i = Li[i].next)
{
int v = Li[i].v; if(Dis[v]>Dis[u]+Li[i].w)
{
Dis[v] = Dis[u]+Li[i].w; if(!vis[v])
{
vis[v]=true; Q.push(v);
}
}
} vis[u] = false;
}
} int main()
{
int u,v,w; while(~scanf("%d %d",&n,&m))
{
memset(Head,-1,sizeof(Head)); top = 0; for(int i=0;i<m;i++)
{
scanf("%d %d %d",&u,&v,&w); AddEdge(u,v,w);
} SPFA(); printf("%d\n",Dis[n]-Dis[1]);
}
return 0;
}

Candies-POJ3159差分约束的更多相关文章

  1. POJ3159:Candies(差分约束)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 39666   Accepted: 11168 题目链接:h ...

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

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

  3. POJ3150 Candies【差分约束】

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

  4. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

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

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

  6. POJ - 3159(Candies)差分约束

    题意: 就是分糖果 然后A觉得B比他优秀  所以分的糖果可以比他多 但最多不能超过c1个, B又觉得A比他优秀.... 符合差分约束的条件 设A分了x个  B分了y个  则x-y <= c1 , ...

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

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

  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 (差分约束)

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

  10. POJ 3159 Candies(差分约束)

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

随机推荐

  1. C# 关键字【转】

      C#中的关键字 关键字是对编译器具有特殊意义的预定义保留标识符.它们不能在程序中用作标识符,除非它们有一个 @ 前缀.例如,@if 是有效的标识符,但 if 不是,因为 if 是关键字. 下面是列 ...

  2. Java线程:线程的同步-同步方法

    Java线程:线程的同步-同步方法   线程的同步是保证多线程安全访问竞争资源的一种手段. 线程的同步是Java多线程编程的难点,往往开发者搞不清楚什么是竞争资源.什么时候需要考虑同步,怎么同步等等问 ...

  3. vs2015企业版和专业版秘钥

    专业版:HMGNV-WCYXV-X7G9W-YCX63-B98R2企业版:HM6NR-QXX7C-DFW2Y-8B82K-WTYJV 来自http://www.cnblogs.com/xuhongfe ...

  4. AngularJS基础知识2

    一.angularJS双向数据绑定 利用双向数据绑定,不仅能把数据模型的变化同步到视图上面,还可以利用双向数据绑定的特性来做一些样式上面的控制. 双向数据绑定用处很多,不仅仅是像知识点1中的那个例子, ...

  5. Sqlserver 循环表

    CREATE TABLE dbo.[User] ( UID BIGINT IDENTITY ,Name ) NOT NULL ,Pwd ) NOT NULL ,CONSTRAINT PK_User P ...

  6. iBatis简单入门教程

    iBatis 简介: iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快.如果不需要太多复杂的功能,iBatis 是能够满足 ...

  7. 电脑文件出现“windows-文件发生意外问题-可修复(严禁修改)-错误代码0X00000BF8”错误,怎么办

    电脑文件出现"windows-文件发生意外问题-可修复(严禁修改)-错误代码0X00000BF8"错误,怎么办 下载一个"纵情文件修复器"修复一下就可以了 下载 ...

  8. 闲说HeartBeat心跳包和TCP协议的KeepAlive机制

    很多应用层协议都有HeartBeat机制,通常是客户端每隔一小段时间向服务器发送一个数据包,通知服务器自己仍然在线,并传输一些可能必要的数据.使用心跳包的典型协议是IM,比如QQ/MSN/飞信等协议. ...

  9. working with fitnesse wiki pages

    fitnesse提供一个简单易用的wiki创建一个web页面用于测试.测试页面有一个button,允许所有的测试在这个页面运行,因此任何人在任何时间都可以去这个页面点击这个按钮,查看测试是否通过.fi ...

  10. C#模拟Http与Https请求框架实例

    using System.Text; using System.Net; using System.IO; using System.Text.RegularExpressions; using Sy ...