POJ 3159 Candies (图论,差分约束系统,最短路)

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

Http

POJ:https://vjudge.net/problem/POJ-3159

Source

图论,差分约束系统,最短路径

题目大意

给出n个小朋友和m对小朋友A不希望小朋友B比他多c个,n比1最多能多多少糖果

解决思路

首先我们观察一组情况,A不希望B比他多c个,用数学语言表示就是

\[D[B]-D[a]>=c
\]

我们把式子变一下形就是

\[D[a]+c<=D[b]
\]

没错,这是不是很像最短路的形式?

所以对于每一个A不希望B比他多c个,我们连一条边A->B,权值为c。那么若要求最后的解就求一遍最短路径就可以了。

此题的关键是不能用spfa+queue,会超时,要用spfa+stack

(只要题中说明了不会出现负环,就可以用spfa+stack,其效率高于spfa+queue)

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std; const int maxN=30001;
const int maxM=150001;
const int inf=2147483647; class Graph//把图的相关内容封装到Graph里,方便编写
{
private:
int cnt;
stack<int> S;
int instack[maxN];//使用stack而不是queue作为spfa的容器
int Head[maxN];
int V[maxM];
int W[maxM];
int Next[maxM];
public:
int Dist[maxN];
void init()
{
memset(Head,-1,sizeof(Head));
memset(Next,-1,sizeof(Next));
cnt=0;
}
void Add_Edge(int u,int v,int w)
{
cnt++;
Next[cnt]=Head[u];
V[cnt]=v;
W[cnt]=w;
Head[u]=cnt;
}
void spfa()
{
memset(Dist,127,sizeof(Dist));
memset(instack,0,sizeof(instack));
while (!S.empty())
S.pop();
Dist[1]=0;
instack[1]=1;
S.push(1);
do
{
int u=S.top();
S.pop();
instack[u]=0;
for (int i=Head[u];i!=-1;i=Next[i])
{
if (Dist[V[i]]>Dist[u]+W[i])
{
Dist[V[i]]=Dist[u]+W[i];
if (instack[V[i]]==0)
{
S.push(V[i]);
instack[V[i]]=1;
}
}
}
}
while (!S.empty());
}
}; int n,m;
Graph G; int read(); int main()
{
while (cin>>n>>m)
{
G.init();
for (int i=1;i<=m;i++)
{
int u=read(),v=read(),w=read();
G.Add_Edge(u,v,w);
}
G.spfa();
cout<<G.Dist[n]<<endl;
}
return 0;
} int read()
{
int x=0;
int k=1;
char ch=getchar();
while (((ch>'9')||(ch<'0'))&&(ch!='-'))
ch=getchar();
if (ch=='-')
{
k=-1;
ch=getchar();
}
while ((ch>='0')&&(ch<='9'))
{
x=x*10+ch-48;
ch=getchar();
}
return x*k;
}

POJ 3159 Candies (图论,差分约束系统,最短路)的更多相关文章

  1. POJ 3159 Candies(差分约束+最短路)题解

    题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...

  2. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  3. POJ 3159 Candies(差分约束)

    http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...

  4. poj 3159 Candies (差分约束)

    一个叫差分约束系统的东西.如果每个点定义一个顶标x(v),x(t)-x(s)将对应着s-t的最短路径. 比如说w+a≤b,那么可以画一条a到b的有向边,权值为w,同样地给出b+w2≤c,a+w3≤c. ...

  5. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  6. POJ 3159 Candies 【差分约束+Dijkstra】

    <题目链接> 题目大意: 给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c .最后求n 比 1 ...

  7. POJ 3159 Candies 还是差分约束(栈的SPFA)

    http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...

  8. [poj 3159]Candies[差分约束详解][朴素的考虑法]

    题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...

  9. poj 3159 Candies dijkstra + queue

    题目链接: http://poj.org/searchproblem 题目大意: 飞天鼠是班长,一天班主任买了一大包糖果,要飞天鼠分发给大家,班里面有n个人,但是学生A认为学生B比自己多的糖果数目不应 ...

随机推荐

  1. 2015520吴思其 基于《Arm试验箱的国密算法应用》课程设计个人报告

    20155200吴思其 基于<Arm试验箱的国密算法应用>课程设计个人报告 课程设计中承担的任务 完成试验箱测试功能4,5,6以及SM3加密实验的实现 测试四 GPIO0按键中断实验 实验 ...

  2. 20155226《网络攻防》 Exp5 MSF基础应用

    20155226<网络攻防> Exp5 MSF基础应用 基础问题回答 1.用自己的话解释什么是exploit,payload,encode? exploit : Exploit的英文意思就 ...

  3. 20155234 昝昕明《基于ARM实验箱的国密算法应用》课程设计个人报告

    20155234 昝昕明<基于ARM实验箱的国密算法应用>课程设计个人报告 个人贡献 参与课设题目讨论及完成全过程: 资料收集: SM1算法及和ARM之间通信 负责串口代码调试: 协调完成 ...

  4. 20155338《网络对抗》Exp3 免杀原理与实践

    20155338<网络对抗>Exp3 免杀原理与实践 实验过程 一.免杀效果参考基准 Kali使用上次实验msfvenom产生后门的可执行文件,上传到老师提供的网址http://www.v ...

  5. Vue 项目集合

    饿了么安全应急响应中心 饿了么招聘 饿了么前端 · GitHub 稀土掘金 异乡好居 明星垂搜 广州建管 基于Vue.js的数据统计系统(一) 基于Vue.js的数据统计系统(二) 基于Vue.js的 ...

  6. Luogu P1113 杂务

    终于没有打模板题了. 一道简单的拓扑题目(但记得以前第一次做的时候爆0了). 发现这个做事的过程是按一定顺序的,然后如果一个工作的前面没有任何事情的话,它一定先被完成(如果不的话就不能使时间最小化,其 ...

  7. binary 和 varbinary 用法全解

    在SQL Server中,使用数据类型 binary(n) 和 varbinary(n) 存储二进制数据,n是指字节数量: binary(n):固定长度为 n 字节,其中 n 值从 1 到 8,000 ...

  8. Flask学习-Flask app接受第一个HTTP请求

    一.__call__() 在Flask app启动后,一旦uwsgi收到来自web server的请求,就会调用后端app,其实此时就是调用app的__call__(environ,start_res ...

  9. Ps矩形工具的运用

    矩形工具 1.标识位置以及快捷键 位于左侧工具栏中,快键键是u,根据需求选择里面包含的工具 2.使用方法 鼠标左键点击工具,直接在图层使用,点击后拖住不放选择想要的图形后松手即可. 可以根据自身的需求 ...

  10. pie的绕过方式

    目标程序下载 提取码:qk1y 1.检查程序开启了哪些安全保护机制 pie机制简介 PIE(position-independent executable) 是一个针对代码段.text, 数据段.*d ...