洛谷 P1993 【小K的农场】

这道题都没有用Ford的啊,跑的比SPFA还快,不加优化就可以过...


看到这一组组的不等式,就可以想到差分约束了,但是这道题需要进行转化,我这里跑的是最短路(好多大佬都跑的最长路qwq)。


  • \(b\ge a-c\)
  • \(a\ge b+c\)
  • 这个变化就得看好了\(a\ge b\) && \(b\ge a\)

这些了解了之后,直接看代码就可啦~

#include <bits/stdc++.h>
using namespace std;
struct node{
	int l , r , w;
};
node e[50010];
int n , m , tot;
long long ans;
int dis[10010];
void add(int x , int y , int z){
	e[++tot].l = x;
	e[tot].r = y;
	e[tot].w = z;
}
int main(){
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		int f , x , y , z;
		cin >> f >> x >> y;
		if(f == 1){
			cin >> z;
			add(x , y , -z);
		}
		if(f == 2){
			cin >> z;
			add(y , x , z);
		}
		if(f == 3){
			add(x , y , 0);
			add(y , x , 0);
		}
	}
	memset(dis , 127 , sizeof(dis));
	dis[1] = 0;
	for(int i = 1; i < n; i++)
		for(int j = 1; j <= tot; j++)
			dis[e[j].r] = min(dis[e[j].r] , dis[e[j].l] + e[j].w);
	for(int j = 1; j <= tot; j++)
		if(dis[e[j].r] > dis[e[j].l] + e[j].w){
			cout << "No";
			return 0;
		}
	cout << "Yes";
	return 0;
}
posted @ 2020-07-02 20:55  那一条变阻器  阅读(73)  评论(0编辑  收藏  举报