相比dij,spfa优点是可处理含负边不含负圈的最短路问题,缺点是算法复杂度不太好【貌似可以使用两种优化。LLL和SLF】

差分约束就是将一些不等式转化为图中的带权边,然后求解最短路或最长路的方法

洛谷P1645https://www.luogu.org/problemnew/show/P1645

 #include<bits/stdc++.h>
using namespace std;
struct pot{
int to;
int next;
int len;
}edge[];
queue<int>pq;
int vis[];
int next[];
int dist[];
void add(int x,int y,int z,int t){
edge[t].len=z;
edge[t].next=next[x];
edge[t].to=y;
next[x]=t;
}
void spfa(int x)
{
while(!pq.empty())pq.pop();
dist[x]=;
vis[x]=true;
pq.push(x);
while(!pq.empty()){
int u=pq.front();
pq.pop();
vis[u]=false;
for(int i = next[u]; i != - ; i=edge[i].next)
{
int w=edge[i].to;
if(dist[w]<dist[u]+edge[i].len)
{
dist[w]=dist[u]+edge[i].len;
if(!vis[w]){
vis[w]=true;
pq.push(w);
}
}
}
}
}
int main()
{
int n;
scanf("%d",&n);
int tot=;
int mmax=;
int mmin=;
memset(dist,-,sizeof(dist));
memset(next,-,sizeof(next));
memset(vis,,sizeof(vis));
for(int i = ; i < n ; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a-,b,c,tot);
if(a<mmin)mmin=a;
if(mmax<b)mmax=b;
tot++;
}
for(int i = mmin ; i <= mmax ; i++)
{
add(i-,i,,tot);
tot++;
add(i,i-,-,tot);
tot++;
}
spfa(mmin-);
cout << dist[mmax]<<endl;
return ;
}

差分约束+spfa【模板】的更多相关文章

  1. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

  2. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  3. P5960 差分约束算法模板

    差分约束 差分约束,一般用来解决有\(n\)个未知数,\(m\)个不等式方程的问题,形如: \[\begin{cases} \ x_{a_1}-x_{b_1}\leq y_1\\ \ x_{a_2}- ...

  4. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  5. 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...

  6. (简单) POJ 3169 Layout,差分约束+SPFA。

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  7. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  8. BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)

    BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...

  9. POJ-3159.Candies.(差分约束 + Spfa)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 40407   Accepted: 11367 Descri ...

随机推荐

  1. Codeforces 496D - Tennis Game

    496D - Tennis Game 思路:枚举每个t,求出对应的满足条件的s. 代码: #include<bits/stdc++.h> using namespace std; #def ...

  2. Ubuntu安装NVIDA显卡驱动

    0. 综述 电脑型号:R720 Ubuntu版本:16 显卡型号:1050ti 目前,知道3种安装N卡驱动的方法: 1. PPA源:最简便,但未必有最新驱动(亲测),或可能遇到问题(风闻). sudo ...

  3. 1月10日 ruby基础教程,查漏补缺; 2月22日 Exception补充

    https://ruby-doc.org/core-2.5.0/Exception.html 1月20日练习完1,2章. 第一章 初探 ‘’单引号不执行转义符. \t 制表符.\n 换行符. p me ...

  4. 加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用

    加密技术通常分为两大类:"对称式"和"非对称式". 对称性加密算法:对称式加密就是加密和解密使用同一个密钥.信息接收双方都需事先知道密匙和加解密算法且其密匙是相 ...

  5. HDU-1272 小希的迷宫 (并查集、判断图是否为树)

    Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...

  6. Leetcode 115

    Ø r a b b b i t Ø r a b b i t class Solution { public: int numDistinct(string s, string t) { ; ; int ...

  7. spring cloud 学习(一)初学SpringCloud

    初学SpringCloud 前言 在SpringBoot的坑还没填完的情况下,我又迫不及待地开新坑了.主要是寒假即将结束了,到时又得忙于各种各样的事情……留个坑给自己应该就会惦记着它,再慢慢地补上…… ...

  8. POJ 1128 拓扑排序 + 深搜

    /* (⊙v⊙)嗯 貌似是一个建图 拓扑+深搜的过程.至于为什么要深搜嘛..一个月前敲得题现在全部推了重敲,于是明白了.因为题意要求如果有多个可能的解的话. * 就要输出字典序最小的那个.所以可以对2 ...

  9. 给构造函数(constructor)创建对象(object)

    (来源http://www.cnblogs.com/dongjc/p/5179561.html) javascript是一种“基于prototype的面向对象语言“,与java有非常大的区别,无法通过 ...

  10. openshift rhc

    Microsoft Windows [Version 6.1.7601]Copyright (c) 2009 Microsoft Corporation. All rights reserved. C ...