3159 -- Candies

  明明找的是差分约束,然后就找到这题不知道为什么是求1~n的最短路的题了。然后自己无聊写了一个heap,518ms通过。

代码如下:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
const int M = ;
struct Edge {
int t, nx, c;
} edge[M];
int eh[N], ec; void init() {
memset(eh, -, sizeof(eh));
ec = ;
} void addedge(int s, int t, int c) {
edge[ec].t = t, edge[ec].nx = eh[s], edge[ec].c = c;
eh[s] = ec++;
} struct Heap {
int val[N], pos[N], id[N], sz, tmp, d;
void init() {
sz = ;
memset(pos, -, sizeof(pos));
}
int up(int t) {
while (t > && val[t] < val[t >> ]) pos[id[t] = id[t >> ]] = t, swap(val[t], val[t >> ]), t >>= ;
return t;
}
void push(int i, int v) {
val[++sz] = v;
pos[i] = up(sz);
id[pos[i]] = i;
}
void down(int tmp) {
while ((tmp << ) <= sz) {
if (val[tmp] <= val[tmp << ] && val[tmp] <= val[tmp << | ]) break;
if ((tmp << == sz) || val[tmp << ] <= val[tmp << | ]) {
d = tmp << ;
swap(pos[id[tmp]], pos[id[d]]);
swap(id[tmp], id[d]);
swap(val[tmp], val[d]);
tmp = d;
} else {
d = tmp << | ;
swap(pos[id[tmp]], pos[id[d]]);
swap(id[tmp], id[d]);
swap(val[tmp], val[d]);
tmp = d;
}
}
}
void modify(int i, int v) {
val[pos[i]] = v;
down(pos[i]);
pos[i] = up(pos[i]);
id[pos[i]] = i;
}
void pop(int &i) {
if (sz == ) {
i = -;
return ;
}
i = id[];
pos[i] = -;
if (sz == ) { sz--; return ;}
val[] = val[sz];
id[] = id[sz];
pos[id[]] = ;
sz--;
if (sz > ) down();
}
int gv(int i) { return ~pos[i] ? val[pos[i]] : -;}
} hp;
int dis[N]; int dij(int s, int t) {
int cur, i;
hp.init();
memset(dis, , sizeof(dis));
dis[s] = ;
hp.push(s, );
while (hp.sz) {
hp.pop(cur);
if (cur == t) return dis[cur];
for (i = eh[cur]; ~i; i = edge[i].nx) {
Edge &e = edge[i];
if (dis[e.t] > dis[cur] + e.c) {
dis[e.t] = dis[cur] + e.c;
if (~hp.gv(e.t)) hp.modify(e.t, dis[e.t]);
else hp.push(e.t, dis[e.t]);
}
}
}
return -;
} int main() {
int x, y, v;
int n, m;
while (~scanf("%d%d", &n, &m)) {
init();
while (m--) {
scanf("%d%d%d", &x, &y, &v);
addedge(x, y, v);
}
printf("%d\n", dij(, n));
}
return ;
}

——written by Lyon

poj 3159 Candies (dij + heap)的更多相关文章

  1. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

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

  3. poj 3159 Candies(dijstra优化非vector写法)

    题目链接:http://poj.org/problem?id=3159 题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的 ...

  4. POJ 3159 Candies(SPFA+栈)差分约束

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

  5. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  6. POJ 3159 Candies(差分约束)

    http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...

  7. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  8. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  9. Invitation Cards POJ 1511 SPFA || dij + heap

    http://poj.org/problem?id=1511 求解从1去其他顶点的最短距离之和. 加上其他顶点到1的最短距离之和. 边是单向的. 第一种很容易,直接一个最短路, 然后第二个,需要把边反 ...

随机推荐

  1. 大半夜吃饱了撑的帮人调IE玩

      那高手的也是IE6,我也是IE6,但是他的IE6就总是进recv,我的IE6就进WSARecv,一点都不科学...擦..不调了.

  2. 转: CentOS上安装LAMP之第一步:Apache环境及安装过程报错解决方案(纯净系统环境)

    传送门:http://blog.csdn.net/zhangatle/article/details/77416996  小心坑!填完就懂怎么安装了 Note:要从零开始搭建,就不要嫌中间遇到各种问题 ...

  3. sublime3安装javascript控制台环境 方法2

    1.下载node.js sublimeText3- 安装nodejs插件   ctrl+shift+p — install package —nodejs 2.修改配置文件 在 Sublie Text ...

  4. LintCode_469 等价二叉树

    题目 检查两棵二叉树是否等价.等价的意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数都相等. 样例 1 1 / \ / \ 2 2 and 2 2 / / 4 4 就是两棵等 ...

  5. ObjectIntputStream / ObjectOutputStream 类

    使用 ObjectInputStream 类 ObjectOutputStream类 将对象整体 读 / 写文件中: [读写对象] 1. ObjectInputStream类 (1)基本概念 java ...

  6. 【django后端分离】mysql原生查询命令后,RawQueryset类型的自定义序列化返回json格式

    1:设置mysql原生分页 # 监控系统首页显示分页 def MyPagination(limitid,offsetid): limitid =str(limitid) offsetid =str(o ...

  7. Springboot项目下mybatis报错:Invalid bound statement (not found)

    mybatis报错:Invalid bound statement (not found)的原因很多,但是正如报错提示一样,找不到xml中的sql语句,报错的情况分为三种: 第一种:语法错误 Java ...

  8. 手把手0基础项目实战(一)——教你搭建一套可自动化构建的微服务框架(SpringBoot+Dubbo+Docker+Jenkins)...

    原文:手把手0基础项目实战(一)--教你搭建一套可自动化构建的微服务框架(SpringBoot+Dubbo+Docker+Jenkins)... 本文你将学到什么? 本文将以原理+实战的方式,首先对& ...

  9. vue-waterfall2 基于Vue.js 瀑布流组件

    vue-waterfall2 1.宽度自适应,数据绑定特效(适用于上拉加载更多) 2.自定义程度高 3.使用极为简便,适用于PC/移动端 4.提供resize(强制刷新布局-适用于下拉刷新)/mix( ...

  10. Microsoft: Get started with Dynamic Data Masking in SQL Server 2016 and Azure SQL

    Dynamic Data Masking (DDM) is a new security feature in Microsoft SQL Server 2016 and Azure SQL DB. ...