题目链接: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 最短路应用(差分约束)的更多相关文章

  1. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  2. POJ 3159 Candies(spfa、差分约束)

    Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...

  3. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  4. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  5. Candies POJ - 3159 (最短路+差分约束)

    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...

  6. POJ 3159 Candies(差分约束+最短路)题解

    题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...

  7. POJ 3159 Candies(SPFA+栈)差分约束

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

  8. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  9. 图论--差分约束--POJ 3159 Candies

    Language:Default Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 43021   Accep ...

  10. 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 ...

随机推荐

  1. ZBrush软件特性之Marker标记调控板

    在ZBrush®中使用Marker标记调控板来记忆物体属性,因此能在任何时间回到标记并使用它给其他物体或改变物体作为参考点. ZBrush软件下载:http://pan.baidu.com/s/1sl ...

  2. LeetCode Golang 单向链表相加 反向实现

    LeetCode 两数之和, 反向实现 1 -> 2 -> 3 -> 4 +                  3 -> 4 ------------------------- ...

  3. linux防火墙查看状态firewall、iptable

    一.iptables防火墙1.基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service ...

  4. 加减法计算器-java

    由于经常进行较大数据的加减法计算,好多计算器都是转换成科学技术法的,所以自己用java写了一个 功能如下: 1,可以输入多个带千位分隔符的数字,进行加减法计算 2,结果展示带千位分隔符 3,结果展示不 ...

  5. python 递归算阶乘 (转载)

    Python 递归函数 递归函数在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘 n! = 1 * 2 * 3 * ... * n,用函数 ...

  6. python中try…except的使用,处理程序异常

    通常情况下,在python中运行程序,多多少少会出现程序异常的问题,try……except能很好的解决程序中的异常.以下是其用法,在不同位置时进行什么样的工作和起到什么样的作用. try: 可能出现异 ...

  7. git与github的连接流程

    https://blog.csdn.net/sssssuuuuu666/article/details/78565381 https://www.cnblogs.com/wzd5230/p/49064 ...

  8. 使用uglifyjs压缩JS

    一般vue项目完成打包以后需要优化,特别是首次打开加载速度们,webpack打包以后js文件体积很大等方法,可以用这个方法来压缩js文件 安装node.js 安装当前应用 -- uglifyjs 如何 ...

  9. DQL命令(查询)

     select *或字段1,字段2...     from 表名     [where 条件]       提示:*符号表示取表中所有列:没有where语句表示        查询表中所有记录:有wh ...

  10. UVALive 2664 One-way traffic

    One-way traffic Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Or ...