POJ 3159 Candies(差分规划+SPFA)

来源:互联网 发布:c语言界面设计 编辑:程序博客网 时间:2024/06/07 05:30

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.


由 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术,即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。
这里注意用栈代替队列,而且保存边的方式也要注意



#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <iomanip>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>#define rep(i,m,n) for(int i=m;i<=n;i++)#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)const int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define MOD 1000000007#define pb push_back#define mp make_pair#define fi first#define se second#define pi acos(-1.0)#define pii pair<int,int>#define Lson L, mid, rt<<1#define Rson mid+1, R, rt<<1|1const int maxn=5e2+10;using namespace std;typedef  vector<int> vi;typedef  long long ll;typedef  unsigned long long  ull;inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};const int N = 1e7+5;const int INF=0x3f3f3f3f;int m,n,x;const int MAXN= 30005 ;const int MAXE= 150010 ;struct Edge{    int to;    int v;    int next;}edge[MAXE];class SPFA{public:void init(){        tol = 0;        memset(head,-1,sizeof(head));for(int i=1;i<=n;i++)dist[i]=INF;}    void addedge(int a,int b,int v)//加边    {        edge[tol].to=b;        edge[tol].v=v;        edge[tol].next=head[a];        head[a]=tol++;    }bool Run(int start,int n){vis[start]=true;dist[start]=0;        int top=0;        int ss[30010];ss[top++]= start;while(top!=0){int u=ss[--top];vis[u]=false;            for(int i=head[u];i!=-1;i=edge[i].next)            {                int v=edge[i].to;                if(dist[v]>dist[u]+edge[i].v)                {                    dist[v]=dist[u]+edge[i].v;                    if(!vis[v])                    {                        vis[v]=true;                        ss[top++]=v;                    }                }            }}return true;}public:int dist[MAXN];private:    int   tol;    int   head[MAXN];//每个结点的头指针bool  vis[MAXN];//在队列标志};int main(){    ios::sync_with_stdio(false);    int u;    int v;    int w;    SPFA sf;    scanf("%d %d",&n,&m);    sf.init();    for(int i=0;i<m;i++)    {        scanf("%d%d%d",&u,&v,&w);        sf.addedge(u,v,w);    }    sf.Run(1,n);    printf("%d\n",sf.dist[n]);    return 0;}




0 0
原创粉丝点击