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. Linux基础上

    文件操作                                                                                                 ...

  2. ssh 端口转发实践

    A: 172.28.92.114 本地主机B: 172.28.92.117 中间主机C: 172.28.92.118 目的主机 (这里名字叫目的主机更合适,原先把这里叫成远程主机,导致我一直认为远程端 ...

  3. [UE4]Grabbable接口

    首先,面向接口编程和面向对象编程并不是平级的,它并不是比面向对象编程更先进的一种独立的编程思想,而是附属于面向对象思想体系,属于其一部分.或者说,它是面向对象编程体系中的思想精髓之一. 接口是一组规则 ...

  4. WPF 引用 ttf文件

    1.在 http://www.iconfont.cn/ 下载图标,将图标加入购物车中,统一下载 2.下载到本地,解压后文件夹如下图.打开 demo_unicode.html 可以查看下载的图标信息和引 ...

  5. 刘志梅2017710101152.《面向对象程序设计(java)》第十三周学习总结

    实验十三  图形界面事件处理技术 实验时间 2018-11-22 1.理论知识 (1)任何支持GUI的操作环境都要不断地监视按键或点击鼠标这样的事件. JAVA这样的面向对象语言,都将事件的相关的信息 ...

  6. redis-cli显示中文

    在启动redis-cli时在其后面加上--raw参数即可启动后 再显示就正常了

  7. eclipse注解模板,实实在在的

    1.可以引用其他codetemplate.xml 2.可以自己配置,很快,就几个主要的,个人建议这个,配一次妥妥的,以后就会了,也不用搭理模板了 3.按照公司要求 拿去https://www.jian ...

  8. IE8图片上传预览

    $("#smallImg").attr('style', "filter:progid:DXImageTransform.Microsoft.AlphaImageLoad ...

  9. 解决idea spring boot项目中target中没有同步更新最新目录文件及资源

    idea不像eclipse那样自动将新保存的文件或目录及其他资源更新到target目录中,必须在pom.xml中设置 <build> <resources> <resou ...

  10. mac更新node,npm版本

    最近开发发现node版本多低的提示,于是升级一下 # 清除node.js的cache: $ sudo npm cache clean -f # 安装 n 工具,专门用来管理node.js版本的工具 $ ...