poj3159(spfa)

来源:互联网 发布:大数据下的中国 编辑:程序博客网 时间:2024/06/17 23:44

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 21 2 52 1 4

Sample Output

5

Hint

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


贴一个前向星的spfa的模板:

const int INF=100000000,N=1600000;///N表示点的最多数量int head[N],dis[N];bool vis[N];int ip;struct data{    int to,next,w;}tu[N*N];void init(){    ip=0;    memset(head,-1,sizeof(head));}void add(int u,int v,int w){    tu[ip].to=v,tu[ip].w=w,tu[ip].next=head[u],head[u]=ip++;}void spfa(int s,int n){    queue<int>q;    for (int i=0; i<=n; i++)        dis[i]=INF;    memset(vis,0,sizeof(vis));    q.push(s);    dis[s]=0;    while(!q.empty())    {        int h=q.front();        q.pop();        vis[h]=0;         for (int i=head[h]; i!=-1; i=tu[i].next)        {            int v=tu[i].to;            int w=tu[i].w;            if (dis[v]>dis[h]+w)            {                dis[v]=dis[h]+w;                if (!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }}
初学者理解spfa的过程建议望多看代码,多加一些输出看到数据的具体过程。spfa有两种写法,用栈和队列都能写,只是有时候因为出题的数据原因会有不稳定现象,不过到目前为止,我做过的spfa的题一般队列都能过,有个别的TLE的改成栈也就过了,其实没那么难,主要在广搜过程的理解,没什么难度。

下面是我这题ac代码:

#include <iostream>#include<stdio.h>#include<string.h>using namespace std;const int INF=100000000;int head[1600000],dis[1600000],hhh[1600000];bool vis[1600000];struct data{    int to,next,w;}x[1600000];void spfa(int s,int n){    int r=0;    for (int i=0; i<=n; i++)        dis[i]=INF;    memset(vis,0,sizeof(vis));    hhh[r++]=s;    dis[s]=0;    while(r)    {        int h=hhh[--r];        vis[h]=0;         for (int i=head[h]; i!=-1; i=x[i].next)        {            int v=x[i].to;            int w=x[i].w;            if (dis[v]>dis[h]+w)            {                dis[v]=dis[h]+w;                if (!vis[v])                {                    vis[v]=1;                    hhh[r++]=v;                }            }        }    }    printf("%d\n",dis[n]);}int main(){    memset(head,-1,sizeof(head));    int n,m,cnt=0;    scanf("%d%d",&n,&m);    for(int i=0;i<m;i++)    {        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        x[i].to=b,x[i].next=head[a],x[i].w=c,head[a]=cnt++;    }    spfa(1,n);    return 0;}

0 0