poj3159

Candies

Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 28133   Accepted: 7766

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

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

POJ Monthly--2006.12.31, Sempr

题解:

题意:班上有n个同学,现在有一些糖要分给他们,设第i个同学得到的糖为p[i],分糖必须满足条件:第i个同学要求第j个同学的糖不能超过自己k个,即p[j] - p[i] <= k,k >= 0。要求在满足这些条件的情况下,求出p[n] - p[1]的最大值。

分析:由p[j] - p[i] <= k可得p[j] <= p[i] + k

在单源最短路径的算法中有一步是“若mindis[j] > mindis[i] + dis[i][j],则mindis[j] = mindis[i] + dis[i][j],这样就满足mindis[j] <= mindis[i] + dis[i][j]”。因此本题可以使用单源最短路径的算法来解决,对于“第i个同学要求第j个同学的糖不能超过自己k个,即p[j] - p[i] <= k,k >= 0”这个条件,建立一条边(i->j)=k,由于不含负权路径,因此建立完所有边之后以第1个同学为起点,可以利用Spfa+Stack算法求解,但由于数据原因必须用Stack,如果用Queue则会超时。

Pass:

一直不知道差分约束是什么类型题目,最近在写最短路问题就顺带看了下,原来就是给出一些形如x-y<=b不等式的约束,问你是否满足有解的问题

好神奇的是这类问题竟然可以转换成图论里的最短路径问题,下面开始详细介绍下

比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值,我们可以把a,b,c转换成三个点,k1,k2,k3是边上的权,如图

由题我们可以得知,这个有向图中,由题b-a<=k1,c-b<=k2,得出c-a<=k1+k2,因此比较k1+k2和k3的大小,求出最小的就是c-a的最大值了

根据以上的解法,我们可能会猜到求解过程实际就是求从a到c的最短路径,没错的....简单的说就是从a到c沿着某条路径后把所有权值和k求出就是c -a<=k的一个

推广的不等式约束,既然这样,满足题目的肯定是最小的k,也就是从a到c最短距离...

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define N 150010
int d[N],u[N],v[N],head[N],next[N],stack[N*4],vis[N];
int n,m,S,T,x,y,z,tot=0;
inline void bianbao(int x,int y,int z){
    u[++tot]=y;
    v[tot]=z;
    next[tot]=head[x];
    head[x]=tot;
}
inline void spfa(){
    for(int i=2;i<=n;i++) d[i]=0x3f3f3f3f;
    d[S=1]=0;
    int top=0;
    stack[++top]=S;
    vis[S]=1;
    while(top){
        int p=stack[top--];
        vis[p]=0;
        for(int i=head[p];i;i=next[i])
            if(d[u[i]]>d[p]+v[i]){
                d[u[i]]=d[p]+v[i];
                if(!vis[u[i]]){
                    vis[u[i]]=1;
                    stack[++top]=u[i];
                }
            }
    }
    printf("%d\n",d[n]);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&x,&y,&z);
        bianbao(x,y,z);
    }
    spfa();
    return 0;
}
时间: 2024-10-07 05:30:16

poj3159的相关文章

poj3159 Candies(差分约束,dij+heap)

poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d[v] – d[u] <= w. 再看题目给出的关系:b比a多的糖果数目不超过c个,即d[b] – d[a] <= c ,正好与上面模型一样, 所以连边a->b,最后用dij+heap求最短路就行啦. ps:我用vector一直TLE,后来改用前向星才过了orz... 1 #include&

poj3159 Candies 2012-09-07

http://poj.org/problem?id=3159 差分约束系统 (spfa+ 队列)会TLE,(spfa+stack)可以过,不过有点奇葩的是建边时 输入 (a,b,c) 如果连边 w(b,a)=c 然后 做 spfa(n),输出dis[1]会超时.反过来 连边 w(a,b)=c,然后做spfa(1),输出dis[n] 就484Ms过了.数据比较坑 爹吧. 1 Program poj3159; 2 3 type cord=record 4 5 ne,da,po:longint; 6

poj3159 Candies(差分约束)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit: 131072K Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’

poj3159 Candies

POJ - 3159 Candies 题目大意: 给n个小孩发糖,有m个条件 每个条件给出a,b,c,使得v[b]-v[a]<=c 求v[n]-v[1]的最大值 Sample Input 2 2 1 2 5 2 1 4 Sample Output 5 /* 差分约束裸题. 差分约束的基本模式为,给出若干个形如a-b<=c的不等式,问你x-y的最大值是多少 那么对每个不等式a-b<=c,连一条由b指向a权值为c的有向边,所有的不等式构成一个图 那么答案就是y到x的最短路 */ #inclu

POJ3159 Dijkstra+邻接表+优先队列

今天学习到了一种新姿势,用邻接表+优先队列优化Dijkstra,这样时间复杂度就由O(N^2+E)变为O(NlogN+E),妈妈再也不用担心我超时了!~\(^o^)/ Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 25077   Accepted: 6810 Description During the kindergarten days, flymouse was the monitor of his

【POJ3159】Candies 裸的pqspfa模版题

不多说了,就是裸的模版题. 贴代码: <span style="font-family:KaiTi_GB2312;font-size:18px;">#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 30500 #define M 200000 #define

poj3159 差分约束 spfa

1 //Accepted 2692 KB 1282 ms 2 //差分约束 -->最短路 3 //TLE到死,加了输入挂,手写queue 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std;

poj3159——Candies(差分约束+SPFA堆栈)

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 ofte

差分约束Poj3159 Candies

没负环.直接搞就行,但是 spfa 队列会超时. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include <cstdio> #include <cstdl