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. hql 条件查询 返回空的一种情况

    为何会出现查询为空,但是查询整个表没问题的情况呢? 这里是没有分清字符串和变量 原来写的是, String hql = "from ClientInfoModel where clientI ...

  2. SpringCloud微服务实战一:Spring Cloud Eureka 服务发现与注册中心(高可用实列为两个注册中心)

    微服务架构: 微服务架构的核心思想是,一个应用是由多个小的.相互独立的.微服务组成,这些服务运行在自己的进程中,开发和发布都没有依赖.不同服务通过一些轻量级交互机制来通信,例如 RPC.HTTP 等, ...

  3. Tomcat7 Redis Session共享

    1.环境 服务器 centos7 tomcat 7 redis nginx 2.配置tomcat配置文件context.xml <Valve className="com.orange ...

  4. opencv 图像基本操作

    目录:读取图像,获取属性信息,图像ROI,图像通道的拆分和合并 1.  读取图像 像素值返回:直接使用坐标即可获得, 修改像素值:直接通过坐标进行赋值 能用矩阵操作,便用,使用numpy中的array ...

  5. Swift 之数组与字典

    http://www.cocoachina.com/swift/20151230/14802.html 说到数组和字典,只要是编过程的小伙伴并不陌生.在Swift中的数组与字典也有着一些让人眼前一亮的 ...

  6. PHP基础-生成静态html页面原理是怎样

    设置example.html为模板文件,然后按照此模板文件生成article-1.html~article-5.html,以此来做简单的演示,代码如下: <?php//将数据存入二维数组$con ...

  7. op应用:官方,wifidog,portal,uci,luci,脚本,框架,usb

    http://wiki.openwrt.org/doc/starthttp://downloads.openwrt.org/docs/buildroot-documentation.htmlhttp: ...

  8. jquery鼠标悬停突出显示

    在线演示 本地下载

  9. iOS从零开始 Code Review

    http://www.cocoachina.com/ios/20151117/14208.html 这篇帖子不是通篇介绍Code Review的方法论, 而是前大段记录了我们团队怎么从没有这个习惯到每 ...

  10. windows和linux下读取文件乱码的终极解决办法!

    乱码是个很恶心的问题. windows和linux读取txt文件,一旦读取了,编码发生改变,就无法再还原了,只有重启项目. 网上有很多方法都是读取文件头,方法很好,但是亲测都不能用(右移8位判断0xf ...