# POJ3159 Candies（差分约束+SPFA的栈实现）

Candies
 Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 29256 Accepted: 8104

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

#include<stdio.h>                //spfa基本上要这些头文件
#include<string.h>
using namespace std;

const int maxn=30005;
const int maxm=150005;
const int INF=0x3f3f3f3f;

int size;//边的个数
int dist[maxn];//起始点到各点距离
int vis[maxn];//在队列标志
int Q[maxn];//手写栈

struct node
{
int to,next;
int v;
}edge[maxm];

void init()
{
size=0;
}

{    //有向图只需要前一半
edge[size].to=b;
edge[size].v=v;
}

void SPFA(int start,int n)
{
int top=0;
for(int v=1;v<=n;v++)//???
{
if(v==start)
{
Q[top++]=v;
vis[v]=true;
dist[v]=0;
}
else
{
vis[v]=false;
dist[v]=INF;
}
}
while(top!=0)
{
int u=Q[--top];
vis[u]=false;
{
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;
Q[top++]=v;
}
}
}
}
}

int main()
{
int T;
int n,m;
int a,b,c;
scanf("%d%d",&n,&m);
init();
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
}
SPFA(1,n);
printf("%d\n",dist[n]);
return 0;
}

#### Candies(差分约束_栈+SPFA)

2014-08-22 17:04:41

#### poj3159Candies——差分约束系统

2016-02-29 22:10:27

#### poj3159 Candies（差分约束，最短路）

2016-01-24 16:24:18

#### poj3159——Candies（差分约束+SPFA堆栈）

2016-08-13 12:48:07

#### Candies-POJ3159差分约束

2016-02-16 18:26:17

#### 谈SPFA解差分约束最大值最小值的原理...

2011-10-11 21:59:53

#### poj3159 Candies（差分约束入门）

2016-10-20 15:48:19

#### 差分约束 poj3159 Candies

2016-04-05 19:41:01

#### POJ3159 Candies（差分约束）

2017-04-29 00:23:58

#### poj3159 Candies 最短路&差分约束

2017-03-18 01:59:41