rt,谢谢.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e3+10,M=1e4+10;
int w[M],e[M],ne[M],h[N],idx;
inline void add(int a,int b,int c) {w[idx]=c;e[idx]=b;ne[idx]=h[a];h[a]=idx++;}
struct pt{
int x,w;
friend bool operator<(pt a,pt b) {return a.w<b.w;}
};
int d[N],cnt[N],n,m;
bool st[N];
bool spfa(){
queue<pt> q;
q.push({0,0});
memset(d,-0x3f,sizeof(d));
d[0]=0;
cnt[0]++;
while(!q.empty()){
auto t=q.front();q.pop();
st[t.x]=false;
for(int i=h[t.x];i != -1;i=ne[i]){
int j=e[i];
if(d[j]<d[t.x]+w[i]){
d[j]=d[t.x]+w[i];
if(!st[j]) {
st[j]=true;
cnt[j]++;
if(cnt[j] >= n+1) return false;
q.push({j,d[j]});
}
}
}
}
return true;
}
int main(){
memset(h,-1,sizeof(h));
scanf("%d%d",&n,&m);
while(m--){
int op,a,b,c;
scanf("%d%d%d",&op,&a,&b);
if(op == 1) scanf("%d",&c),add(b,a,c);
else if(op == 2) scanf("%d",&c),add(a,b,-c);
else add(a,b,0),add(b,a,0);
}
for(int i=1;i <= n;i++) add(0,i,0);
if(spfa()) puts("Yes");
else puts("No");
return 0;
}