zoukankan      html  css  js  c++  java
  • POJ 3159 Candies (栈优化spfa)

    Candies

    题目链接:

    http://acm.hust.edu.cn/vjudge/contest/122685#problem/J

    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.
    ##题意: 求图中#1到#N的最短路.
    ##题解: 题是很裸的最短路,但是数据非常大. 队列形式的spfa会TLE. 这里需要用栈来优化spfa. 事实上,在不需要判断负环的情况下,栈实现spfa比队列要快. (涨姿势了)
    ##代码: ``` cpp #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 200000 #define inf 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); using namespace std;

    int m,n,k;
    int edges, u[maxn], v[maxn], w[maxn];
    int first[maxn], next[maxn];
    int dis[maxn];

    void add_edge(int s, int t, int val) {
    u[edges] = s; v[edges] = t; w[edges] = val;
    next[edges] = first[s];
    first[s] = edges++;
    }

    //queue q;
    int Q[maxn];
    bool inq[maxn];
    int inq_cnt[maxn];
    bool spfa(int s) {
    int top = 0;
    memset(inq, 0, sizeof(inq));
    memset(inq_cnt, 0, sizeof(inq_cnt));
    for(int i=1; i<=n; i++) dis[i] = inf; dis[s] = 0;
    //while(!q.empty()) q.pop();
    //q.push(s);
    inq_cnt[s]++;
    Q[top++] = s;

    while(top) {
        int p = Q[--top];
        inq[p] = 0;
        for(int e=first[p]; e!=-1; e=next[e]) if(dis[v[e]] > dis[p]+w[e]){
            dis[v[e]] = dis[p] + w[e];
            if(!inq[v[e]]) {
                //q.push(v[e]);
                Q[top++] = v[e];
                inq[v[e]] = 1;
                inq_cnt[v[e]]++;
                //if(inq_cnt[v[e]] >= n) return 0;
            }
        }
    }
    
    return 1;
    

    }

    int main(int argc, char const *argv[])
    {
    //IN;

    /*
        spfa+stack  用queue会TLE
    */
    
    while(scanf("%d %d",&n,&m) != EOF)
    {
        edges = 0;
        memset(first, -1, sizeof(first));
        for(int i=1; i<=m; i++) {
            int u,v,w; scanf("%d %d %d",&u,&v,&w);
            add_edge(u,v,w);
        }
    
        spfa(1);
    
        printf("%d
    ", dis[n]);
    }
    
    return 0;
    

    }

  • 相关阅读:
    机器学习理论基础
    Python 2/3 安装与运行环境设置
    connect/express 的参考
    Koa2 的安装运行记录(二)
    iOS如何才能在招聘中表现得靠谱?
    游戏本地化不止是翻译,还有……
    IOS 判断当前UIViewController 是否正在显示
    获取iOS应用中当前处于Activity状态的ViewController
    APP UI设计趋势:为好设计而动
    实现ios后台缩略图模糊的一种方法
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5751549.html
Copyright © 2011-2022 走看看