Candies SPFA + 栈

来源:互联网 发布:威盾软件 编辑:程序博客网 时间:2024/05/29 06:58
Candies
Time Limit: 1500MS Memory Limit: 131072KTotal Submissions: 33447 Accepted: 9392

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 integersN 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 throughN. snoopy and flymouse were always numbered 1 and N. Then followM lines each holding three integers A, B and c in order, meaning that kidA 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 21 2 52 1 4

Sample Output

5

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

POJ Monthly--2006.12.31, Sempr


这是一道 stl队列+ SPFA过不了的题,一般就T了
然后发现我手写队列会RE,会WA,也会TLE。。。
而有大佬用栈实现SPFA。。。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>#define inf 0x3f3f3f3f#define ms(x) memset(x,false,sizeof(x))using namespace std;const int N = 2000020;struct Edge{    int v;    int w;    int next;};Edge q[N];int n, cnt;int head[4*N], dis[N];int que[N*10];bool vis[N];void addedge(int u, int v, int w){    q[cnt].v = v, q[cnt].w = w;    q[cnt].next = head[u];    head[u] = cnt++;}template <typename T>inline bool scan_d (T &ret) {    char c;    int sgn;    if (c = getchar(), c == EOF) return 0; //EOF    while (c != '-' && (c < '0' || c > '9') ) {        if((c = getchar()) == EOF) return 0;    }    sgn = (c == '-') ? -1 : 1;    ret = (c == '-') ? 0 : (c - '0');    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');    ret *= sgn;    return 1;}void SPFA(int s){    ms(vis);    for(int i=1;i<=n;i++) dis[i] = inf;    dis[s] = 0;    vis[s] = true;    int top,tail;    top = tail = 0;    que[top++] = s;    //tail++;    while(top!=0)    {        int u  = que[--top];        vis[u] = false;        int i;        for(i=head[u];i!=-1;i=q[i].next)        {            int v = q[i].v;            if(dis[v] > dis[u] + q[i].w)            {                dis[v] = dis[u] + q[i].w;                if(!vis[v])                {                    vis[v] = true;                    que[top++] = v;                    //if(++cnt[v] > n) return false;                }            }        }    }    //return true;}int main(){    int T, m;        scan_d(n);        scan_d(m);        memset(head, -1, sizeof(head));        cnt=0;        for(int i=1;i<=m;i++)        {            int u, v, w;            scan_d(u);            scan_d(v);            scan_d(w);           // scanf("%d%d%d",&u,&v,&w);            addedge(u,v,w);        }SPFA(1);        printf("%d\n",dis[n]);    return 0;}



原创粉丝点击