poj 3159 Candies(dijstra优化非vector写法)
题目链接:http://poj.org/problem?id=3159
题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。
最后求n比1最多多多少糖果。
可以从条件中的
B-A<=c及B<=A+c最后要达成这个条件就是要当B>A+c时B=A+c即可
所以差不多就是求最短路。这题中还有一些优化比如
if(vis[u]) continue;这个避免了u点的重复查找
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <cstdio>
#define inf 0X3f3f3f3f
using namespace std;
const int M = 2e5;
int n , m , a , b , c , dis[30010];
struct TnT {
int u , v , w , next;
}T[M];
struct qnode{
int v , c;
qnode(int v , int c):v(v) , c(c){}
bool operator < (const qnode &r) const{
return c > r.c;
}
};
int head[30010] , e;
void add(int u , int v , int w) {
T[e].v = v;
T[e].w = w;
T[e].next = head[u];
head[u] = e++;
}
bool vis[M];
void dij(int s) {
priority_queue<qnode>q;
memset(vis , false , sizeof(vis));
q.push(qnode(s , 0));
dis[s] = 0;
while(!q.empty()) {
int u = q.top().v;
q.pop();
if(vis[u])
continue;
vis[u] = true;
for(int i = head[u] ; i != -1 ; i = T[i].next) {
int v = T[i].v , w = T[i].w;
if(!vis[v] && dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
q.push(qnode(v , dis[v]));
}
}
}
}
int main() {
while(scanf("%d%d" , &n , &m) != EOF) {
e = 0;
for(int i = 1 ; i <= n ; i++) {
dis[i] = inf;
head[i] = -1;
}
for(int i = 1 ; i <= m ; i++) {
scanf("%d%d%d" , &a , &b , &c);
add(a , b , c);
}
dij(1);
printf("%d\n" , dis[n]);
}
return 0;
}
poj 3159 Candies(dijstra优化非vector写法)的更多相关文章
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- poj 3159 Candies (dij + heap)
3159 -- Candies 明明找的是差分约束,然后就找到这题不知道为什么是求1~n的最短路的题了.然后自己无聊写了一个heap,518ms通过. 代码如下: #include <cstdi ...
- POJ 3159 Candies (栈优化spfa)
Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description During the kinderga ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- poj 3159 Candies dijkstra + queue
题目链接: http://poj.org/searchproblem 题目大意: 飞天鼠是班长,一天班主任买了一大包糖果,要飞天鼠分发给大家,班里面有n个人,但是学生A认为学生B比自己多的糖果数目不应 ...
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
- POJ 3159 Candies(差分约束)
http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...
- (简单) POJ 3159 Candies,Dijkstra+差分约束。
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
随机推荐
- codeforces 576 div2 A-D题解
A题 Description 题目链接: https://codeforces.com/contest/1199/problem/A 题意: 给定长度为n(1≤n≤100000)的一个序列a,以及两个 ...
- leetcode并发题目解题报告JAVA版
一.Print in Order Suppose we have a class: public class Foo { public void first() { print("first ...
- spring cloud stream 经验总结
---恢复内容开始--- 基本概念 spring: cloud: stream: kafka: binder: brokers: cloudTest:19092 zk-nodes: cloudTest ...
- Compatibility模式安装windows7后改为AHCI模式无法启动Windows7的解决办法
在用Compatibility模式安装Windows 7后,再在BIOS中去开启SATA硬盘的AHCI功能的话,就会出现无法启动的情况.只有改回Compatibility模式后,系统才恢复正常.经过试 ...
- CodeForces 29D Ant on the Tree
洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译. 这题有\(\bm3\)种解法,但只有一种是正解(这不是废话嘛). 方法\(\bm1\):最近公共祖先LCA(正 ...
- 【原创】微信小程序支付java后台案例(公众号支付同适用)(签名错误问题)
前言 1.微信小程序支付官方接口文档:[点击查看微信开放平台api开发文档]2.遇到的坑:预支付统一下单签名结果返回[签名错误]失败,建议用官方[签名验证工具]检查签名是否存在问题.3.遇到的坑:签名 ...
- vscode 配置 nodejs 开发环境
1.配置 cnpm 镜像 (国内淘宝镜像网速更快) npm install -g cnpm --registry=https://registry.npm.taobao.org 2.配置智能提示 安装 ...
- JS数据结构第五篇 --- 二叉树和二叉查找树
一.二叉树的基本概念 从逻辑结构角度来看,前面说的链表.栈.队列都是线性结构:而今天要了解的“二叉树”属于树形结构. 1.1 多叉树的基本概念,以上图中“多叉树”为例说明 节点:多叉树中的每一个点都叫 ...
- 零拷贝Zero copy-linux and java
背景-几种拷贝方式 方式1:Copying in Two Sample System Calls read(file, tmp_buf, len); write(socket, tmp_buf, le ...
- Python模块之ncclient
一.简介 此模块是是netconf协议的客户端,可与netconf服务端进行交互 二.实验环境 1.操作系统:win10 2.python版本:python3.6.6 3.ncclient模块版本:0 ...