POJ 3159 Candies(差分约束+最短路)题解
题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少
思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa的松弛条件相对应,所以我们建一条a到b的边,权值c,然后跑最短路,求出所有差值最大的那个即为答案。应该算是基础的线性差分约束题。
ps:队列超时,这里用栈。
关于差分约束可以点这里
#include<cstdio>
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = +;
const int INF = 0x3f3f3f3f;
struct Edge{
int v,cost,next;
}edge[];
int head[maxn],tot;
void init(){
tot = ;
memset(head,-,sizeof(head));
}
void addEdge(int u,int v,int cost){
edge[tot].v = v;
edge[tot].cost = cost;
edge[tot].next = head[u];
head[u] = tot++;
}
bool vis[maxn];
int cnt[maxn];
int dist[maxn];
bool spfa(int start,int n){
memset(vis,false,sizeof(vis));
for(int i = ;i <= n;i++)
dist[i] = INF;
vis[start] = true;
dist[start] = ;
stack<int> q;
while(!q.empty()) q.pop();
q.push(start);
memset(cnt,,sizeof(cnt));
cnt[start] = ;
while(!q.empty()){
int u = q.top();
q.pop();
vis[u] = false;
for(int i = head[u];i != -;i = edge[i].next){
int v = edge[i].v;
if(dist[v] > dist[u] + edge[i].cost){
dist[v] = dist[u] + edge[i].cost;
if(!vis[v]){
vis[v] = true;
q.push(v);
if(++cnt[v] > n) return false;
}
}
}
}
return true;
}
int main(){
int n,m;
init();
int a,b,c; // b - a <= c : when b > a + c then change
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c);
}
spfa(,n);
int Max = ;
for(int i = ;i <= n;i++){
if(dist[i] < INF) Max = max(Max,dist[i]);
}
printf("%d\n",Max);
return ;
}
POJ 3159 Candies(差分约束+最短路)题解的更多相关文章
- 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[差分约束详解][朴素的考虑法]
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...
- 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(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- 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 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- 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 ...
随机推荐
- Maven常用操作
1. 修改Maven的本地仓库路径 1.1 默认会放在~/.m2/repository目录下 (“~”代表用户的目录,比如windows下一般都是C:\Documents and Settings\[ ...
- 安装Memcache的PHP扩展
查看memcached是否启动 # ps -ef | grep memcached 安装Memcache的PHP扩展 1.在http://pecl.php.net/package/memcache 选 ...
- Thrift 入门之helloWorld
不多说,先看项目结构 首先先编写一个hello.thrift的文件 hello.thrift namespace java sawshaw service HelloService { string ...
- angular -- 无刷新做分页
无刷新做分页参考地址: http://www.jq22.com/demo/angular201707111100/ 示例代码: <!DOCTYPE html> <html lang= ...
- 微信小程序 --- if/else条件渲染
if 条件渲染:当为真的时候显示,当为假的时候隐藏: else 条件渲染:当为真的时候隐藏,当为假的时候显示: <view wx:if="{{true}}">{{tex ...
- php 常用的系统函数
字符串函数 strlen:获取字符串长度,字节长度 substr_count 某字符串出现的次数 substr:字符串截取,获取字符串(按照字节进行截取) mb_strlenmb_substr str ...
- 【ES6】001---module模块------【巷子】
001.前言 什么是模块化开发? 为了提高代码的复用度,开发人员会按照功能把大量的js代码分成若干文件,这样在多个页面就可以使用同一个文件了.,下面是某个网站的js引用情况 怎么会有这么多js文件 ...
- CommonHelper 公共类
public static class CommonHelper 公共帮助类 using System.Collections.Generic; using System.Linq; using ...
- mysql-blog
https://www.cnblogs.com/zhanht/p/5450559.html
- Django - Cookie、Session、自定义分页和Django分页器
2. 今日内容 https://www.cnblogs.com/liwenzhou/p/8343243.html 1. Cookie和Session 1. Cookie 服务端: 1. 生成字符串 2 ...