POJ3159 差分约束系统

Candies
Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 20396 Accepted: 5404

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 integersN andM not exceeding 30 000 and 150 000 respectively.N is the number of kids in the class and the kids were numbered 1 throughN. snoopy and flymouse were always numbered 1 andN. Then followM lines each holding three integersA,B andc in order, meaning that kidA believed that kidB 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
题目意思:
flymouse是幼稚园班上的班长,一天老师给小朋友们买了一堆的糖果,由flymouse来分发,在班上,
flymouse和snoopy是死对头,两人势如水火,不能相容,因此fly希望自己分得的糖果数尽量多于
snoopy,而对于其他小朋友而言,则只希望自己得到的糖果不少于班上某某其他人就行了。

比如A小朋友强烈希望自己的糖果数不能少于B小朋友m个,即B- A<=m,A,B分别为
A、B小朋友的分得的糖果数。这样给出若干组这样的条件,要使fly最后分得的糖果数s1和snoopy
最后分得的糖果数s2差别取到最大!即s2-s1取最大.

因此根据题意,可以列出如下的不等式:
  
    Sbi-Sai<=ci(1=<i<=n)             
  
最终要使得Sn-S1最大;

其实就是一个差分约束系统。
求最短路时用到的三角形不等式中,最终对于每条有向边(u,v)有: d[v]<=d[u]+w(u,v);
将Sbi-Sai<=ci变成Sbi<=Sai+ci;就跟上式的形式相似。

在最短路的松弛过程中每次都是 if(d[v]>d[u]+w(u,v)) then d[v]<=d[u]+w(u,v);
则最后不断的松弛,使得对所有边 d[v]<=d[u]+w(u,v);
对于Sbi<=Sai+ci;通过做bellmanford,Sbi通过不断的松弛,由正的无穷不断减小,直到所有的
约束条件都的到满足,所以这时的求出的Sbi是满足约束条件的最大的一组解!!
这样最后的结果就是Sn-S1,初始时将S1设为0,则最后的结果就是Sn的值!

spfa差分约束,dis[i]为第i人得到的糖果数目。对于每个约束管理就能列出不等式:dis[a]>=dis[b]-c,就能转化为dis[b]<=dis[a]+c。也就是差分约束最短路中的三角不等式。又由于这里求的是第n点与第1个点的关系,所以就把1设为最短路中的其实点而不舍虚拟起始点。按照这个关系建出图求出最短路,则dis[n]就是答案。

差分约束系统入门题。

题中给出(a,b,c)表示s[b] - s[a] <= c,则从a到b连一条长度为c的边。最终结果就是从1到n的最短路。

如果不好理解的话就从差分约束系统的本质出发,因为要解的是一堆不等式,例如:

s[a] - s[b] <= k1

s[b] - s[c] <= k2

s[a] - s[d] <= k3

s[d] - s[c] <= k4

则s[a] - s[c] <= k1 + k2

且s[a] - s[c] <= k3 + k4

因为最终结果要求满足所有不等式的最大值,因此s[a] - s[c] <= min(k1 + k2, k3 + k4),也就是每条到达路径的最小值。

因此题目可以转化为求最短路。


注意:本道题卡了队列, 用栈不会超时
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#define forl(i,a,b)  for(int i=(a);i<(b);++i)
#define forle(i,a,b) for(int i=(a);i<=(b);++i)
#define forg(i,a,b)  for(int i=(a);i>(b);--i)
#define forge(i,a,b) for(int i=(a);i>=(b);--i)
#define mes(a,v)  memset(a, v, sizeof (a) );
#define cpy(a,b)  memcpy(a, b, sizeof (a) );
#define mesn(a, v, n) memset(a, v, (n)*sizeof((a)[0]))
#define cpyn(a, b, n) memcpy(a, v, (n)*sizeof((a)[0]))
const int maxn=30000;
const int maxe=150000;
const int inf=100000000;
struct NodeEdges
{
    int to;
    int next;
    int w;
};
NodeEdges edges[maxe];
int head[maxn],dist[maxn],outque[maxn];
bool inque[maxn];
int n,m,numedges;
void AddEdges(int u,int v,int w)
{
    ++numedges;
    edges[numedges].to=v;
    edges[numedges].w=w;
    edges[numedges].next=head[u];
    head[u]=numedges;
}
bool Spfa(int s,int n)
{
    mes(inque,0);
    mes(outque,0);
    int k;
    stack<int>st;
    for(int i=0;i<=n;i++)
        dist[i]=inf;

    dist[s]=0;
    st.push(s);
    inque[s]=true;
    while(!st.empty())
    {
        int iq=st.top();
        st.pop();
        inque[iq]=false;
        outque[iq]++;
        if(outque[iq]>n)    return false;
        for(k=head[iq];k!=-1;k=edges[k].next)
        {
            if(dist[edges[k].to]>dist[iq]+edges[k].w)
            {
                dist[edges[k].to]=dist[iq]+edges[k].w;
                if(!inque[edges[k].to])
                {
                    inque[edges[k].to]=true;
                    st.push(edges[k].to);
                }
            }
        }

    }
    return true;
}
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    mes(head,-1);
    for(int i=1;i<=m;i++)
    {
        int u,v,w;
        scanf("%d %d %d",&u,&v,&w);
        AddEdges(u,v,w);
    }
   // for(int i=1;i<=n;i++)
        //AddEdges(0,i,0);
    Spfa(1,n);
    printf("%d",dist[n]);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值