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 ...
随机推荐
- hook的本质就是在本原可执行文件中加东西
hook的本质就是在本原可执行文件中加东西. 本质就是添加东西:
- Python内置数据结构之字典dict
1. 字典 字典是Python中唯一的内置映射类型,其中的值不按顺序排列,而是存储在键下.键可能是数(整数索引).字符串或元组.字典(日常生活中的字典和Python字典)旨在让你能够轻松地找到特定的单 ...
- day13 基本的文件操作(好东西)
目录 基本的文件处理 什么是文件 如何使用文件 使用Python写一个小程序控制文件 open(打开文件) read: readline:一次性读取一行 del:删除 close:关闭 write(写 ...
- Python的流程控制
条件判断 通过`if`,`elif`,`else`关键字来实现条件判断逻辑的实现,执行改结构中的其中一个,其结构如下: if condition1: pass elif condition2: pas ...
- 小程序--wepy省市区三级联动选择
产品老哥对项目再一次进行关爱, 新增页面, 新增需求, 很完美........ 不多说, 记录一下新增东西中的省市区联动选择, (这里全国地区信息是在本地, 但不建议这么做, 因为js文件太大.. 建 ...
- docker删除docker_gwbridge网桥
最后更新时间:2018年12月26日 使用命令:docker network rm docker_gwbridge 提示无法删除. [root@localhost ~]# docker network ...
- html全屏显示
JavaScript代码: function toggleFullScreen() { if (!document.fullscreenElement && // alternativ ...
- React和Vue中,是如何监听变量变化的
React 中事件监听 本地调试React代码的方法 先将React代码下载到本地,进入项目文件夹后yarn build 利用create-react-app创建一个自己的项目 把react源码和自己 ...
- elementUI 日期时间选择器el-date-picker开始时间与结束时间约束
主要思路:el-date-picker组件需要 :picker-options属性,属性值为data,data的数据来自于methods中的方法. ##template代码 <el-form-i ...
- NYIST 489 哭泣天使
哭泣天使时间限制:1000 ms | 内存限制:65535 KB难度:5 描述Doctor Who乘着Tardis带着Amy来到了一个星球,一开Tadis大门,发现这个星球上有个壮观的石像群,全是一些 ...