Candies POJ - 3159 (最短路+差分约束)
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
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std; int dist[];
bool vis[]; typedef pair<int,int>pii;
struct Node
{
int y,val,next;
}node[]; int cnt,head[]; void add(int u,int v,int val)
{
node[++cnt].y=v;
node[cnt].val=val;
node[cnt].next=head[u];
head[u]=cnt;
} int n,m;
priority_queue<pii,vector<pii>,greater<pii> >que;
void dijstra()
{
while(!que.empty())que.pop();
que.push(pii(,));
memset(dist,,sizeof(dist));
while(!que.empty())
{
pii tmp = que.top();
que.pop();
int s=tmp.second;
int v=tmp.first;
if(vis[s])continue;
vis[s]=;
dist[s]=v;
for(int i=head[s];i;i=node[i].next)
{
int to = node[i].y;
if(dist[to] < v+node[i].val)
{
que.push(pii(v+node[i].val,to));
}
}
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v,k;
scanf("%d%d%d",&u,&v,&k);
add(u,v,k);
}
dijstra();
printf("%d\n",dist[n]);
}
Candies POJ - 3159 (最短路+差分约束)的更多相关文章
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
- POJ 3169 Layout(差分约束+最短路)题解
题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...
- K - Candies(最短路+差分约束)
题目大意:给N个小屁孩分糖果,每个小屁孩都有一个期望,比如A最多比B多C个,再多了就不行了,会打架的,求N最多比1多几块糖 分析:就是求一个极小极大值...试试看 这里需要用到一个查分约束的东西 下面 ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
- 转载 - 最短路&差分约束题集
出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★ ...
- poj 1364 King(差分约束)
题意(真坑):傻国王只会求和,以及比较大小.阴谋家们想推翻他,于是想坑他,上交了一串长度为n的序列a[1],a[2]...a[n],国王作出m条形如(a[si]+a[si+1]+...+a[si+ni ...
- poj 1201 Intervals(差分约束)
做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- poj 3169 Layout(差分约束+spfa)
题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...
随机推荐
- Hbase 客户端Scan
Hbase 客户端Scan 标签(空格分隔): Hbase HBase扫描操作Scan 1 介绍 扫描操作的使用和get()方法类似.同样,和其他函数类似,这里也提供了Scan类.但是由于扫描工作方式 ...
- HBase读写的几种方式(二)spark篇
1. HBase读写的方式概况 主要分为: 纯Java API读写HBase的方式: Spark读写HBase的方式: Flink读写HBase的方式: HBase通过Phoenix读写的方式: 第一 ...
- JGUI源码:组件及函数封装方法(7)
以Accordion为例1.在base.js定义一个对象,这样可以和JQuery对象区分开,用户使用组件时比较清晰一点,也可以在这里引用多个库. var JGUI = J = { version : ...
- CentOS7自定义系统服务示例
CentOS7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,需要开机不登陆就能运行的程序,存在系统服务里,即:/usr/lib/ ...
- MFC常用宏
MFC调试宏 TRACE() 其形式与函数printf()的参数一样,功能是在调试运行时把表达式的值输出到Output调试窗口. Debug版有效 ASSERT()——断言宏, 表达式为真,则程 ...
- Input子系统与多点触摸技术-3【转】
转自:https://blog.csdn.net/u012839187/article/details/77335941 版权声明:本文为博主原创文章,欢迎转载,转载请注明转载地址 https://b ...
- Mark SDL2 Lazy Foo's Producations
重点 初学SDL2,此处为笔记.有错误还请指出. 13)When our SDL 2 application runs, the operating system needs to be able t ...
- JavaBean与xml互转的方法详解
本文实例讲述了Jaxb2实现JavaBean与xml互转的方法.分享给大家供大家参考,具体如下: 一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的 ...
- JSX的替代方案(译文)
原文链接:https://blog.bloomca.me/2019/02/23/alternatives-to-jsx.html JSX作为一种流行的模板语言,在各种框架都得到了广泛的应用.但是,如果 ...
- 使用requests+pyquery爬取dd373地下城跨五最新商品信息
废话不多说直接上代码: 可以使用openpyel库对爬取的信息写入Execl表格中代码我就不上传了 import requests from urllib.parse import urlencode ...