• POJ 3159 Candies(差分规划+SPFA)


    Candies
    Time Limit: 1500MS   Memory Limit: 131072K
    Total Submissions: 18737   Accepted: 4930

    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 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 3159
    
    差分约束+SPFA
    */
    
    /*
    /*
    给n个人派糖果,给出m组数据,每组数据包含A,B,c  三个数,
    意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。
    最后求n 比 1 最多多多少糖果。
    【解题思路】
    这是一题典型的差分约束题。不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,
    我们得到 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术,
    即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。
    即是满足题中的条件dis[B]-dis[A]<=w(A,B),由于要使dis[B] 最大,
    所以这题可以转化为最短路来求。
    这题如果用SPFA 算法的话,则需要注意不能用spfa+queue 来求,会TLE ,而是用 spfa + stack
    
    2012-8-17
    */
    
    
    
    */
    
    
    
    
    //SPFA的队列实现会超时,堆栈实现可以。
    //没有负环回路判断,堆栈实现SPFA(有时候堆栈确实比较快)
    //G++ 516ms
    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    
    const int MAXN=30010;
    const int MAXE=150010;
    const int INF=0x3f3f3f3f;
    int head[MAXN];//每个结点的头指针
    int vis[MAXN];//在队列标志
    
    int Q[MAXN];//堆栈
    int dist[MAXN];
    
    struct Edge
    {
        int to;
        int v;
        int next;
    }edge[MAXE];
    int tol;
    void add(int a,int b,int v)//加边
    {
        edge[tol].to=b;
        edge[tol].v=v;
        edge[tol].next=head[a];
        head[a]=tol++;
    }
    void SPFA(int start,int n)
    {
        int top=0;
        for(int v=1;v<=n;v++)//初始化
        {
            if(v==start)
            {
                Q[top++]=v;
                vis[v]=true;
                dist[v]=0;
            }
            else
            {
                vis[v]=false;
                dist[v]=INF;
            }
        }
        while(top!=0)
        {
            int u=Q[--top];
            vis[u]=false;
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].to;
                if(dist[v]>dist[u]+edge[i].v)
                {
                    dist[v]=dist[u]+edge[i].v;
                    if(!vis[v])
                    {
                        vis[v]=true;
                        Q[top++]=v;
                    }
                }
            }
        }
    }
    int main()
    {
       // freopen("in.txt","r",stdin);
      //  freopen("out.txt","w",stdout);
        int n;
        int M;
        int a,b,c;
        while(scanf("%d%d",&n,&M)!=EOF)
        {
            tol=0;//加边计数,这个不要忘
            memset(head,-1,sizeof(head));
            while(M--)
            {
                scanf("%d%d%d",&a,&b,&c);
                //b-a<=c
                add(a,b,c);
                //大-小<=c ,有向边(小,大):c
            }
            SPFA(1,n);
            printf("%d\n",dist[n]);
        }
        return 0;
    }
  • 相关阅读:
    从开发人员角度对软件测试的些许理解
    ObjectiveC的语法
    HttpModule与HttpHandler使用
    我为什么学习HASKELL?
    Linux下C语言编程环境Make命令和Makefile
    一个简单的验证框架
    程序员之路
    ObjectiveC语法之ObjectiveC语言和IOS系统(简介,语法,系统结构)
    Teamcity
    Python进阶 错误处理
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2644685.html
Copyright © 2020-2023  润新知