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;
        }
    }
  • 相关阅读:
    Solution -「洛谷 P5659」「CSP-S 2019」树上的数
    前端随心记---------接私活必备的 10 个开源项目!
    前端随心记---------Vue3.0马上就要来了,TypeScript学会了没?
    前端随心记---------vue3.0终于来了,作者已公布源码
    前端随心记---------vuex
    前端随心记---------为什么要使用Nodejs
    前端随心记---------Javascript系列(第三节.函数的变量提升)
    前端随心记---------Javascript系列(第二节----函数.事件处理程序)
    前端随心记---------Javascript系列(判断一个数是否为素数的三种解法)
    前端随心记---------Javascript系列(第一节)
  • 原文地址:https://www.cnblogs.com/bryce02/p/9927544.html
Copyright © 2011-2022 走看看