POJ-3159_Candies
Candies
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.
题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。最后求小孩中的最大差值是多少。
题解:这是一题典型的差分约束题,可以把糖果看成距离,然后就是求最短路的问题了。
由 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术,即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。
坑点就在于SPFA队列会超时,所以采取堆栈。
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 30050;
const int INF = 1e9+7;
int q[maxn],head[maxn],f[maxn],dis[maxn];
int num,n;
struct node
{
int to,next,w;
}edge[150050];
void add(int v,int u,int w)
{
edge[num].to = u;
edge[num].next = head[v];
edge[num].w = w;
head[v] = num++;
}
void SPFA()
{
int i,top;
for(i=1;i<=n;i++)
{
f[i] = 0;
dis[i] = INF;
}
top = 0;
dis[1] = 0;
f[1] = 1;
q[top++] = 1;
while(top)
{
int u,v,w;
u = q[top-1];
f[u] = 0;
top--;
for(i=head[u];i!=-1;i=edge[i].next)
{
v = edge[i].to;
w = edge[i].w;
if(dis[u]+w<dis[v])
{
dis[v] = dis[u] + w;
if(!f[v])
{
f[v] = 1;
q[top++] = v;
}
}
}
}
cout<<dis[n]<<endl;
}
int main()
{
int m,i,a,b,c;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
head[i] = -1;
num = 0;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
SPFA();
return 0;
}
POJ-3159_Candies的更多相关文章
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
- poj 2352 Stars 数星星 详解
题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
随机推荐
- LUOGU P2441 角色属性树
题目描述 绪萌同人社是一个有趣的组织,该组织结构是一个树形结构.有一个社长,直接下属一些副社长.每个副社长又直接下属一些部长--. 每个成员都有一个萌点的属性,萌点属性是由一些质数的萌元素乘积构成(例 ...
- 移植thinkPHP的dump()函数
由于公司的开发机没有开xdebug拓展,导致var_dump()在浏览器上看很不方便. 因此,加入从thinkphp搬过来的dump()函数. function dump($var, $echo=tr ...
- Codeforces Round #573 (Div. 2)
A:Tokitsukaze and Enhancement 当时看错条件了..以为A>C>B>D.就胡写了判断条件. #include<bits/stdc++.h> us ...
- Ubuntu下安装Mongo方法
场景:Ubuntu14下安装mongo,建议不要使用apt-get install 的命令安装,因为版本比较老 1.deb下载地址(可以自行选择OS,版本,server或tool或shell)http ...
- C++学习笔记----4.4 继承情况下的类作用域嵌套
引言: 在继承情况下,派生类的作用域嵌套在基类作用域中:如果不能在派生类作用域中确定名字,就在外围基类作用域中查找该名字的定义. 正是这种类作用域的层次嵌套使我们能够直接访问基类的成员,就好像这些成员 ...
- Direct2D 第5篇 绘制图像
原文:Direct2D 第5篇 绘制图像 我加载的图像是一张透明底PNG图像,背景使用渐变的绿色画刷 #include <windows.h> #include <d2d1.h> ...
- vue+axios 对restful 请求封装
礼拜天来公司整理项目,项目是最近开始重构的,里面的各种http请求接口是restful结构的(为了提升项目的比格),整理一下笔记 [restful介绍][1]博主讲的很详细 技术栈: vue + vu ...
- JQuery--jQquery控制CSS样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 封装函数通过输入(元素,属性,目标值)改变div样式
## 假设一个div样式如下```html<!DOCTYPE html><html lang="en"> <head> <meta cha ...
- SQLServer数据库(二)
数据库设计:就是将数据库中的数据库实体及这些数据库实体之间的关系,进行规划和结构化的过程. 项目开发过程: 需求分析 概要设计 详细设计 代码编写 运行测试 打包发行 数据库的系统分析基本步骤:收集信 ...