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] ...
随机推荐
- ConcurrentHashMap1.7源码分析
参考:https://www.cnblogs.com/liuyun1995/p/8631264.html HashMap不是线程安全的,其所有的方法都未同步,虽然可以使用Collections的syn ...
- STM32 NVIC中断优先级分组说明
STM32F103系列上面,又只有60个可屏蔽中断(在107系列才有68个) 中断管理方法 首先,对STM32中断进行分组,组0~4.同时,对每个中断设置一个抢占优先级和一个响应优先级值. 分组配置是 ...
- 【HDOJ6578】Blank(DP)
题意:一个长为n的序列,每个位置上的值是0,1,2,3中的一个,有m个限制条件,限制位置[l[i],r[i]]中不同的数值有x[i]个,问方案数MOD 998244353 n<=100,m< ...
- url 上回调函数(JSONP原理)
1.JSONP原理:就是跨域的 js程序(get请求对应url,获取到文本数据,在script标签中,就是按照 js 程序执行,)执行时 调用 当前程序中写好的函数,并且把跨域的数据(即参数),传 ...
- window 任务管理器
用的是win10 系统,一般window都差不多. 1.查看进程: 2.查看端口:性能 --> 打开资源资源监视器 --> 网络 --> 侦听端口 3.查看磁盘活动(查看文件被哪个进 ...
- element upload上传前对文件专门bs64上传
<!-- 文件上传 --> <template> <section class="file-upload"> <p class=" ...
- Extjs的一些基础使用!
一.获取元素(Getting Elements) 1. Ext.get() var el = Ext.getCmp('id');//获取元素,等同于document.getElementById('i ...
- python中对列表元素大小排序(冒泡排序法和选择排序法)
前言:排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列.本文主要讲述python中经常用的两种排序算法,选择排序法 ...
- Docker容器数据卷-Volume详解
Docker中的数据可以存储在类似于虚拟机磁盘的介质中,在Docker中称为数据卷(Data Volume).数据卷可以用来存储Docker应用的数据,也可以用来在Docker容器间进行数据共享.数据 ...
- 用 Flask 来写个轻博客 (33) — 使用 Flask-RESTful 来构建 RESTful API 之二
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 构建 RESTful Flask API 定义资源路由 格式 ...