OpenJ_Bailian - 3424 Candies (差分约束)
题面
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 A, B 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 (差分约束)的更多相关文章
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- POJ-3159.Candies.(差分约束 + Spfa)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 40407 Accepted: 11367 Descri ...
- 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 ...
- [poj 3159]Candies[差分约束详解][朴素的考虑法]
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...
- [poj3159]Candies(差分约束+链式前向星dijkstra模板)
题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- poj3159 Candies(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Candies Time Limit: 1500MS Memory Limit ...
- POJ3159 Candies —— 差分约束 spfa
题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS Memory Limit: 131072K Total Submiss ...
- Candies(差分约束)
http://poj.org/problem?id=3159 题意: flymouse是幼稚园班上的班长,一天老师给小朋友们买了一堆的糖果,由flymouse来分发,在班上,flymouse和snoo ...
随机推荐
- 编程技巧│浏览器 Notification 桌面推送通知
目录 一.什么是 Notification 二.弹窗授权 三.弹窗使用 四.浏览器支持检测 五.授权回调 六.3秒后关闭弹窗 一.什么是 Notification Notification 是浏览器最 ...
- 高通sensor理解
.1.高通为什么引入adsp? 2.adsp sensor 是如何工作起来的? 3.adsp 和ap 是如何通信的? 4.adsp 架构组成 解答: 1.高通在msm8960之前sensor 是挂在p ...
- 适配抖音!三角面转换和3d模型体量减小,轻量化一键即可完成!
抖音3d特效,可谓是越来越火爆了,这个有着迪士尼画风的3D大眼,就刷屏了国内外用户的首页! 有人好奇这些特效究竟是怎么制作的?其实就是把3D模型调整适配到头部模型上,调整位置或者大小就可以制作出一个简 ...
- Vue.js CLI4 Vue.config.js标准配置 (最全注释)
前言: Vue.js CLI工具 不知不觉发展到了4.0时代,CLI给人最直白的感受是没有了build文件夹跟config文件夹,所有的配置都在Vue.config.js完成.那么该文件的配置至关重要 ...
- 数字格式化的 js 库
数字格式化的 js 库 Numeral.js,是一个用于格式化数字和处理数字的 js 库. Tip:目前 Star 有 9.2k,5年以前就没有在更新.其文档写得不很清晰,比如它提供了多语言,但如何切 ...
- Multiparty Cardinality Testing for Threshold Private Set-2021:解读
本文记录阅读该论文的笔记. 本文基于阈值加法同态加密方案提出了一个新的允许\(N\)方检查其输入集的交集是否大于\(n-t\)的IPSI方案,该协议的通信复杂度为\(O(Nt^2)\). 注意:\(N ...
- DTCC 干货分享:Real Time DaaS - 面向TP+AP业务的数据平台架构
2021年10月20日,Tapdata 创始人唐建法(TJ)受邀出席 DTCC 2021(中国数据库技术大会),并在企业数据中台设计与实践专场上,发表主旨演讲"Real Time Daa ...
- ESP分区重建,解决各种引导问题
电脑装了双系统,win7和win10,每次重启都进入不同系统,郁闷至极,索性把不常用的Win7盘格式化,但依旧解决不了问题.所以有了以下方法. 1.进PE删除ESP分区(先备份). 2.新建ESP分区 ...
- java 配置aop 写入无效
一个项目不同的Module 含有相同的路径以及文件,配置的AOP的expression吸入日志无效,要点击包查看当前包是否是本Module下面的,不然调用无效. 改为本Module就行了
- NOI / 1.2编程基础之变量定义、赋值及转换全题详解(5063字)
目录 01:整型数据类型存储空间大小 02:浮点型数据类型存储空间大小