POJ 3159 Candies 差分约束
2015-07-15 20:38
330 查看
链接:http://poj.org/problem?id=3159
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 overc 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
Hint
32-bit signed integer type is capable of doing all arithmetic.
Source
POJ Monthly--2006.12.31, Sempr
题意:
班长分糖
B的糖不能比A多C个以上
班长是n号,1号是史努比。
问班长在符合条件的情况下,最多比史努比多多少糖。
做法:(字母表示该人的糖数)
容易见得, B-A<=C
求n-(1) <=?
很明显的差分约束。
A->B建边,然后计算(1)到n的最短路就好了。
Candies
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 24852 | Accepted: 6737 |
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 overc 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
POJ Monthly--2006.12.31, Sempr
题意:
班长分糖
B的糖不能比A多C个以上
班长是n号,1号是史努比。
问班长在符合条件的情况下,最多比史努比多多少糖。
做法:(字母表示该人的糖数)
容易见得, B-A<=C
求n-(1) <=?
很明显的差分约束。
A->B建边,然后计算(1)到n的最短路就好了。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #include <stack> #include <queue> #include <vector> #include <deque> #include <set> #include <map> struct lamp { int v,k,c,l; }; lamp la[1010]; int sum[1010]; int dp[1010]; int cmp(lamp a,lamp b) { return a.v<b.v; } int main() { int t; int cas=1; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d%d%d",&la[i].v,&la[i].k,&la[i].c,&la[i].l); } sort(la+1,la+n+1,cmp); sum[0]=0; for(int i=1;i<=n;i++) sum[i]=sum[i-1]+la[i].l; memset(dp,0x7f7f7f7f,sizeof dp); //printf("%d\n",dp[1]); dp[0]=0; for(int i=1;i<=n;i++) { for(int j=0;j<i;j++) { dp[i]=min(dp[i],dp[j]+la[i].k+la[i].c*(sum[i]-sum[j])); } } printf("Case %d: %d\n",cas++,dp ); } return 0; }
相关文章推荐
- 成员初始化列表
- HDU 1421 搬寝室 (线性dp 贪心预处理)
- python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码
- POJ 2184 01背包+负数处理
- 编译原理第五单元习题
- 快速排序算法
- 解决Eclipse界面无法显示出LogCat或Console等窗口的问题
- poj1160 Post Office
- 软件体系结构风格
- GreenDao
- 【转】Java常量池详解
- TP3.2 引入微信类
- 剑指offer(一)
- #pragma once 和#ifndef ... #define ... #endif 比较
- String内部函数-用于处理字符串
- 各种操作符及其优先级
- pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
- 重建二叉树
- TIPs
- 黑马day18 juery的高级特性&Ajax的$.get()/post()方法