TYVJ1415 差分约束
思路:
i–>i+1连一条边权为0的边
i–>i-1连一条边权为-1的边
start-1 —>end 连一条边权为w的边
求0—>n的最长路即可
//By SiriusRen
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 22222
int next[N],first[N],v[N],w[N],tot=0;
int n,m,xx,inq[N],dis[N];
void spfa(){
queue<int>q;
q.push(0);
while(!q.empty()){
int t=q.front();q.pop();
inq[t]=0;
for(int i=first[t];~i;i=next[i])
if(dis[v[i]]<dis[t]+w[i]){
dis[v[i]]=dis[t]+w[i];
if(!inq[v[i]])
inq[v[i]]=1,q.push(v[i]);
}
}
}
int main(){
memset(first,-1,sizeof(first));
memset(dis,-1,sizeof(dis));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&xx,&v[tot],&w[tot]);
next[tot]=first[xx-1],first[xx-1]=tot++;
}
for(int i=1;i<=n;i++)
w[tot]=0,v[tot]=i,next[tot]=first[i-1],first[i-1]=tot++;
for(int i=0;i<=n;i++)
w[tot]=-1,v[tot]=i,next[tot]=first[i+1],first[i+1]=tot++;
dis[0]=0;
spfa();
printf("%d\n",dis[n]);
}
TYVJ1415 差分约束的更多相关文章
- Candies-POJ3159差分约束
Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- ZOJ 2770火烧连营——差分约束
偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- 2014 Super Training #6 B Launching the Spacecraft --差分约束
原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...
- POJ 1364 King --差分约束第一题
题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...
随机推荐
- java1.8对集合中对象的特有属性进行排序
每天学习一点点,知识财富涨点点 1.创建对象user12 2.编写测试类 3.输出结果 加油!!!!
- 4.matlab基础
1 函数句柄 clear all; f1=@cos t=:pi/:pi f1(t) f2=@complex f2(,) clear all %函数句柄 f1=@char %函数句柄转换为字符串 s1= ...
- 32.QT绘图
widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPainter> #inclu ...
- Repeater控件使用小结持续更新
Repeater嵌套Repeater绑定数据 前台代码 <!--注意层级关系不要写错了--> <asp:Repeater ID="rpGroup" runat=& ...
- .NET XML POST 请求
//请求体,XML参数 string xmlstring = @"<root></root>“; //请求URL string postUrl ="http ...
- 微信小程序调试 Webview
document.querySelectorAll("webview")[1].showDevTools(true);
- SQL Server 检测到基于一致性的逻辑 I/O 错误
背景:新建DB_GZN 恢复数据库备份文件 执行: select * from VI_MPS_PAPLT 错误提示: 消息 824,级别 24,状态 2,第 2 行 SQL Serv ...
- 基于mysql主从同步的proxy读写分离
mysql-proxy 简介 MySQL Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测.分析或改变它们的通信.它使用灵活,没有限制,常见的用途包括:负载 ...
- 转:Hibernate中Criteria和DetachedCriteria的完整用法
原文地址:http://blog.sina.com.cn/s/blog_667528fd0100rkrf.html 设计上可以灵活的根据 Criteria 的特点来方便地进行查询条件的组装.现在对 H ...
- 路飞学城Python-Day19(practise)
# 特性1.继承:2.多态:3.封装 # 1.继承的用处:通过继承就可以解决类与类之间的代码冗余关系 # 2.多态的用处:1.增加了程序的灵活性,以不变应万变,使用者都是同一种形式去调用(func(a ...