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 ...
随机推荐
- 树、递归、广度优先搜索(BFS)————二叉树的最小深度
解法一:递归 遇到叶子节点不递归,否则接着往子树递归,每次递归层数加1 要确定的是,一定要保证初始输入的节点是有子节点的.因为可能出现只有单子树的情况,所以要先确认这种情况. 具体过程: 1.分析初始 ...
- echarts的下载为excel小工具
echarts自带有下载为图片的功能,但是没有下载为excel表格的功能,个人觉得下载为表格更为需要,所以写了个工具,此工具应用有局限性,只能用于柱形图和折线图,option的series要是如下形式 ...
- BZOJ 2244 [SDOI2011]拦截导弹 (三维偏序CDQ+线段树)
题目大意: 洛谷传送门 不愧为SDOI的duliu题 第一问?二元组的最长不上升子序列长度?裸的三维偏序问题,直接上$CDQ$ 由于是不上升,需要查询某一范围的最大值,并不是前缀最大值,建议用线段树实 ...
- os.getcwd()函数的用法
获得当前路径 在Python中可以使用os.getcwd()函数获得当前的路径. 其原型如下所示: os.getcwd() 该函数不需要传递参数,它返回当前的目录.需要说明的是,当前目录并不是指脚本所 ...
- php $_SERVER['PHP_SELF']安全漏洞
REQUEST_URI 返回的是包括后面数据串的地址,如 index.php?str=1234 PHP_SELF 是 index.php ------------------------------- ...
- web_custom_request函数做get接口测试
最近研究了使用loadrunner做接口测试,刚开始一直不成功,后来加了QQ群,遇到大神了,经指导终于成功 下面是具体实例代码: //{"signIOS":1,"sign ...
- Nutch命令大全
Nutch采用了一种命令的方式进行工作,其命令可以是对局域网方式的单一命令也可以是对整个Web进行爬取的分步命令.主要的命令如下: 1. Crawl Crawl是"org.apache.nu ...
- Hive分区表与分桶
分区表 在Hive Select查询中.通常会扫描整个表内容,会消耗非常多时间做不是必需的工作. 分区表指的是在创建表时,指定partition的分区空间. 分区语法 create table tab ...
- poj3071之概率DP
Football Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2667 Accepted: 1361 Descript ...
- Android技术归档
各位小伙伴们.以后小巫的一些开源码都会上传到github中,所以欢迎大家Follow https://github.com/devilWwj 基于眼下我基本的技术领域在Android上,以后关于And ...