zoukankan      html  css  js  c++  java
  • Candies

    Candies
    Time Limit: 1500MS   Memory Limit: 131072K
    Total Submissions: 30247   Accepted: 8409

    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.

    教训

    注意样例中先输入的是点数还是边数

    调了一上午,写了三遍,队列,栈,vector,结构体。。。

    终于不TLE了,WA了

    原来就是因为这个。。。。记住这血的教训

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    #define MAXM 150010
    #define MAXN 30010
    int e_num,head[MAXN],cnt,n,m,dis[MAXN];
    int q[MAXN],top;
    bool f[MAXN];
    struct node
    {
        int pre,to,v;
    }e[MAXM];
    int qread()
    {
        int j=0;
        char ch=getchar();
        while(ch<='9'&&ch>='0'){j=j*10+ch-'0';ch=getchar();}
        return j;
    }
    void insert(int from,int to,int v)
    {
        e[++e_num].to=to;
        e[e_num].v=v;
        e[e_num].pre=head[from];
        head[from]=e_num;
    }
    void spfa(int s,int t)
    {
        memset(dis,0x3f,sizeof(dis));
        memset(f,0,sizeof(f));
        int point=s;dis[s]=0;
        q[++top]=point;
        f[point]=1;
        while(top>0)
        {
            point=q[top];
            top--;
            f[point]=0;
            for(int i=head[point];i;i=e[i].pre)
            {
                int k=e[i].to;
                if(dis[point]+e[i].v<dis[k])
                {
                    dis[k]=dis[point]+e[i].v;
                    if(f[k]==0)
                    {
                        q[++top]=k;
                        f[k]=1;
                    }
                }
            }
        }cout<<dis[t];
    }
    int main()
    {
        n=qread(),m=qread();
        int a,b,c;
        for(int i=1;i<=m;i++)
        {
            a=qread(),b=qread(),c=qread();
            insert(a,b,c);
        }
        spfa(1,n);
    }
  • 相关阅读:
    Java实现 LeetCode 440 字典序的第K小数字
    Java实现 LeetCode 438 找到字符串中所有字母异位词
    route命令详解与使用实例
    Google protobuf的安装及使用
    linux内核驱动中_IO, _IOR, _IOW, _IOWR 宏的用法与解析
    _GUN_SOURCE宏
    CodeViz产生函数调用图
    linux下阅读源代码的工具
    linux gcc 编译时头文件和库文件搜索路径
    Makefile第四讲:include 引用其它makefile文件
  • 原文地址:https://www.cnblogs.com/thmyl/p/7359338.html
Copyright © 2011-2022 走看看