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 ...
随机推荐
- HTTP协议详解(经典)
转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HTTP是一个属于应用层的面向对象的 ...
- Java问题解读系列之String相关---String类的常用方法?
今天的题目是:String类的常用方法? 首先,我们在eclipse中定义一个字符串,然后使用alt+/就会出现String类的所有方法,如下图所示: 下面我就挑选一些常用的方法进行介绍: 首先定义两 ...
- JavaScript类型转换规则
注:JavaScript相对于其他强类型(如c,c++,java,定义变量的同时申明变量类型)语言来说,是弱类型和松散型(在定义的时候不需要申明变量类型,在使用的时候才赋值给她什么类型就是什么类型.所 ...
- 实体类No default constructor found 找不到默认构造函数;
root cause org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [c ...
- java 字符串拼接
package com.fh.controller.pacm.checkbill; import com.google.common.base.Joiner; /** * 字符串拼接 * * @aut ...
- 【洛谷】P1427 小鱼的猜数游戏
P1427 小鱼的数字游戏 题目描述 小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字(长度不一定,以0结束,最多不超过100个,数字不超过2^32-1),记住了然后反着念出来(表示结束的数字0 ...
- 洛谷P1573 栈的操作 [2017年6月计划 数论11]
P1573 栈的操作 题目描述 现在有四个栈,其中前三个为空,第四个栈从栈顶到栈底分别为1,2,3,…,n.每一个栈只支持一种操作:弹出并 压入.它指的是把其中一个栈A的栈顶元素x弹出,并马上压入任意 ...
- openpyxl 模块的使用
参考博客:https://www.cnblogs.com/anpengapple/p/6399304.html?utm_source=itdadao&utm_medium=referral 在 ...
- CF981H K Paths
CF981H K Paths 题解 一道不错的分治ntt题目 题目稍微转化一下,就是所有k条链的存在交,并且交的部分都被覆盖k次 所以一定是两个点,之间路径选择k次,然后端点两开花 f[x]表示x子树 ...
- Ubuntu小知识:更改主机名
Linux主机名是在安装Linux操作系统的过程中设定的,并作为网络中的某一台主机的唯一标志,但是在安装好Linux系统后,如果想修改主机名,该怎么办呢?本文介绍基于Ubuntu Desktop 9. ...