您的位置:首页 > 其它

poj 3159 Candies

2016-09-15 18:18 351 查看
Candies

Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 29377 Accepted: 8135
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 A, B 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

Hint

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

POJ Monthly--2006.12.31, Sempr


提示

题意:

飞鼠是班长,老师给了飞鼠一些糖果,班上有n(1<=n<=30000)个小孩,有m(1<=m<=150000)条信息,每条信息表示第a个小孩的糖果数不会比第b个小孩多c个糖果。求出第1个到第n个小孩最多相差多少个糖果。

思路:

差分约束系统基础题,不等式有:

a[i]-a[i-1]<=c.

用spfa求最短路(用最小来更新)就行了。值得注意的是用队列会TLE,把队列改成栈会快很多。

Dijkstra需要有优先队列优化。下面只给出spfa,Dijkstra表示不会(%>_<%)。


示例程序

Source Code

Problem: 3159 Code Length: 1122B
Memory: 2148K Time: 579MS
Language: GCC Result: Accepted
#include <stdio.h>
#include <string.h>
#define MAX 1000000007
struct
{
int v,c,next;
}w[150000];
int h[30000],q[200000],numw;
void insert(int u,int v,int c)
{
w[numw].v=v;
w[numw].c=c;
w[numw].next=h[u];
h[u]=numw;
numw++;
}
int spfa(int n)
{
int i,v[30000],d[30000],pos,pos1,top=0;
for(i=0;30000>i;i++)
{
v[i]=0;
d[i]=MAX;
}
d[0]=0;
q[top]=0;
top++;
v[0]=1;
while(top!=0)
{
pos=q[top];
v[pos]=0;
for(i=h[pos];i!=-1;i=w[i].next)
{
pos1=w[i].v;
if(d[pos1]>d[pos]+w[i].c)
{
d[pos1]=d[pos]+w[i].c;
if(v[pos1]==0)
{
q[top]=pos1;
top++;
v[pos1]=1;
}
}
}
top--;
}
return d[n-1];
}
int main()
{
int n,m,i,u,v,c;
numw=0;
memset(h,-1,sizeof(h));
scanf("%d %d",&n,&m);
for(i=1;m>=i;i++)
{
scanf("%d %d %d",&u,&v,&c);
insert(u-1,v-1,c);
}
printf("%d",spfa(n));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: