Candies

Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 39666   Accepted: 11168

题目链接:http://poj.org/problem?id=3159

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

题意:

给出n个点,m个关系,然后每个关系输入a,b,c,表示a+c>=b,a、b表示两个点,最后求出1和n最大差值为多少。

题解:

差分约束模板题= =直接建边跑最短路即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ,M = ;
int n,m;
int head[N],vis[N],d[N];
struct Edge{
int u,v,w,next ;
}e[M<<];
int tot;
struct node{
int d,u;
bool operator < (const node &A)const{
return d>A.d;
}
};
void adde(int u,int v,int w){
e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
void Dijkstra(int s){
priority_queue <node> q;memset(d,INF,sizeof(d));
memset(vis,,sizeof(vis));d[s]=;
node now;
now.d=;now.u=s;
q.push(now);
while(!q.empty()){
node cur = q.top();q.pop();
int u=cur.u;
if(vis[u]) continue ;
vis[cur.u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
now.u=v;now.d=d[v];
q.push(now);
}
}
}
}
int main(){
cin>>n>>m;
memset(head,-,sizeof(head));
for(int i=;i<=m;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
adde(u,v,c);
}
Dijkstra();
cout<<d[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(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit ...

  5. POJ3159 Candies —— 差分约束 spfa

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

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

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

  7. 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 ...

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

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

  9. poj 3159 Candies 差分约束

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

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

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

随机推荐

  1. 《深入浅出MFC》– Document-View深入探讨

    1.其实Document/View不是什么新东西,Xerox PARC实验室是这种观念的滥觞.它是Smalltalk环境中的关键性部分,在那里它被称为Model-View-Controller(MVC ...

  2. java8lambda表达式初识

    一.函数式接口 只有一个 抽象方法 的 接口 叫函数式接口 /** * @auther hhh * @date 2018/12/24 22:20 * @description 函数式接口:只有 一个 ...

  3. Kubernetes-apiserver

    Kubernetes API服务器为API对象验证和配置数据,这些对象包含Pod.Service.ReplicationController等等.API Server提供REST操作以及前端到集群的共 ...

  4. CSS3新特性回顾

    CSS3 介绍 开始实例 新特征简介 强大的CSS选择器 抛弃图片的视觉效果 盒模型变化(多列布局和弹性盒模型) 阴影效果 Web字体和web Font 图标 CSS33过渡与动画交互效果 媒体查询 ...

  5. xss挑战赛小记 0x03(xssgame)

    0x00 继续做xss吧 这次是xssgame 地址 http://www.xssgame.com/ 一共八关 学到了很多东西 0x01 啥也没有 <svg/onload="alert ...

  6. C++11中default的使用

    In C++11, defaulted and deleted functions give you explicit control over whether the special member ...

  7. OrCAD把原理图中的器件添加到原理图库

    1. 在使用OrCAD的时候,有时需要把别人的原理图里面的器件添加到自己的原理图库,方便以后使用,具体操作如下,依次选择Design Cache---元器件--Copy 2. 选中要存放的原理图库,鼠 ...

  8. C++重载赋值操作符

    1.C++中重载赋值操作函数应该返回什么? 类重载赋值操作符一般都是作为成员函数而存在的,那函数应该返回什么类型呢?参考内置类型的赋值操作,例如 int x,y,z; x=y=z=15; 赋值行为相当 ...

  9. 步骤2:JMeter 分布式测试(性能测试大并发、远程启动解决方案)

    转载(记录) http://www.cnblogs.com/fengpingfan/p/5583954.html http://www.cnblogs.com/puresoul/p/4844539.h ...

  10. 适用于Linux的windows子系统

    Windows基于图形界面的易用性是有目共睹的,这也是很多普通用户往往难以舍弃的原因.但是Linux系统更强大的网络应用开发能力,却又是Windows系统所无法比拟的.一直以来,很多人都在试图采用各种 ...