正文
增加 100 行删除 0 行
1,518 行未改动全部展开1519152015211522152315241525152615271,530 行未改动全部展开1,518 行未改动全部展开cout<<dis[i]<<" ";return 0;}```## Floyd```cppfor(int k=1;k<=n;k++){1,530 行未改动全部展开1,518 行未改动全部展开15191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271,530 行未改动全部展开1,518 行未改动全部展开cout<<dis[i]<<" ";return 0;}```## 差分约束| 原式 | 转化 | 建边 ||---|---|---|| $x_c-x_{c'}\le y$ | 不变 | `c' -> c`,权 $y$ || $x_c-x_{c'}\ge y$ | $x_{c'}-x_c\le -y$ | `c -> c'`,权 $-y$ || $x_c-x_{c'}< y$ | $x_c-x_{c'}\le y-1$ | `c' -> c`,权 $y-1$ || $x_c-x_{c'}> y$ | $x_{c'}-x_c\le -y-1$ | `c -> c'`,权 $-y-1$ || $x_c-x_{c'}= y$ | 拆成 $\le y$ 和 $\ge y$ | 上面两条边 |```cpp#include <bits/stdc++.h>using namespace std;#define ll long long#define db double#define pii pair<int,int>#define mp make_pair#define fi first#define se secondconst int N = 10010;int n, m;vector<pii> g[N];int dis[N];int cnt[N];bool vis[N];bool spfa(int s){for (int i = 0; i <= n; i++){dis[i] = INT_MAX;vis[i] = false;cnt[i] = 0;}queue<int> q;dis[s] = 0;vis[s] = true;q.push(s);while(!q.empty()){int x=q.front();q.pop();vis[x]=0;for(pii e : g[x]){int y=e.fi;int w=e.se;if(dis[y]>dis[x]+w){dis[y]=dis[x]+w;cnt[y]=cnt[x]+1;if(cnt[y]>n) return 0;if(!vis[y]){vis[y]=1;q.push(y);}}}}return true;}int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m;for(int i=1;i<=n;i++){g[0].push_back({i,0});}for(int i=1;i<=m;i++){int c,cp,y;cin>>c>>cp>>y;g[cp].push_back({c,y});}if(!spfa(0)){cout<<"NO"<<endl;}else{for(int i=1;i<=n;i++){cout<<dis[i]<<" ";}cout<<endl;}return 0;}```## Floyd```cppfor(int k=1;k<=n;k++){1,530 行未改动全部展开