博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3159——Candies(差分约束+SPFA堆栈)
阅读量:2343 次
发布时间:2019-05-10

本文共 2922 字,大约阅读时间需要 9 分钟。

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

一开始没什么思路,题意都不太懂,网上看了下才知道是差分约束,又是个新知识点。

题意是a最多能忍受b比他多c个糖果,也就是b-a<=c,把这个等式转换成图上的边,a,b为两点,c为权值,等式转换成a+c<=b就可以看出这是条a至b的有向边。图建完就可以用求最短路的方法解出1到n最多能给的糖果数。
一般用SPFA,本题没有负权环路,不能用SPFA+QUENE,开小了会WA,开大了就TLE,只能用SPFA+STACK

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 1000010#define mod 1000000007using namespace std;struct Edge{ int v,w,next;};Edge edge1[MAXN<<1];int head1[MAXN],n,m,e,vis[MAXN],dis[MAXN];int q[MAXN];void add(Edge *edge,int *head,int u,int v,int w){ edge[e].v=v; edge[e].w=w; edge[e].next=head[u]; head[u]=e;}long long spfa(Edge *edge,int *head){ /*memset(vis,0,sizeof(vis)); for(int i=1; i<=n; ++i) dis[i]=INF; dis[u]=0; queue
q; q.push(u); while(!q.empty()) { u=q.front(); q.pop(); vis[u]=1; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v,w=edge[i].w; if(w+dis[u]
dis[u]+w) { dis[v]=dis[u]+w; if(!vis[v]) { vis[v]=1; q[top++]=v; } } } } return dis[n];}int main(){ while(~scanf("%d%d",&n,&m)) { e=0; int u,v,w; memset(head1,-1,sizeof(head1)); while(m--) { scanf("%d%d%d",&u,&v,&w); //v-u<=w add(edge1,head1,u,v,w); //即w+u<=v,权值+起点<=终点 e++; } long long ans; ans=spfa(edge1,head1); printf("%I64d\n",ans); } return 0;}

转载地址:http://pscvb.baihongyu.com/

你可能感兴趣的文章
Java Request 获取请求method,请求param,请求body
查看>>
Mybatis plus 多数据源切换
查看>>
极速搭建一个FTP服务器
查看>>
Java Ftp 文件操作
查看>>
Windows 简单安装配置Redis
查看>>
Mybatis Plus 逆向生成实体类、实现类、控制层
查看>>
IDEA解除SVN关联
查看>>
Springboot 指定运行时配置文件的几种方式
查看>>
Log4j 输出完整的异常信息
查看>>
Spring Boot打包成执行jar后获取classpath下文件异常解决
查看>>
Springboot 同时支持Http和Https访问
查看>>
Java 简单的复习下JDBC 工具类
查看>>
将Java Swing Jar 封装成exe文件
查看>>
端口显示被占用,netstat -aon | findstr却找不到端口的解决方法
查看>>
Linux内核中读写文件数据的方法
查看>>
USB电池充电基础:应急指南(转载)
查看>>
I2C死锁原因及解决方法【转】
查看>>
Ubuntu系统如何安装双网卡及更改网卡名称(eth0改为eth1)
查看>>
销毁new的指针
查看>>
向控制台打印指针
查看>>