POJ3159(KB4-K 差分约束)
Candies
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 33283 | Accepted: 9334 |
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 Mlines 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
Source
//2017-08-29
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack> using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f; int head[N], tot;
struct Edge{
int to, next, w;
}edge[M]; void init(){
tot = ;
memset(head, -, sizeof(head));
} void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} int n, m;
bool vis[N];
int dis[N], cnt[N];
int sk[N], top;//手写栈
bool spfa(int s, int n){
for(int i = ; i <= n; i++){
vis[i] = ;
cnt[i] = ;
dis[i] = INF;
}
vis[s] = ;
dis[s] = ;
cnt[s] = ;
top = ;
sk[top++] = s;
while(top){
int u = sk[--top];
vis[u] = ;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].w){
dis[v] = dis[u] + edge[i].w;
if(!vis[v]){
vis[v] = ;
sk[top++] = v;
if(++cnt[v] > n)return false;
}
}
}
}
return true;
} int main()
{
//freopen("inputK.txt", "r", stdin);
while(scanf("%d%d", &n, &m)!=EOF){
init();
int u, v, w;
while(m--){
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
}
spfa(, n);
printf("%d\n", dis[n]);
} return ;
}
POJ3159(KB4-K 差分约束)的更多相关文章
- POJ3159:Candies(差分约束)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 39666 Accepted: 11168 题目链接:h ...
- K - Candies(最短路+差分约束)
题目大意:给N个小屁孩分糖果,每个小屁孩都有一个期望,比如A最多比B多C个,再多了就不行了,会打架的,求N最多比1多几块糖 分析:就是求一个极小极大值...试试看 这里需要用到一个查分约束的东西 下面 ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- 【BZOJ3436】小K的农场(差分约束)
[BZOJ3436]小K的农场(差分约束) 题面 由于BZOJ巨慢无比,使用洛谷美滋滋 题解 傻逼差分约束题, 您要是不知道什么是差分约束 您就可以按下\(Ctrl+W\)了 #include< ...
- BZOJ_3436_小K的农场_差分约束
BZOJ_3436_小K的农场_差分约束 题意: 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得 一些含糊的信息(共m个),以下列三种形式描述 ...
- P1993 小K的农场 && 差分约束
首先第一篇讨论的是差分约束系统解的存在 差分约束系统是有 \(n\) 个变量及 \(m\) 个(如 \(x_{i} - x_{j} \leq a_{k}\) )关系组成的系统 差分约束解的求解可以转化 ...
- 【10.9校内练习赛】【搜索】【2-sat】【树链剖分】【A_star k短路】【差分约束+判负环】
在洛谷上复制的题目! P3154 [CQOI2009]循环赛 题目描述 n队伍比赛,每两支队伍比赛一次,平1胜3负0. 给出队伍的最终得分,求多少种可能的分数表. 输入输出格式 输入格式: 第一行包含 ...
- 【BZOJ3436】小K的农场 差分约束
[BZOJ3436]小K的农场 Description 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了, ...
- 小K的农场(差分约束,spfa)
题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b至少多种植了c个单位的作 ...
- [bzoj3436]小K的农场_差分约束
小K的农场 bzoj-3436 题目大意:给定n个点,每个节点有一个未知权值.现在有m个限制条件,形如:点i比点j至少大c,点i比点j至多大c或点i和点j相等.问是否可以通过给所有点赋值满足所有限制条 ...
随机推荐
- Elasticsearch重要文章之四:监控每个节点(ThreadPool部分)
http://zhaoyanblog.com/archives/754.html ThreadPool部分 Elasticsearch 内部使用了线程池,通过这些线程池之间的合作完成工作,在需要时传递 ...
- Linux上安装java JDK
yum方式 1.查看yum中的各个版本 yum -y list java* 2.选择一个版本安装(如1.7) yum -y install java-1.7.0-openjdk* 3.安装完成后可查看 ...
- flex定位下overflow失效的问题研究
概述 这是我在写移动端页面遇到的问题及解决方法,记录下来供以后开发时参考,相信对其他人也有用. 问题 之前写移动端页面,有一个顶条是导航条,需要固定在页面顶部,并且里面的元素需要可以左右滚动. 但是当 ...
- CSS3盒子模型(上)
CSS的盒子模型分为三个大模块: 盒子模型 . 浮动 . 定位,其余的都是细节.要求这三部分,只要是学前端的无论如何也要学的非常精通. 所谓盒子模型就是把HTML页面中的元素看作是一个矩形的盒子,也就 ...
- [LeetCode] 两数相加
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- webstorm无法显示左边文件夹目录的解决方法
webstorm无法显示左边文件夹目录的解决方法 方法一 view-->Tool Windows-->Project 就可以显示或者关闭 方法二 1.删除webstorm的配置文件夹 2. ...
- [Umbraco] DocumentType设计指南
1. 命名规则 1.1. 文档类型(DocumentType)命名规则 图 1. Document Type命名示例 名称(Name) 采用帕斯卡命名法 如:TextPage 别名(Alias) ...
- HuLu机器学习问题与解答系列(1-8)
声明:本系列文章转载自微信公众号HULU,本人只是搬运工,仅供学习,如有不妥,后续告知删除. 嗨,欢迎回来,希望你能保持定期回顾的好习惯噢!下面是Hulu机器学习问题与解答系列的前8篇内容,点击主题名 ...
- python多线程-Semaphore(信号对象)
Semaphore(value=1) Semaphore对象内部管理一个计数器,该计数器由每个acquire()调用递减,并由每个release()调用递增.计数器永远不会低于零,当acquire() ...
- (转)python类:magic魔术方法
原文:https://blog.csdn.net/pipisorry/article/details/50708812 版权声明:本文为博主皮皮http://blog.csdn.net/pipisor ...