差分约束
1.构图方式:
就是利用不等式和最短路中的性质,通过建图得到的一种最优解,
例如,最短路中有dist[u]>=dist[v]+w(v,u)那么就类似:A-B<=C,移项得: 建边(B,A,C) .
2.难点:难点就在于这种题一般不会裸的告诉你什么关系,总是要根据题目分析出来不等关系后才能进行建边和差分约束的应用。一般有负权存在的时候都采用spfa算法,如果没有的话,优化的dij算法是最优的。
2.例题(shui):
poj3169—奶牛的题目:
就是n头奶牛,给了几种关系,问1-n奶牛最远距离。
代码:
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stdio.h>
using namespace std;
const int maxn=1010;
const int inf=0x3f3f3f3f;
struct node
{
int v,cost;
node(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<node>G[maxn];
void add(int u,int v,int w){
G[u].push_back(node(v,w));
}
bool vis[maxn];
int dist[maxn];
int cnt[maxn];
bool spfa(int start,int n){
memset