POJ 3159 Candies(差分约束+SPAF)

题意:

给n个小朋友分发糖果,但小朋友们之间有嫉妒心。接下来m行,每行三个数,分别表示小朋友A希望B得到的糖果不能比他多x个。要求你计算在满足所有小朋友的条件的情况下最多需要准备多少颗糖。

题目:

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.

分析:

1.这道题目是典型的差分约束系统,小于等于是最短路 大于等于是最长路,核心在于:xj-xi<=bk,会发现它类似最短路中的三角不等式d[v]<=d[u]+w[u,v],即d[v]-d[u]<=w[u,v],即从u指向v建边,num[B]<=num[A]+x。求最大值的话就是跑1-n的最短路,对应求最小值就是跑1-n的最长路。此题用队列的话会T掉,可以改成栈或者想办法优化。
2.关于差分约束选择队列还是堆栈:当存在回路,SPFA的队列实现会超时,堆栈实现可以,堆栈实现SPFA(有时候堆栈确实比较快)
都大三的老阿姨了,差分约束还不熟练,加油吧,查缺补漏中。。。。

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int M=3e4+10;
const int N=15e4+10;
int n,m,k,a,b,c;
int vis[M]/**在队列标志*/;
int head[M]/**每个结点的头指针(或者编号)*/;
int q[M]/**堆栈*/,dis[M];
struct node
{int b;int c;int next;
}edge[N];void add(int a,int b,int c)//加边
{edge[k].b=b;edge[k].c=c;edge[k].next=head[a];head[a]=k++;
}
void SPFA(int start,int n)
{int top=0;for(int i=1;i<=n;i++)//初始化{if(i==start){q[top++]=i;//入栈vis[i]=true;dis[i]=0;}else{vis[i]=false;dis[i]=inf;}}while(top!=0){int a=q[--top];vis[a]=false;for(int i=head[a];i!=-1;i=edge[i].next){int b=edge[i].b;if(dis[b]>dis[a]+edge[i].c){dis[b]=dis[a]+edge[i].c;if(!vis[b]){vis[b]=true;q[top++]=b;}}}}
}
int main()
{while(~scanf("%d%d",&n,&m)){k=0;//加边计数,这个不要忘memset(head,-1,sizeof(head));for(int i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);add(a,b,c);}SPFA(1,n);//printf("*****\n");printf("%d\n",dis[n]);//此处求n比1最多多多少糖果}return 0;
}
/**给n个人派糖果,给出m组数据,每组数据包含A、B、c  三个数,
意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。
最后求n 比 1 最多多多少糖果。*/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309644.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

掌握了Docker Layer Caching才敢自称精通Dockerfile

长话短说&#xff1a;本次原创将向您展示在Docker中使用Layer Cache以加快镜像构建。“这个话题的初衷在于&#xff1a;应用打包过程是很慢的(下载并安装框架&第三方依赖包、生成assets)&#xff0c;这个过程在Docker中也不能避免。About Layer Caching in DockerDocker使…

Go unsafe Pointer

Go unsafe Pointer Go被设计为一种强类型的静态语言&#xff0c;强类型意味着类型一旦确定就无法更改&#xff0c;静态意味着类型检查在运行前就做了。 指针类型转换 为了安全考虑&#xff0c;两个不同类型的指针不能相互转换&#xff0c;例如&#xff1a; package mainfun…

How Many Answers Are Wrong HDU - 3038(带权并查集)

题意&#xff1a; TT写一个数列&#xff0c;现在TT会选择一个区间&#xff0c;然后让FF计算这个区间里面所有数的和&#xff0c;FF准备捉弄一下TT&#xff0c;有时候她会故意计算出来一个错的答案&#xff0c;当然TT也比较聪明&#xff0c;他会发现这个答案跟以前的答案会有冲…

ASP.NET Core on K8s学习之旅(14)Ingress灰度发布

【云原生】| 作者/Edison Zhou这是恰童鞋骚年的第236篇原创文章上一篇介绍了Ingress的基本概念和Nginx Ingress的基本配置和使用&#xff0c;然后我还录了一个快速分享小视频介绍了一下蓝绿发布和灰度发布策略的基本概念&#xff0c;本篇介绍一下如何实战使用Nginx Ingress实现…

[汇编语言]实验:应用更灵活的寻址方式来定位内存地址

实验内容: &#xff08;1&#xff09;将datasg段中每个单词的头一个字母改成大写字母。 datasg段中的数据为: &#xff08;2&#xff09; 将datasg段中每个单词的字母改成大写字母。 datasg段中的数据为: ibm dec dos vax …

Redundant Paths POJ - 3177(tarjan+边双连通分量)

题意&#xff1a; 有n个牧场&#xff0c;要求从一个牧场到另一个牧场&#xff0c;要求至少要有2条独立的路可以走。现已有m条路&#xff0c;求至少要新建多少条路&#xff0c;使得任何两个牧场之间至少有两条独立的路。两条独立的路是指&#xff1a;没有公共边的路&#xff0c…

Slice的本质

Slice的本质 我们先看下面的代码&#xff0c;看看它的输出是什么&#xff1a; package mainimport "fmt"type Slice []intfunc (A Slice) Append(value int) {A append(A, value) } func main() {mSlice : make(Slice, 10, 20)mSlice.Append(5)fmt.Println(mSlice…

你需要了解操作系统发展历程

本文我们大概回顾计算机操作系统发展历程&#xff0c;这里不会记录关于操作系统的完整历史记录&#xff0c;只是记录那些里程碑事件&#xff0c;看看各位接触计算机时&#xff0c;操作系统发展正处于哪个年代起初没有操作系统&#xff0c;没有编程语言或编译器&#xff0c;甚至…

[汇编语言]实验:更灵活的寻址方式 -应用si和di

实验内容: &#xff08;1&#xff09; 用寄存器SI和DI实现将字符串‘welcome to masm!’ 复制到它后面的数据区中。 &#xff08;2&#xff09; 用[bx(si或di)idata]的方式&#xff0c;来使程序变得简洁。 &#xff08;1&#xff09; 代码如下: assume ds:datasg,cs:code…

http.ListenAndServe()到底做了什么?

参考&#xff1a;https://studygolang.com/articles/25849?frsidebar ​ http://blog.csdn.net/gophers 实现一个最简短的hello world服务器 package mainimport "net/http"func main() {http.HandleFunc("/", func(w http.ResponseWriter, r *http.Reque…

Strongly connected HDU - 4635(tarjan+强连通分量)

题意&#xff1a; 给一个简单有向图&#xff0c;让你加最多的边&#xff0c;使他还是一个简单有向图。 题目&#xff1a; Give a simple directed graph with N nodes and M edges. Please tell me the maximum number of the edges you can add that the graph is still a …

C++编程基础题训练

1.编写一个c风格的程序&#xff0c;用动态分配空间的方法计算Fibonacci数列的前20项并存储到动态分配的空间中 1.代码如下 #include <iostream> using namespace std;int main() {int *a new int[25];a[0] 0;a[1] 1;for (int i 2; i < 20; i){a[i] a[i - 1] a…

基于 abp vNext 和 .NET Core 开发博客项目 - 使用Redis缓存数据

上一篇文章完成了项目的全局异常处理和日志记录。在日志记录中使用的静态方法有人指出写法不是很优雅&#xff0c;遂优化一下上一篇中日志记录的方法&#xff0c;具体操作如下&#xff1a;在.ToolKits层中新建扩展方法Log4NetExtensions.cs。//Log4NetExtensions.cs using log4…

G - 水陆距离 HihoCoder - 1478(广搜+队列先进先出性质)

题目&#xff1a; 给定一个N x M的01矩阵&#xff0c;其中1表示陆地&#xff0c;0表示水域。对于每一个位置&#xff0c;求出它距离最近的水域的距离是多少。 矩阵中每个位置与它上下左右相邻的格子距离为1。 Input 第一行包含两个整数&#xff0c;N和M。 以下N行每行M个0…

第一讲 工作区和GOPATH

此为 《极客时间&Go语言核心36讲》 个人笔记&#xff0c;具体课程详见极客时间官网。 Table of Contents generated with DocToc 第一讲 工作区和GOPATH 1. 环境变量配置2. 配置GOPATH的意义 2.1 Go语言源码的组织方式2.2 源码安装后的结果&#xff08;归档文件、可执行文…

C++重载运算符小结与注意点

重载运算符需注意: 1.重载运算符时容易忘记写返回值。 2.重载赋值运算符时&#xff0c;记得加const&#xff0c;因为赋值操作必须是固定的右值。 3重载时&#xff0c;写在类中的只能有一个参数(实际有两个参数&#xff0c;另外一个是this指针&#xff0c;我们看不见而已)&am…

开发大会上,前微软CEO放出的狠话!.NET开发随时起飞,你准备好了吗?

“开发者&#xff0c;开发者&#xff0c;开发者&#xff0c;开发者”&#xff0c;微软前任CEO史蒂夫鲍尔默(Steve Ballmer)用这种略带疯狂、又唱又跳的方式表达他对开发者的热爱。不夸张的说&#xff0c;相比二十年前那个如日中天的巨无霸微软&#xff0c;现在的微软比以往任何…

Balanced Lineup POJ - 3264(线段树模板+查询比大小+建树)

题意&#xff1a; 给你n个数&#xff0c;然后问一段区间的最大的差值是多少。 题目&#xff1a; For the daily milking, Farmer John’s N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbe…

第二讲 命令源码文件

此为 《极客时间&Go语言核心36讲》 个人笔记&#xff0c;具体课程详见极客时间官网。 Table of Contents generated with DocToc 第二讲 命令源码文件 1. 什么是命令源码文件&#xff1f;2. 命令参数的接收和解析 2.1 命令源码文件怎么接收参数?2.2 怎样在运行源代码文件…