POJ3159 Candies —— 差分约束 spfa
题目链接:http://poj.org/problem?id=3159
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 33576 | Accepted: 9422 |
Description
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.
snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?
Input
The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.
Output
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
Sample Input
2 2
1 2 5
2 1 4
Sample Output
5
Hint
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 2e5+; int n, m; struct edge
{
int to, w, next;
}edge[maxn];
int cnt, head[maxn]; void addedge(int u, int v, int w)
{
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
} void init()
{
cnt = ;
memset(head, -, sizeof(head));
} int dis[maxn], times[maxn], inq[maxn];
void spfa(int st)
{
memset(inq, , sizeof(inq));
memset(times, , sizeof(times));
for(int i = ; i<=n; i++)
dis[i] = INF; stack<int>S;
S.push(st);
inq[st] = ;
dis[st] = ;
while(!S.empty())
{
int u = S.top();
S.pop(); inq[u] = ;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v] = dis[u]+edge[i].w;
if(!inq[v])
{
S.push(v);
inq[v] = ;
}
}
}
}
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
init();
for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addedge(u,v,w);
} spfa();
printf("%d\n", dis[n]);
}
}
POJ3159 Candies —— 差分约束 spfa的更多相关文章
- POJ-3159.Candies.(差分约束 + Spfa)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 40407 Accepted: 11367 Descri ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- poj3159 Candies(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Candies Time Limit: 1500MS Memory Limit ...
- [poj3159]Candies(差分约束+链式前向星dijkstra模板)
题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...
- 【poj3169】【差分约束+spfa】
题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- POJ 3159 Candies(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
- POJ——3159Candies(差分约束SPFA+前向星+各种优化)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 28071 Accepted: 7751 Descrip ...
随机推荐
- hadoop格式化出错,提示IO异常
配置好hadoop之后,在进行格式化的时候出现异常,原因是由于在core-site.xml 配置文件中写的路径格式不对. 不需要加 file:/ 或者 file:// 直接写绝对路径就行. <c ...
- CSU 1307 最短路+二分
题目大意: 帮忙找到一条a到b的最短路,前提是要保证路上经过的站点的最大距离尽可能短 这道题居然要用到二分...完全没去想过,现在想想求最大距离的最小值确实是... 这里不断二分出值代入spfa()或 ...
- [luoguP1352] 没有上司的舞会(DP)
传送门 树上的dp,从底向上dp就行. 设dp[u][0]表示不选节点 u 的最大值,dp[u][1]表示选节点 u 的最大值. 则状态转移方程为: dp[u][0] = ∑max(dp[v][1], ...
- Atlantis(hdu1542)
题意:求n个矩阵的面积并. /* 线段树维护扫描线 把每个矩形看成两条线段,从左到右添加线段,如果是矩形左边的线段,那就给线段所在的区间(y值)cover+1,反之则cover-1. 并且如果这条线段 ...
- php统计图类库JpGraph
php统计图类库JpGraph JpGraph官网地址:https://jpgraph.net/. (1)下载类库: 下载地址:https://jpgraph.net/download/. 选择版本, ...
- HDU3549 最大流 裸题
EK算法 时间复杂度o(n*m*m) 因为有反向边每次bfs时间为 n*m 每次删一条边 最多m次 代码 #include<iostream> #include<string.h& ...
- [bzoj3998][TJOI2015]弦论_后缀自动机
弦论 bzoj-3998 TJOI-2015 题目大意:给定一个字符串,求其$k$小子串. 注释:$1\le length \le 5\cdot 10^5$,$1\le k\le 10^9$. 想法: ...
- loj6157 A^B Problem (并查集)
题目: https://loj.ac/problem/6157 分析: 这种树上异或,一般是采用分位考虑,但是这题即使分位,也会发现非常不好处理 这里考虑维护一个点到其根的路径的异或值 用并查集去检测 ...
- babel 用法及其 .babelrc 的配置详解,想做前端架构,拒绝一知半解...
Babel 官方介绍:将 ECMAScript 2015 及其版本以后的 javascript 代码转为旧版本浏览器或者是环境中向后兼容版本的 javascript 代码. 简而言之,就是把不兼容的 ...
- 《javascript设计模式》读书笔记二(封装和隐藏信息)
1.为什么要封装和信息隐藏 做过编程的朋友们知道"耦合"这个词.事实上封装的效果就是为了解耦,让类和类之间没有太多的联系,防止某一天改动某一类的时候,产生"多米骨诺牌效应 ...