POJ-3159 Candies( 差分约束 )
2016-07-13 14:04
489 查看
题目链接:http://poj.org/problem?id=3159
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
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个人分糖果,分糖果时部分学生AB之间存在一条规则,学生B所分得的糖果最多比学生A都C个,现问N号学生最多能比1号学生多分几个 差分约束 一道最基本的差分约束题,即约束为val[a] - val[b] <= k,求N最多比1大多少,可以转化为有向路edge[b][a] = k,然后求1大N的最短路问题 如果用spfa需要用stack,否则会超时,然后,慎用cin
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<cstdio> #include<queue> #include<stack> using namespace std; const int LENN = 30000 + 5; const int LENM = 150000 + 5; const int INF = 0x3f3f3f3f; struct Edge{ int to, next; int val; }edge[LENM]; int dis[LENN]; bool vis[LENN]; int h[LENN]; int n, m; void spfa_bfs(){ memset( vis, false, sizeof( vis ) ); memset( dis, INF, sizeof( dis ) ); dis[1] = 0; stack<int> Q; Q.push( 1 ); vis[1] = true; while( !Q.empty() ){ int x = Q.top(); Q.pop(); vis[x] = false; for( int k = h[x]; k != 0; k = edge[k].next ){ int y = edge[k].to; int val = edge[k].val; if( dis[x] + val < dis[y] ){ dis[y] = dis[x] + val; if( !vis[y] ){ Q.push( y ); vis[y] = true; } } } } } int main(){ //ios::sync_with_stdio( false ); cin >> n >> m; int beg, end, val; memset( h, 0, sizeof( h ) ); for( int i = 1; i <= m; i++ ){ scanf( "%d%d%d", &beg, &end, & val ); edge[i].to = end; edge[i].val = val; edge[i].next = h[beg]; h[beg] = i; } spfa_bfs(); cout << dis << endl; return 0; }
相关文章推荐
- SVM学习笔记
- uc/os-iii学习笔记-临界段
- 安卓 android 程序运行,禁止锁屏代码
- 不能为虚拟机xxxx电脑 打开一个新任务
- 解决vi命令不正常退出导致.swp文件无法删除问题的梦幻之旅
- Codeforces Round #271 (Div. 2) E. Pillars(线段树+DP)
- STL-优先级队列
- 【风宇冲】Unity3D教程宝典之Shader篇:特别讲 CG函数
- js 非构造函数的继承 object()方法
- Python之函数参数介绍
- canvas元素的基础知识
- 实现postfix支持mysql方法
- 深度神经网络实现图像理解的原理
- Spring Security在登录验证中增加额外数据(如验证码)
- 【风宇冲】Unity3D教程宝典之Shader篇:特别讲 常见问题解答
- android动画(一)Interpolator
- 源码编译安装LNMP
- Linux启动和关闭时自动执行的所有脚本文件
- Android绘图基础2--仪表制作
- 【风宇冲】Unity3D教程宝典之Shader篇:基础讲 基础知识