[poj3159]Candies(差分约束+链式前向星dijkstra模板)
题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果
解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与dijkstra中的d[v]>=d[u]+C相同,即可求解。
最小值用最长路,最大值用最短路。
技巧:有一个超级源点向所有点连边,来固定每个点的约束。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<queue>
#define max_v 32000
#define maxm 152000
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int head[max_v],tot;
struct edge{
int to,w,nxt;
}e[maxm];
void add_edge(int u,int v,int w){
e[tot].w=w;
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
}
typedef pair<int,int> P;
int V,n,m,a,b,c,d[max_v];
void dijkstra(int s){
priority_queue<P,vector<P>,greater<P> >que;
fill(d,d+V+,inf);
d[s]=;
que.push(P(,s));
while(que.size()){
P p=que.top();
que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=head[v];i!=-;i=e[i].nxt){
edge ex=e[i];
if(d[ex.to]>d[v]+ex.w){
d[ex.to]=d[v]+ex.w;
que.push(P(d[ex.to],ex.to));
}
}
}
} int main(){
memset(head,-,sizeof head);
scanf("%d%d",&n,&m);
V=n;
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
dijkstra();
printf("%d\n",d[n]);
return ;
}
[poj3159]Candies(差分约束+链式前向星dijkstra模板)的更多相关文章
- POJ 3169 Layout(差分约束+链式前向星+SPFA)
描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板
一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...
- 图的存储结构:邻接矩阵(邻接表)&链式前向星
[概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- 链式前向星+SPFA
今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...
- hdu2647 逆拓扑,链式前向星。
pid=2647">原文地址 题目分析 题意 老板发工资,可是要保证发的工资数满足每一个人的期望,比方A期望工资大于B,仅仅需比B多1元钱就可以.老板发的最低工资为888元.输出老板最 ...
- 【模板】链式前向星+spfa
洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...
- zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)
2131: Can Win Time Limit: 1 Sec Memory Limit: 128 MB Submit: 431 Solved: 50 SubmitStatusWeb Board ...
- HDU1532 Drainage Ditches SAP+链式前向星
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- 转:autofac在mvc和webapi集成的做法
本文转自:http://www.cnblogs.com/Hai--D/p/5992573.html var builder = new ContainerBuilder(); // Mvc Regis ...
- 【VS2013编译DirectX Tutorials时遇到的错误】"const wchar_t *" 类型的实参与 "LPCSTR" 类型的形参不兼容
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/3696367.html 通过查看LPCSTR的定义: typedef _Null_te ...
- 条款42:了解typename的双重含义
typename在很多种情况下与class是完全相同的,例如下面的使用: templame<typename T> ...... template<class T> ..... ...
- Buffer 和Cache 的区别
Buffer 和Cache 的区别buffer 与cache 操作的对象就不一样.buffer
- HDU - 5307 :He is Flying (分治+FFT)(非正解)
JRY wants to drag racing along a long road. There are nn sections on the road, the ii-th section has ...
- ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...
- secret CRT 会话光标不闪烁问题
点击 选项->会话选项 然后在取消即可,就有了闪烁的光标,应该是个bug.
- php中empty(), is_null(), isset()函数区别(转)
empty(), is_null(), isset()真值表(区别) 我们先来看看这3个函数的功能描述 www.111cn.net isset 判断变量是否已存在,如果变量存在则返回 TRUE,否 ...
- think python chapter2
1.An assignment statement creates a new variable and gives it a value: Variable names can be as long ...
- 安装部署ELK系统监控Azure China的NSG和WAF Log
ELK是一个开源的产品,其官网是:https://www.elastic.co/ ELK主要保护三个产品: Elasticsearch:是基于 JSON 的分布式搜索和分析引擎,专为实现水平扩展.高可 ...