POJ 3159--Candies【基础差分规划】
2015-07-25 21:37
357 查看
Candies
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
Sample Output
5
粘的斌神的解题思路。
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 25002 | Accepted: 6782 |
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个人派糖果,给出m组数据,每组数据包含A,B,c 三个数, 意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。 最后求n 比 1 最多多多少糖果。 【解题思路】 这是一题典型的差分约束题。不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值, 我们得到 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术, 即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。 即是满足题中的条件dis[B]-dis[A]<=w(A,B),由于要使dis[B] 最大, 所以这题可以转化为最短路来求。 这题如果用SPFA 算法的话,则需要注意不能用spfa+queue 来求,会TLE ,而是用 spfa + stack
粘的斌神的解题思路。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define maxn 30010 #define INF 0x3f3f3f3f using namespace std; struct node { int u, v, w, next; }; int n,m; node edge[maxn * 4]; int head[maxn], cnt; int q[maxn]; int dist[maxn]; bool vis[maxn]; void init(){ memset(head, -1, sizeof(head)); cnt = 0; } void add(int u, int v, int w){ edge[cnt] = {u, v, w, head[u]}; head[u] = cnt++; } void getmap(){ // for(int i = 0 ; i <= n; ++i) // add(i, 0, 0); while(m--){ int a, b ,c; scanf("%d%d%d", &a, &b, &c); add(a, b, c); } } int SPFA(){ int k = 0; for(int i = 1; i <= n; ++i){ vis[i] = 0; dist[i] = INF; } dist[1] = 0; vis[1] = 1; q[k++] = 1; while(k){ int u = q[--k]; vis[u] = 0; for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].v; int w = edge[i].w; if(dist[v] > dist[u] + w){ dist[v] = dist[u] + w; if(!vis[v]){ vis[v] = 1; q[k++] = v; } } } } printf("%d\n", dist ); } int main (){ while(scanf("%d%d", &n, &m) != EOF){ init(); getmap(); SPFA(); } return 0; }
相关文章推荐
- VIM使用(三)
- 怎样扫描图片how to scan images
- 统计字符串第一个空字符前的字符长度
- JavaScript读书笔记---基本数据类型string与基本包装类型String
- QtScript, QML, Quick1, Quick2, Declarative 之间的关系
- 数据库 基础学习8— 横标与纵表转换
- excel模板增加扩展辅助数据的配置
- 南阳理工 2 括号配对问题
- 剑指 | 7-利用两个栈构建一个队列
- 【LeetCode】240. Search a 2D Matrix II
- Ubuntu下的PHP开发环境架设
- STMCUBE——8频率计
- spring-boot 添加JSP页面功能
- [UVA10410]Tree Reconstruction[BFS、DFS序建树]
- int main(int argc, char*argv[])
- BZOJ 1045 糖果传递
- Java框架篇---Mybatis 构建SqlSessionFactory
- C++ 打印矩阵
- MATLAB图像函数 块和邻域的处理
- mysql查询练习