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 ...
随机推荐
- Linux 启动文件、设置环境变量的位置
系统级启动文件 ==================================== 1./etc/rc 主启动文件,不要修改它 2./etc/rc.conf 决定启动哪些系统自带的守护进程 ...
- JS-随机div颜色
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- DOM操作的性能问题
造成DOM操作性能差的原因:1.DOM操作的实现和ECMAscript的实现是两个独立的部分,之间通过接口来完成相应的DOM操作. 2.实时查询文档得到HTML集合,重复执行查询操作.特别是lengt ...
- 【BZOJ3939】[Usaco2015 Feb]Cow Hopscotch 动态规划+线段树
[BZOJ3939][Usaco2015 Feb]Cow Hopscotch Description Just like humans enjoy playing the game of Hopsco ...
- eclipse项目更换svn共享库
eclipse项目更换svn共享库 参考内容: http://blog.csdn.net/yang5726685/article/details/59111586 已经共享过的svn项目,更换资源库时 ...
- windows服务的默认启动类型和登录帐户
转自:http://www.winhelponline.com/blog/windows-7-services-default-startup-type/ Service Name Startup T ...
- Swift - 获取状态栏一些信息
// 获取状态栏的各种信息 :网络类型,运营商,电池电量,显示的系统时间等信息 import UIKit enum NetWorkType { case NetworkStatesNone // 没有 ...
- iPad - 开发(Universal Applications)
一.iPad 1.判断是否在iPad上 BOOL iPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdi ...
- xpath定位方法小结(转载)
1.实例化一个浏览器WebDriver driver = new FirefoxDriver(); 2.driver.get() get传参数到浏览器中 3.常用定位方法webelement XX=d ...
- 【vue】---Object.defineProperty基本使用---【巷子】
1.object.defineProperty 给一个对象定义一个新的属性或者在修改一个对象现有的属性,并返回这个对象 语法: Object.defineProperty(参数1,参数2,参数3) 参 ...