题面

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.

飞鼠的幼儿园班上经常发糖果,全班Infinite个糖果由飞鼠分配给包括飞鼠(和史努比)在内的n个孩子。表现乖的人得到的糖果多很正常,但其中可能 有小孩A 觉得 无论自己的糖果多么少,另一个小孩B都不能得到 比自己多 超过c个的糖果。飞鼠不敢让同学们不满意,因为他们会告诉老师。

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 AB and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.

多组数据,输入到文件末尾

每组数据开头n和m,(m表示对糖果数的m对要求,飞鼠标号为n,史努比标号为1)

下面m行每行A、B、C,表示糖果数要满足“B的糖果数 ≤ A的糖果数 + C”

Output

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

每组数据一行答案,保证有解。所有数都在int范围内。

题解

分析一下这道题的条件,设c[i]表示 i 的糖果数,发现“c[B] <= c[A] + C” 相似于 “dis[B] <= dis[A] + weight”,后者是一张图中每个点到原点最短路满足的条件,而且,每个点的最短路都是满足上述条件的最大值。于是,把A向B连一条边权为C的边,再从1到n跑一遍最短路就完了。(建议别用SPFA)

CODE(dij)

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#define MAXN 30005
#define MAXM 150005
#define LL long long
#define ENDL putchar('\n')
using namespace std;
LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-')f = -f;s = getchar();}
while(s >= '0' && s <= '9') {x = x*10+(s-'0');s = getchar();}
return f*x;
}
int n,m,i,j,s,o,k;
struct it{
int v,w;
it(){v = w = 0;}
it(int V,int W){v = V;w = W;}
};
vector<it> g[MAXN];
LL dp[MAXN];
int bing(int a,int b) {return dp[a] < dp[b] ? a:b;}
int tre[MAXN<<2],M;
void maketree(int n) {M = 1;while(M < n+2)M <<= 1;}
void addtree(int x,int y) {
int s = M + x;tre[s] = y;s >>= 1;
while(s) {tre[s] = bing(tre[s<<1],tre[s<<1|1]);s >>= 1;}
}
int findall() {return tre[1];}
int main() {
while(scanf("%d%d",&n,&m) == 2) {
for(int i = 1;i <= m;i ++) {
s = read();o = read();k = read();
g[s].push_back(it(o,k));
}
memset(tre,0,sizeof(tre));
maketree(n);
for(int i = 0;i <= n;i ++) dp[i] = 1e18;
dp[1] = 0;
addtree(1,1);
for(int i = 1;i <= n;i ++) {
int t = findall();
if(t == 0) break;
for(int j = 0;j < g[t].size();j ++) {
if(dp[g[t][j].v] > dp[t] + g[t][j].w) {
dp[g[t][j].v] = dp[t] + g[t][j].w;
addtree(g[t][j].v,g[t][j].v);
}
}
addtree(t,0);
}
printf("%lld\n",dp[n]);
}
return 0;
}

OpenJ_Bailian - 3424 Candies (差分约束)的更多相关文章

  1. poj3159 Candies(差分约束,dij+heap)

    poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...

  2. POJ-3159.Candies.(差分约束 + Spfa)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 40407   Accepted: 11367 Descri ...

  3. POJ 3159 Candies 差分约束dij

    分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u ...

  4. [poj 3159]Candies[差分约束详解][朴素的考虑法]

    题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...

  5. [poj3159]Candies(差分约束+链式前向星dijkstra模板)

    题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...

  6. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  7. poj3159 Candies(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit ...

  8. POJ3159 Candies —— 差分约束 spfa

    题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submiss ...

  9. Candies(差分约束)

    http://poj.org/problem?id=3159 题意: flymouse是幼稚园班上的班长,一天老师给小朋友们买了一堆的糖果,由flymouse来分发,在班上,flymouse和snoo ...

随机推荐

  1. ExtJS 同行表单域对齐有误处理办法

    更新记录 2022年5月29日 第一次编辑.使用的ExtJS版本:ExtJS 7.4 问题 原本都是显示正常的表单域,比如这些文本框.选择框都是正常. 在用户进行操作,然后显示验证提示后,明显出现了问 ...

  2. markdown常用到的语法

    一.标题 后加文字,几个#代表几级标题,最高为6 ,标准语法一般在#后跟个空格再写文字. 二.分割线 三个或者三个以上的 - 或者 * 三.图片 格式: ![A](B "C") A ...

  3. kvm虚拟机在线扩容

    fdisk -l查看当前虚拟机磁盘容量 1. 镜像扩容 先操作镜像,给镜像增加2T容量: 关闭虚拟机back_log,然后再宿主机上给虚拟机扩容 qemu-img info /home/kvm/bac ...

  4. 嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】

    本文参考CSDBN:建国雄心 的博客,这里找不到该帖子,放一个类似的仅供参考https://blog.csdn.net/qiaojiongzeng6321/article/details/748572 ...

  5. label问题排查:打不开标注好的图像

    问题描述 之前标注好的文件,标注有bbox和若干points.选择Open Dir打开图像目录,选择Change Output Dir选择json文件所在目录.发现有些图片能正常显示标注后的状态.而有 ...

  6. Three.js系列: 在元宇宙看电影,享受 VR 视觉盛宴

    本文 gihtub 地址: https://github.com/hua1995116/Fly-Three.js 最近元宇宙的概念很火,并且受到疫情的影响,我们的出行总是受限,电影院也总是关门,但是在 ...

  7. led跑马灯多种方法(移位法,位拼接法,调用模块法,位移及位拼接语法,testbench的理解,源文件的存储路径,计数器的个数,调用模块的方式)

    跟着教程写了几种方法,才发现自己写的虽然能实现,但比较繁琐.教程有三种方法: 1.移位法,每次左移一位,相比我自己写的,优点是不用把每一种情况都写出来.但是需要考虑左移到最后一位时需要自己再写个赋值语 ...

  8. 基于cornerstone.js的dicom医学影像查看浏览功能

    最近由于项目需求,需要医学影像.dcm文件的预览功能,功能完成后,基于原生Demo做一个开源分享. 心急的小伙伴可以先看如下基于原生js的全部代码: 一.全部代码 <!DOCTYPE html& ...

  9. this关键字和构造方法

    构造方法: 构造方法的定义:构造方法是类的一个特殊成员,它会在类实例化对象的时候被自动调用 作用:可以在实例化对象的同时对这个对象的属性进行赋值 案例:Student student = new St ...

  10. spring boot实现不同生产环境下的文件配置

    配置不同生产环境 本文适用于开发环境下需要打包项目至生产环境,避免开发环境的配置文件泄露. 设置maven 作用:1. 手动调节运行时的不同环境 2. 打包时可以不会有其它环境的文件 注:每次换环境前 ...