转载请注明出处: 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. CString 字符串转化和分割

    1.格式化字符串 CString s;s.Format(_T("The num is %d."), i);相当于sprintf() 2.转为 int 转10进制最好用_ttoi() ...

  2. Mysql学习(慕课学习笔记5)约束

    约束类型: 1.NOT NULL (非空约束) 2.PRIMARY KEY(主键约束) 每张数据表只能存在一个主键 主键保证记录的唯一性 主键自动为NOT NULL (Auto_increment  ...

  3. slf4j教程

    slf4j只是一个门面(facet),它不包含具体的实现,而是将一些log4j,java.logging等实现包装成统一的接口.借用下图展示了常用日志文件的关系: 通过上面的图,可以简单的理清关系! ...

  4. js prototype之诡异

    想必经常写js的人必然会经常性的用到prototype这个属性,我写这篇文章倒不是自己对prototype这个属性理解有多深刻,相反是因为自己理解肤浅,想通过写文章来加深理解.废话不多说.下面总结一下 ...

  5. a标签加绝对定位在图片上面,a的链接和块状属性block失效,而且是所有IE版本都失效的

    谷歌和火狐浏览器下测试是正常的,IE下鼠标移过logo是没有超链接的提示的 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...

  6. CSS实现背景透明/半透明效果的方法

    全透明代码:{background:transparent} 半透明代码:{filter:alpha(opacity=80);-moz-opacity:0.8;width:auto !importan ...

  7. memcached在Windows下的安装

    memcached简介详情请谷歌.这里介绍如何在windows下安装. 1.下载     下载地址:http://download.csdn.net/detail/u010562988/9456109 ...

  8. 自己动手写谷歌API翻译接口

      可以看到,利用GET请求方式,带入某些参数,就会返回一个json数组,QueryString参数如下:     同样的,我们只需要传入这三个参数,就可以获得我们想要的翻译内容,公开方法,代码如下. ...

  9. 通过expdp/impdp进行oracle数据库的备份恢复详细指导

    假定导出oracle数据库home目录为/opt/oracle,数据库用户为exp_user/test,导入用户为imp_user/test,给出如下样例,具体使用时根据实际情况修改路径及用户名/密码 ...

  10. MicrosoftSQLServer中的锁模式

    在SQL Server数据库中加锁时,除了可以对不同的资源加锁,还可以使用不同程度的加锁方式,即锁有多种模式,SQL Server中锁模式包括: 1.共享锁 SQL Server中,共享锁用于所有的只 ...