推荐几个比较好的博客:https://blog.csdn.net/whereisherofrom/article/details/78922648 最短路和差分约束

http://www.cnblogs.com/void/archive/2011/08/26/2153928.html 差分约数系统详解

https://blog.csdn.net/xuezhongfenfei/article/details/8685313 差分约束系统

 

SPFA最短路模板:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<queue>
 6 using namespace std;
 7 const int maxn=1010;
 8 const int inf=1e9;
 9 struct edge{
10     int v,cost;
11     edge(int _v=0,int _cost=0):v(_v),cost(_cost) {}
12 };
13 vector<edge>E[maxn];
14 bool vis[maxn];
15 int cnt[maxn],dist[maxn],s,n;
16 
17 void addedge(int u,int v,int w)
18 {
19     E[u].push_back(edge(v,w));
20 }
21 
22 bool SPFA()
23 {
24     memset(vis,false,sizeof(vis));
25     memset(cnt,0,sizeof(cnt));
26     for ( int i=1;i<=n;i++ ) dist[i]=inf;
27     vis[s]=true;
28     dist[s]=0;
29     queue<int>que;
30     que.push(s);
31     cnt[s]=1;
32     while ( !que.empty() ) {
33         int u=que.front();
34         que.pop();
35         vis[u]=false;
36         for ( int i=0;i<E[u].size();i++ ) {
37             int v=E[u][i].v;
38             int cost=E[u][i].cost;
39             if ( dist[v]>dist[u]+cost ) {
40                 dist[v]=dist[u]+cost;
41                 if ( !vis[v] ) {
42                     vis[v]=true;
43                     que.push(v);
44                     if ( ++cnt[v]>n ) return false;
45                 }
46             }
47         }
48     }
49     return true;
50 }
SPFA求最短路

相关文章:

猜你喜欢
  • 2021-10-13
  • 2022-01-04
  • 2022-12-23
  • 2021-09-26
  • 2021-11-29
相关资源
相似解决方案