zoukankan      html  css  js  c++  java
  • poj3159 Candies

    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.
     
    题意:对n个值给出m个约束条件a,b,c,每个条件都满足v[b]-v[a] <= c,求v[n]-v[1]的最大值
     
    /*
        这是一道差分约束的裸题。
        所谓差分约束,就是满足若干个条件满足i-j <= ck
        问任意两个数x-y的最大值
        解法就是链接j->i权值为ck的有向边,最大值即为y->x的最短路径长度
        SPFA和dj搞一搞就好了。
    */
    
    #include <cstdio>
    #include <iostream>
    #include <stack>
    
    using namespace std;
    
    const int maxn = 100001;
    int d[maxn],vis[maxn],head[maxn];
    int n,m,s,cnt;
    
    struct node{
        int to,pre,v;
    }G[500005];
    
    void addedge(int from,int to,int v){
        G[++cnt].to = to;
        G[cnt].v = v;
        G[cnt].pre = head[from];
        head[from] = cnt;
    }
    
    void spfa(){
        stack<int> q;
        for(int i = 1;i <= n;i++) d[i] = 0x7fffffff;
        q.push(1);vis[1] = 1;d[1] = 0;
        while(!q.empty()){
            int x = q.top();q.pop();vis[x] = 0;
            for(int i = head[x];i;i = G[i].pre){
                int v = G[i].to,w = G[i].v;
                if(d[v] > d[x] + w){
                    d[v] = d[x] + w;
                    if(!vis[v]){
                        vis[v] = 1;
                        q.push(v);
                    }
                }
            }
        }
    }
    
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i = 1;i <= n;i++) vis[i] = 0,head[i] = -1;
            int x,y,z;
            for(int i = 1;i <= m;i++){
                scanf("%d%d%d",&x,&y,&z);
                addedge(x,y,z);
            }
            spfa();
            printf("%d ",d[n]);
            return 0;
        }
    }
  • 相关阅读:
    微信打开网址添加在浏览器中打开提示遮罩
    Java内存分配之堆、栈和常量池
    腾讯面试题04.进程和线程的区别?
    cookie 和 session的区别
    jvm内存模型-回收算法-和内存分配以及jdk、jre、jvm是什么关系(阿里,美团,京东面试题)
    HTTP中GET与POST的区别
    Socket send函数和recv函数详解
    JSP九大内置对象及四个作用域
    JavaScript
    网页布局(html+css基础)
  • 原文地址:https://www.cnblogs.com/bryce02/p/9927544.html
Copyright © 2011-2022 走看看