# 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;
}

• 本文已收录于以下专栏：

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

DescriptionDuring the kindergarten days, flymouse was the monitor of his class. Occasionally the hea...
• blue_skyrim
• 2016年08月13日 12:48
• 772

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

CandiesCrawling in process... Crawling failed Time Limit:1500MS     Memory Limit:131072KB     6...
• u013486414
• 2014年08月22日 17:04
• 806

## poj3159Candies——差分约束系统

poj3159Candies：http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 13107...
• hahaAll
• 2016年02月29日 22:10
• 348

## Candies-POJ3159差分约束

Time Limit: 1500MS Memory Limit: 131072KDescriptionDuring the kindergarten days, flymouse was the ...
• huayunhualuo
• 2016年02月16日 18:26
• 862

## POJ3159：Candies（差分约束）

Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 32836   Acce...
• junior19
• 2017年08月13日 23:16
• 57

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

• Na_OH
• 2017年03月18日 01:59
• 113

## 差分约束 poj3159 Candies

• qwb492859377
• 2016年04月05日 19:41
• 185

## POJ3159 Candies（差分约束）

• hcx11333
• 2017年04月29日 00:23
• 103

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

http://poj.org/problem?id=3159 题意：n个点m个关系，每个关系中A B w代表B比A多至多w颗糖，求n号人比1号人最多多几个糖果。 ps：差分约束第一题。 思路...
• Flynn_curry
• 2016年10月20日 15:48
• 114

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

• qq_21057881
• 2016年01月24日 16:24
• 506

举报原因： 您举报文章：POJ3159 Candies（差分约束+SPFA的栈实现） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)