Candies

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

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 AB 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.

Source

 
本题大意:n个孩子分糖果,给出m个a,b,c意为b比a分的糖果不能多于c个,即t[ b ] - t[ a ] <= c。很明显是差分约束了,可以转为单源最短路进行求解。
我们可以根据上面的约束方程推出松弛方程为if(dist[b] > dist[a] + edge[i].w) {dist[b] = dist[a] + edge[i].w;}。
 
这题应该是数据问题吧,至今不太明白为什么queue会TLE,而栈可以过...
 
参考代码:
 #include <cstdio>
#include <cstring>
#include <stack>
using namespace std; const int maxn = , maxe = , INF = 0x3f3f3f3f;
struct node {
int to, w, next;
}edge[maxe];
int num, head[maxn], dist[maxn];
bool vis[maxn]; void addedge(int u, int v, int cost) {
edge[num].to = v;
edge[num].w = cost;
edge[num].next = head[u];
head[u] = num ++;
} void spfa(int start, int n) {
stack <int> Q;
for(int v = ; v <= n; v ++) {
if(v == start) {
Q.push(v);
vis[v] = true;
dist[v] = ;
}
else {
vis[v] = false;
dist[v] = INF;
}
}
while(!Q.empty()) {
int u = Q.top(); Q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if(dist[v] > dist[u] + edge[i].w) {
dist[v] = dist[u] + edge[i].w;
if(!vis[v]) {
vis[v] = true;
Q.push(v);
}
}
}
}
} int main () {
int n, m, a, b, cost;
while(~scanf("%d %d", &n, &m)) {
num = ;
memset(head, -, sizeof head);
for(int i = ; i <= m; i ++) {
scanf("%d %d %d", &a, &b, &cost);
addedge(a, b, cost);
}
spfa(, n);
printf("%d\n", dist[n]);
}
return ;
}

或者用数组模拟栈也可以:

 #include <cstdio>
#include <cstring>
using namespace std; const int maxn = , maxe = , INF = 0x3f3f3f3f;
struct node {
int to, w, next;
}edge[maxe];
int num, head[maxn], dist[maxn];
int Q[maxn];
bool vis[maxn]; void addedge(int u, int v, int cost) {
edge[num].to = v;
edge[num].w = cost;
edge[num].next = head[u];
head[u] = num ++;
} void spfa(int start, int n) {
int top = ;
for(int v = ; v <= n; v ++) {
if(v == start) {
Q[top ++] = v;
vis[v] = true;
dist[v] = ;
}
else {
vis[v] = false;
dist[v] = INF;
}
}
while(top != ) {
int u = Q[-- top];
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if(dist[v] > dist[u] + edge[i].w) {
dist[v] = dist[u] + edge[i].w;
if(!vis[v]) {
vis[v] = true;
Q[top ++] = v;
}
}
}
}
} int main () {
int n, m, a, b, cost;
while(~scanf("%d %d", &n, &m)) {
num = ;
memset(head, -, sizeof head);
for(int i = ; i <= m; i ++) {
scanf("%d %d %d", &a, &b, &cost);
addedge(a, b, cost);
}
spfa(, n);
printf("%d\n", dist[n]);
}
return ;
}

POJ-3159.Candies.(差分约束 + Spfa)的更多相关文章

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

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

  2. 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 ...

  3. poj 3159 Candies 差分约束

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

  4. POJ——3159Candies(差分约束SPFA+前向星+各种优化)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 28071   Accepted: 7751 Descrip ...

  5. POJ3159 Candies —— 差分约束 spfa

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

  6. POJ——1364King(差分约束SPFA判负环+前向星)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11946   Accepted: 4365 Description ...

  7. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  8. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  9. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

随机推荐

  1. rocketMQ(二 )Centos7 集群

    rocketMQ集群: 在运用中流程一般 是在程序中使用代码编辑生产者,将所需要的消息发送到rocketmq中,然后另一个程序编辑消费者从rocketmq里面获取消息.rocketmq集群 需要对na ...

  2. javaAgent介绍

    JavaAgent(转载) http://www.cnblogs.com/diyunpeng/archive/2011/05/26/2057932.html 一文带你了解Java Agent http ...

  3. 为什么我说IPFS社区从卖矿机开始,就是错的

    要回答这个问题,首先要了解去中心化存储项目和传统的区块链项目有什么区别.其中去中心化存储项目包括IPFS,基于IPFS的FileCoin.PPIO.Storj等. 传统区块链项目没有供需问题 首先以比 ...

  4. 深度学习(一)——CNN算法流程

    深度学习(一)——CNN(卷积神经网络)算法流程 参考:http://dataunion.org/11692.html 0 引言 20世纪60年代,Hubel和Wiesel在研究猫脑皮层中用于局部敏感 ...

  5. docker 搭建 hustoj

    docker 搭建 hustoj hustoj 是个GPL开源的OJ,其提供了docker形式的安装方式. 为执行方便,选择使用aliyun提供的docker镜像来加速安装. 拉取镜像 docker ...

  6. 自学大数据(hadoop)第一天

    熟悉linux系统 1.安装linux系统-ubuntu 官网链接:https://www.ubuntu.com/download 下载ubuntu desktop 即可,拖拽到VMvare里即可安装 ...

  7. 搭建zookeeper+kafka集群

      搭建zookeeper+kafka集群 一.环境及准备 集群环境:   软件版本: 部署前操作: 关闭防火墙,关闭selinux(生产环境按需关闭或打开) 同步服务器时间,选择公网ntpd服务器或 ...

  8. JAVA WEB项目中开启流量控制Filter

    Flow Control:控流的概念 主要是用来限定server所能承载的最大(高并发)流量峰值,以免在峰值是Server过载而宕机,对于WEB系统而言 通常是分布式部署,如果请求并发量很大,会导致整 ...

  9. IDE 热部署配置

    从eclipse切换到IDE,遇到应用不能热部署问题,解决如下 1.tomcat 中server配置下面三点需要注意 2.tomcat的deployment 中 需要选择war exploded而不是 ...

  10. eShopOnContainers 看微服务③:Identity Service

    引言 通常,服务所公开的资源和 API 必须仅限受信任的特定用户和客户端访问.那进行 API 级别信任决策的第一步就是身份认证——确定用户身份是否可靠. 在微服务场景中,身份认证通常统一处理.一般有两 ...