zoukankan      html  css  js  c++  java
  • POJ3159 Candies

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=267#problem/C

                                                     C - Candies
    Time Limit:1500MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

    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

    这题的条件很简单就是

    db-da<=x;

    但是这题必须用栈的思想去解题,用队列去做会超时,我也不知道为什么??

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stack>
    #define N 1000001
    using namespace std;
    int n,m,tt;
    int head[30001];
    struct node
    {
        int x,y,z;
        int next;
    } q1[150001];
    int dis[30001],v[30001];
    void add(int xx,int yy,int zz)
    {
        q1[tt].x=xx;
        q1[tt].y=yy;
        q1[tt].z=zz;
        q1[tt].next=head[xx];
        head[xx]=tt;
        tt++;
    }
    void spfa()
    {
        stack<int>q;
        for(int i=0;i<=n;i++)
        {
            dis[i]=N;
            v[i]=0;
        }
        v[1]=1;
        dis[1]=0;
        q.push(1);
        while(!q.empty())
        {
            int ff=q.top();
            q.pop();
            v[ff]=0;
            for(int i=head[ff];i!=-1;i=q1[i].next)
            {
                if(dis[q1[i].y]>dis[ff]+q1[i].z)
                {
                    dis[q1[i].y]=dis[ff]+q1[i].z;
                    if(v[q1[i].y]==0)
                    {
                       q.push(q1[i].y);
                       v[q1[i].y]=1;
                    }
                }
            }
        }
       printf("%d
    ",dis[n]);
    }
    int main()
    {
        int xx,yy,zz;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            tt=0;
            memset(head,-1,sizeof(head));
            while(m--)
            {
                scanf("%d%d%d",&xx,&yy,&zz);
                add(xx,yy,zz);
            }
            spfa();
        }
        return 0;
    }
  • 相关阅读:
    php的迭代器
    memcache学习使用
    php数组操作小结
    ThinkPHP-3.2.3学习
    正则
    PHP中$_FILES的使用方法及注意事项说明
    子进程管理模块subprocess
    atexit模块解析
    GNU自动补全模块readline解析
    python命令行解析工具argparse模块【3】
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/3931378.html
Copyright © 2011-2022 走看看