POJ-3159 Candies 最短路应用(差分约束)
题目链接:https://cn.vjudge.net/problem/POJ-3159
题意
给出一组不等式
求第一个变量和最后一个变量可能的最大差值
数据保证有解
思路
一个不等式a-b<=c,通过移项,实际上就是满足了a<=b+c
发现在整个约束系统中,a在下满足不等式的情况下求最大值,就是在求最短路
然而如果直接用BellmanFord(spfa)的话,还是会超时
这时得对Bellman做第二次优化,用stack代替queue
但是对于更多的图中,Dijsktra依然更优,所以没有必要太过考虑这个问题?
代码
Dijkstra
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=3e4+20, maxm=15e4+20, INF=0x3f3f3f3f;
typedef pair<int, int> Node;
struct Cmp{
bool operator () (const Node &a, const Node &b){
return a.first>b.first;
}
};
struct Edge{
int to, dis, next;
}edges[maxm+5];
int head[maxn+5], size=0;
void addEdge(int from, int to, int dis){
edges[size]=Edge{to, dis, head[from]};
head[from]=size++;
}
void init(void){
memset(head, -1, sizeof(head));
size=0;
}
int Bellman(int n){
int dist[maxn+5], sta[maxn+5], top=0;//cnt[maxn+5];
bool inq[maxn+5]={false};
// queue<int> que;
memset(dist, INF, sizeof(dist)); dist[1]=0;
sta[top++]=1;
while (top!=0){
int from=sta[--top];
inq[from]=false;
for (int i=head[from]; i!=-1; i=edges[i].next){
Edge &e=edges[i];
int &to=e.to, &dis=e.dis;
if (dist[to]<=dist[from]+dis) continue;
dist[to]=dist[from]+dis;
if (inq[to]) continue;
sta[top++]=to; inq[to]=true;
}
}return dist[n];
}
int Dij(int n){
int dist[maxn+5];
priority_queue<Node, vector<Node>, Cmp> que;
memset(dist, INF, sizeof(dist)); dist[1]=0;
que.push(Node(dist[1], 1));
while (que.size()){
Node x=que.top(); que.pop();
if (x.first!=dist[x.second]) continue;
int &from=x.second;
for (int i=head[from]; i!=-1; i=edges[i].next){
Edge &e=edges[i];
int &to=e.to, &dis=e.dis;
if (dist[to]<=dist[from]+dis) continue;
dist[to]=dist[from]+dis;
que.push(Node(dist[to], to));
}
}return dist[n];
}
int main(void){
int n, m, from, to, dis;
init();
scanf("%d%d", &n, &m);
for (int i=0; i<m; i++){
scanf("%d%d%d", &from, &to, &dis);
addEdge(from, to, dis);
}printf("%d\n", Dij(n));//Bellman(n));
return 0;
}
| Time | Memory | Length | Lang | Submitted |
|---|---|---|---|---|
| 532ms | 2568kB | 1960 | G++ | 2018-05-27 00:47:58 |
BellmanFord
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=3e4+20, maxm=15e4+20, INF=0x3f3f3f3f;
struct Edge{
int to, dis, next;
}edges[maxm+5];
int head[maxn+5], size=0;
void addEdge(int from, int to, int dis){
edges[size]=Edge{to, dis, head[from]};
head[from]=size++;
}
void init(void){
memset(head, -1, sizeof(head));
size=0;
}
int Bellman(int n){
int dist[maxn+5], sta[maxn+5], top=0;//cnt[maxn+5];
bool inq[maxn+5]={false};
// queue<int> que;
memset(dist, INF, sizeof(dist)); dist[1]=0;
sta[top++]=1;
while (top!=0){
int from=sta[--top];
inq[from]=false;
for (int i=head[from]; i!=-1; i=edges[i].next){
Edge &e=edges[i];
int &to=e.to, &dis=e.dis;
if (dist[to]<=dist[from]+dis) continue;
dist[to]=dist[from]+dis;
if (inq[to]) continue;
sta[top++]=to; inq[to]=true;
}
}return dist[n];
}
int main(void){
int n, m, from, to, dis;
init();
scanf("%d%d", &n, &m);
for (int i=0; i<m; i++){
scanf("%d%d%d", &from, &to, &dis);
addEdge(from, to, dis);
}printf("%d\n", Bellman(n));
return 0;
}
| Time | Memory | Length | Lang | Submitted |
|---|---|---|---|---|
| 485ms | 2108kB | 1220 | G++ | 2018-05-27 00:39:53 |
POJ-3159 Candies 最短路应用(差分约束)的更多相关文章
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- POJ 3159 Candies(spfa、差分约束)
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- Candies POJ - 3159 (最短路+差分约束)
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...
- POJ 3159 Candies(差分约束+最短路)题解
题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...
- 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(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
- 图论--差分约束--POJ 3159 Candies
Language:Default Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 43021 Accep ...
- 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 ...
随机推荐
- JSP 点击量统计!
详细JSP课程:阿里云大学——开发者课堂 有时候我们需要知道某个页面被访问的次数,这时我们就需要在页面上添加页面统计器,页面访问的统计一般在用户第一次载入时累加该页面的访问数上. 要实现一个计数器,您 ...
- Java开发就业形势和面试技巧
如果从软件编程的就业来讲,如果你现在不懂架构,那么找到一份好工作还是比较难的,但是这里面有两点需要注意: 传统软件公司,这类公司还会使用最为原始的开发技术(SSH),但是这样的传统软件公司的招聘量已经 ...
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...
- 四、分布式 Git(未完待续)
一.分布式工作流程 在 Git 中,每个开发者同时扮演着节点和集线器的角色——也就是说,每个开发者既可以将自己的代码贡献到其他的仓库中,同时也能维护自己的公开仓库,让其他人可以在其基础上工作并贡献代码 ...
- elementUI 日期时间选择器el-date-picker开始时间与结束时间约束
主要思路:el-date-picker组件需要 :picker-options属性,属性值为data,data的数据来自于methods中的方法. ##template代码 <el-form-i ...
- 《一个民企CEO的职场阳谋》–读书总结(下)
职场是一个战场,很多人几十年在这里战斗. 职场是一个熔炉,很多人大半生在这里修炼. 如果在办公室里得不到快乐,生活就不会快乐. 如果公司里头感觉不到幸福,人生就不会幸福.(以上四句来自老刘的博客) & ...
- Vue父子组件之间的通讯(学习笔记)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 基于nginx的静态网页部署
背景: 一序列的html网页需要部署 基于nginx的部署: 本文采用的基于openresty的nginx 配置. 简单地配置 Nginx 的配置文件,以便在启动 Nginx 时去启用这些配置即可实现 ...
- HDU 4333 Contest 4
一开始就想到了扩展KMP,因为只有扩展KMP才是处理后缀的.但忽然短路以为扩展KMP求的是最长公共后缀,囧....又浪费了很多时间,都是对这个算法练得不多 再看那个扩展KMP算法之后,就很确定要的就是 ...
- ACM-ICPC Dhaka Regional 2012 题解
B: Uva: 12582 - Wedding of Sultan 给定一个字符串(仅由大写字母构成)一个字母表示一个地点,经过这个点或离开这个点都输出这个地点的字母) 问: 每一个地点经过的次数(维 ...