转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Candies
Time Limit: 1500MS   Memory Limit: 131072K

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.
 
题意:
有n个人编号为1至n,m个要求,每条要求为ui,vi,wi。代表编号ui的人分到的糖果最多只能比编号为vi的人少wi个。要求求出编号为n的人最多能比编号为1的人多几个糖果。
分析:
设x[i]代表编号为i的人所分到的糖果数目。
则可以得到如下式子x[vi]-x[ui]<=wi;
根据该式子,建图即为从ui向vi连一条权值为wi的有向边。
而后利用最短路求解。
注意:该题spfa中如果用队列会TLE,栈能AC。当然,由于该题的权值全部为正,故可采用dijkstra
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <cstdlib>
using namespace std;
#define REP(A,X) for(int A=0;A<X;A++)
#define MAXE 200010
#define MAXP 30010
#define INF 0x7fffffff
#define MP(A,B) make_pair(A,B)
typedef pair<int,int> PII ;
struct node{
int v,d,next;
}edge[MAXE];
int e=;
int head[MAXP];
void init(){
e=;
REP(i,MAXP)head[i]=-;
}
void add_edge(int u,int v,int d)
{
edge[e].v=v;
edge[e].d=d;
edge[e].next=head[u];
head[u]=e;
e++;
}
int vis[MAXP],dis[MAXP];
void spfa()
{
REP(i,MAXP)vis[i]=;
REP(i,MAXP)dis[i]=i==?:INF;
//queue<int>q;
stack<int>q;
q.push();
vis[]=;
while(!q.empty()){
//int x=q.front();
int x=q.top();
q.pop();
for(int t=head[x];t!=-;t=edge[t].next)
{
int y=edge[t].v;
int d=edge[t].d;
if(dis[y]>dis[x]+d){
dis[y]=dis[x]+d;
if(!vis[y]){
q.push(y);
vis[y]=;
}
}
}
vis[x]=;
}
}
void dijkstra(int s)
{
REP(i,MAXP)vis[i]=;
REP(i,MAXP)dis[i]=i==s?:INF;
priority_queue<PII,vector<PII>,greater<PII> > q;
q.push(MP(dis[s],s));
while(!q.empty())
{
PII p=q.top();
q.pop();
int x=p.second;
if(vis[x])continue;
vis[x]=;
for(int t=head[x];t!=-;t=edge[t].next)
{
int y=edge[t].v;
int d=edge[t].d;
if(!vis[y]&&dis[y]>dis[x]+d)
{
dis[y]=dis[x]+d;
q.push(MP(dis[y],y));
}
}
} } int main()
{
int m,n;
while(scanf("%d%d",&n,&m)!=EOF){
int u,v,w;
init();
REP(i,m){
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
}
//dijkstra(1);
spfa();
printf("%d\n",dis[n]);
}
return ;
}

代码君

poj3159 Candies(差分约束)的更多相关文章

  1. poj3159 Candies(差分约束,dij+heap)

    poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...

  2. POJ-3159.Candies.(差分约束 + Spfa)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 40407   Accepted: 11367 Descri ...

  3. [poj3159]Candies(差分约束+链式前向星dijkstra模板)

    题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...

  4. POJ3159 Candies —— 差分约束 spfa

    题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submiss ...

  5. POJ-3159(差分约束+Dijikstra算法+Vector优化+向前星优化+java快速输入输出)

    Candies POJ-3159 这里是图论的一个应用,也就是差分约束.通过差分约束变换出一个图,再使用Dijikstra算法的链表优化形式而不是vector形式(否则超时). #include< ...

  6. POJ 3159 Candies 差分约束dij

    分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u ...

  7. [poj 3159]Candies[差分约束详解][朴素的考虑法]

    题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...

  8. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  9. POJ3159(KB4-K 差分约束)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 33283   Accepted: 9334 Descrip ...

随机推荐

  1. hdu 2642 Stars

    Problem Description Yifenfei is a romantic guy and he likes to count the stars in the sky. To make t ...

  2. jquery 做出专业的界面,SHOW 一下最近的成果~~~

    最近在项目中把整个UI框架重新做了一下,都是用Jquery实现的,没有使用EXT.EasyUI那一类的UI框架再也不用担心版权问题啦~~~~~~ 接下来我会在博客中把常用的功能分享出来,先上一下动态T ...

  3. linux配置备忘

    ubuntu英文系统环境下,emacs输入中文设置:(http://www.cnblogs.com/pylemon/archive/2012/01/05/2312682.html) ~/.profil ...

  4. format %x invalid or incompatible with argument问题解决方法

    现在还有好多朋友在用Protel 99se来画图,可是在现在的双核或四核电脑上运行Protel出现错误并且弹出对话框:“format '%x' invalid or incompatible with ...

  5. C++中初始化和定义对象的语法,带括号与不带括号的区别

    小记:运行环境:win xp  vs2008 #include <iostream>#include <string> using std::cout;using std::c ...

  6. HDU 5428 The Factor (素因数分解)

    题意:给出n个数,问这n个数的乘积中至少有三个因子的最小因子.若不存在这样的因子,则输出 -1: 思路:求出每个数的最小的两个素因数,然后输出其中最小的两个数的乘积. 代码: #include< ...

  7. sudo nopasswd

    preface,不问头条,但汝读荐,诚意满满的!

  8. linux下面测试网络带宽 (转载)

    利用bmon/nload/iftop/vnstat/iptraf实时查看网络带宽状况 一.添加yum源方便安装bmon# rpm -Uhv http://apt.sw.be/redhat/el5/en ...

  9. QString转换为char*

    QString在Qt里相当于C++里的std::string,或者是C里的c style string.不过,QString跟编码相关,在低层想把一个QString发送出去相当麻烦,尤其对方用的不是Q ...

  10. 在多线程环境中使用Jedis

    Jedis是一个Java语言的Redis客户端,它为Java语言连接与操作Redis提供了简单易用的接口. Jedis不是线程安全的.故不应该在多线程环境中共用一个Jedis实例.可是.也应该避免直接 ...