POJ-3159-Candies-(差分约束,Dijkstra)
链接:https://vjudge.net/problem/POJ-3159
题意:
N个小孩,M个约束
以A,B,C给出。即小孩B的糖果不能比A多C以上。
思路:
差分约束:
若有 A-B <= V1,B-C <= V2,即A-C <= V1+V2。
用最短路求法求。
A->B = V1,B->C=V2,A->C=V3,则有A->C = min(A->C,(A->B+B->C))。
代码:
#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
const int MAXN = 150000+10;
const long long INF = 99999999;
typedef long long LL;
//邻接表存图优化Dijkstra算法
struct Node
{
int _w;
long long _v;
bool operator < (const Node & that)const
{
return this->_v > that._v;
}
Node(int w,int v):_w(w),_v(v){}
};
int First[MAXN],Next[MAXN];
int u[MAXN],v[MAXN],w[MAXN]; int Dis[MAXN];
int Vis[MAXN];
int n,m; void Read_Graph()
{
scanf("%d%d",&n,&m);
for (int e = 1;e <= m;e++)
{
scanf("%d%d%d",&u[e],&v[e],&w[e]);
}
} void Init()
{
for (int i = 1;i<=m;i++)
First[i] = -1;
for (int e = 1;e <= m;e++)
{
Next[e] = First[u[e]];
First[u[e]] = e;
}
} int Dijkstra()
{
for (int i = 1;i <= n;i++)
{
Dis[i] = INF;
Vis[i] = 0;
}
Dis[1] = 0;
priority_queue<Node> que;
que.push(Node(1,0));
while (!que.empty())
{
if (Vis[que.top()._w] == 1)
que.pop();
else
{
int e = que.top()._w;
Vis[e] = 1;
e = First[e];
while (e != -1)
{
if (Dis[v[e]] > Dis[u[e]] + w[e])
{
Dis[v[e]] = Dis[u[e]] + w[e];
que.push(Node(v[e],Dis[v[e]]));
}
e = Next[e];
}
que.pop();
}
}
return Dis[n];
} int main()
{
Read_Graph();
Init();
cout << Dijkstra() << endl; return 0;
}
POJ-3159-Candies-(差分约束,Dijkstra)的更多相关文章
- [poj 3159]Candies[差分约束详解][朴素的考虑法]
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...
- 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 ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- (简单) POJ 3159 Candies,Dijkstra+差分约束。
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
- POJ 3159 Candies 【差分约束+Dijkstra】
<题目链接> 题目大意: 给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c .最后求n 比 1 ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- 图论--差分约束--POJ 3159 Candies
Language:Default Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 43021 Accep ...
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
随机推荐
- Ubuntu 12.04安装grub2过程中出错怎么办【转】
本文转载自:https://zhidao.baidu.com/question/491448169666671012.html 其实是可以不用优盘启动的.但使用优盘没有风险.你的系统虽然出现==不能安 ...
- LightOJ1336 Sigma Function —— 质因子分解、约数和为偶数
题目链接:https://vjudge.net/problem/LightOJ-1336 1336 - Sigma Function PDF (English) Statistics Forum ...
- selenium使用笔记(一)——selenium你该知道的
有时候在交流群里经常会看到这样的问题,selenium能进行性能测试吗?selenium1和selenium2有什么区别等等问题,在这里谈一下自己学习和工作以后对selenium的认识.我所记录的东西 ...
- 【Redis笔记(四)】 Redis数据结构 - list链表
原创作品,转载请标明:http://blog.csdn.net/Xiejingfa/article/details/50573605 经过前面的介绍,我们学习了Redis中string字符串.hash ...
- 上传图片 ajax input type="file" 兼容 ie chroem 火狐
上传图片,转载请注明出处!!! 兼容 ie chroem 火狐 html: <div id="uploadForm"> <input id="file& ...
- python pickle/cPickle模块
序列化(picking): 把变量从内存中变成可存储或传输的过程称为序列化,序列化之后,就可以把序列化的对象写入磁盘,或者传输给其他设备; 反序列化(unpickling):相应的,把变量的内容从序列 ...
- Android-Styles and Themes [From API Guide]
This blog was opened 5 months ago and it has 57 posts now,but the poor thing is by now no one has co ...
- codevs1068乌龟棋
codevs1068乌龟棋 1068 乌龟棋 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamon 题目描述 Descrip ...
- vue 表单验证省市县三联动
<el-col :span="24"> <el-form-item label="所在地区" prop="region" ...
- bzoj 4260 Codechef REBXOR——trie树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4260 一段的异或和就是两个前缀的异或和.正反扫两边,用trie树算每个位置为左/右端点时最大 ...