POJ 3159 Candies(spfa、差分约束)
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
题意给你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、差分约束)的更多相关文章
- POJ 3159 Candies(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- POJ 3159 Candies(差分约束+最短路)题解
题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...
- POJ 3159 Candies 【差分约束+Dijkstra】
<题目链接> 题目大意: 给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c .最后求n 比 1 ...
- POJ 3159 Candies 还是差分约束(栈的SPFA)
http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...
- POJ 3159 Candies(差分约束)
http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...
- poj 3159 Candies (差分约束)
一个叫差分约束系统的东西.如果每个点定义一个顶标x(v),x(t)-x(s)将对应着s-t的最短路径. 比如说w+a≤b,那么可以画一条a到b的有向边,权值为w,同样地给出b+w2≤c,a+w3≤c. ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
随机推荐
- 聊聊Dubbo - Dubbo可扩展机制实战
1. Dubbo的扩展机制 在Dubbo的官网上,Dubbo描述自己是一个高性能的RPC框架.今天我想聊聊Dubbo的另一个很棒的特性, 就是它的可扩展性. 如同罗马不是一天建成的,任何系统都一定是从 ...
- luogu P3768 简单的数学题 杜教筛 + 欧拉反演 + 逆元
求 $\sum_{i=1}^{n}\sum_{j=1}^{n}ijgcd(i,j)$ 考虑欧拉反演: $\sum_{d|n}\varphi(d)=n$ $\Rightarrow \sum_{i ...
- 【Elasticsearch】清空指定index/type下的数据
1.postman请求接口 http://ip:端口/index/type/_delete_by_query?conflicts=proceed body为: { "query": ...
- Oracle数据库之触发器(一)
触发器trigger是数据库提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发.比如当对一个表进行操作(in ...
- CSS基础知识复习
1. CSS优先级 标签内部属性 style定义的CSS > 文档内定义的css > 引用外部CSS文件 2. CSS选择器类型 . 标签选择器 . 类选择器(使用.做标识) . ID选择 ...
- jQuery 问题收集
1.页面动态生成的dom元素,监听事件失效.需用事件代理进行监听. 对于动态绑定元素可以这样写 $(document).on('click', '.xxx', function() { // do s ...
- XScreenSaver强大的锁屏工具
source install: https://www.jwz.org/xscreensaver/ XScreenSaver Related articles DPMS Xresources ...
- CentOS 7在VMware 12中共享文件看不见的问题?
前言 由于rhel 7.2因为没有注册导致yum无法使用,包括自己配置本地源,这个命令在你没有注册都不能使用,每次使用rpm去装软件,自己去找缺少的依赖包,实在是麻烦.于是不如就换一个系统,CentO ...
- jenkins实现master变化时,才触发构建(过滤分支)
现状:现在是这样的,每个开发push时,都触发jenkins进行构建 期望:只有当代码被push到master时才进行构建 (根据使用的git平台)做这些配置需要先了解一些概念: (github) p ...
- [LeetCode] 627.交换性别
给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值.交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然). 要求只使用一个更新(Update)语句,并且没 ...