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. Linux下安装jboss并设置自启动服务

    一.JDK和JBOSS下载jdk:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...

  2. ZooKeeper的分布式锁实现

    分布式锁一般有三种实现方式: 1. 数据库乐观锁: 2. 基于Redis的分布式锁: 3. 基于ZooKeeper的分布式锁. 本篇博客将介绍第三种方式,基于Zookeeper实现分布式锁.虽然网上已 ...

  3. Vbulletin Used to Show Malicious Advertisements

    In the past, we have seen a massive amount of vBulletin websites compromised through theVBSeo Vulner ...

  4. Laravel(PHP)使用Swagger生成API文档不完全指南 - 基本概念和环境搭建 - 简书

    在PHPer中,很多人听说过Swagger,部分人知道Swagger是用来做API文档的,然而只有少数人真正知道怎么正确使用Swagger,因为PHP界和Swagger相关的资料实在是太少了.所以鄙人 ...

  5. iview 中table列 一列显示多个数据(后台返回数组显示在列内)

    一.首先出现的是比较复杂的一种情况(多个key) 1.首先页面显示效果如下 2.后台返回数据格式如下: 3.在iview中table的columns中的render函数: 4.具体代码 render: ...

  6. 卡特兰数(catalan)总结

    卡特兰数的公式 递推公式1:$f(n)=\sum \limits_{i=0}^{n-1}f(i)*f(n-i-1)$ 递推公式2:$f(n)=\frac{f(n-1)*(4*n-2)}{n+1}$ 组 ...

  7. bzoj1823满汉全席

    2-sat模板 这篇博客写得非常好 传送门 //Achen #include<algorithm> #include<iostream> #include<cstring ...

  8. 深入剖析Redis RDN持久化机制

    rdb是redis保存内存数据到磁盘数据的其中一种方式(另一种是AOF).Rdb的主要原理就是在某个时间点把内存中的所有数据的快照保存一份到磁盘上.在条件达到时通过fork一个子进程把内存中的数据写到 ...

  9. 关于chrome浏览器的帐号密码自动填充以及出现的黄色背景色填充问题

    不知道大家平时做项目的时候有木有关注这个问题,其实之前做项目遇到过类似的问题,但是因为是单独的chrome浏览器的填充,而且是样式问题稍微严重点,也就没在意.然而在近期的项目中有遇到了这个问题,最为一 ...

  10. 删除重复节点 Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...