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);
}
原文地址:https://www.cnblogs.com/thmyl/p/6292457.html