POJ 3159 Candies 差分约束

来源:互联网 发布:网络上怼是什么意思 编辑:程序博客网 时间:2024/06/02 18:30

链接:http://poj.org/problem?id=3159

Candies
Time Limit: 1500MS Memory Limit: 131072KTotal Submissions: 24852 Accepted: 6737

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 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

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[n]); }return 0;}





0 0