POJ 3159 Candies 差分约束
来源:互联网 发布:网络上怼是什么意思 编辑:程序博客网 时间:2024/06/02 18:30
链接: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 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 21 2 52 1 4
Sample Output
5
Hint
Source
题意:
班长分糖
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[n]); }return 0;}
- POJ 3159 Candies(差分约束系统)
- 【POJ 3159】 Candies 差分约束系统
- POJ 3159 Candies 差分约束+spfa
- POJ--3159[Candies] 差分约束
- poj 3159 Candies 差分约束 + spfa
- poj-3159-Candies-差分约束+stack
- POJ 3159 Candies 差分约束系统
- poj 3159 Candies 差分约束
- POJ 3159 Candies 差分约束
- POJ 3159 Candies(差分约束)
- POJ 3159 Candies(差分约束系统)
- poj 3159 Candies(差分约束)
- POJ 3159 Candies(差分约束)
- POJ 3159 Candies 差分约束
- 差分约束系统 & poj 3159 Candies
- poj--3159--Candies(简单差分约束)
- POJ 3159Candies (差分约束)
- POJ 3159Candies 差分约束系统
- 知识总结
- 安装 pylibmc(Memcached的python客户端)
- MongoDB的介绍
- Android四大组件之一:Activity
- List对里面元素进行过滤删除
- POJ 3159 Candies 差分约束
- 小散, 教你如何进行集合竞价
- 代理模式、动态代理与AOP
- pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
- zoj 3633 Alice's present
- C之运算符与强制类型转换
- TIPs
- 编译原理第五单元习题
- 黑马day18 juery的高级特性&Ajax的$.get()/post()方法