#include<cstdio>
#include<queue>
using namespace std;
const int N=1e4+10;
inline int read()
{
int s=0,b=1; char c=getchar();
while(c<'0'||c>'9') {if(c=='-') b=-1; c=getchar();}
while(c>='0'&&c<='9') s=(s<<3)+(s<<1)+(c&15),c=getchar();
return s*b;
}
struct edge{int next,to,dis;}a[N*2];
queue<int> q;
int n,m,t,x,y,z,s,i;
int dis[N],cnt[N],h[N*2]; bool f,b[N];
inline void build(int x,int y,int z){a[++s]=edge{h[x],y,z},h[x]=s;}
bool spfa()
{
while(!q.empty())
{
int u=q.front();
q.pop(); b[u]=0;
for(int i=h[u];i;i=a[i].next)
{
int v=a[i].to;
if(dis[v]>dis[u]+a[i].dis)
{
dis[v]=dis[u]+a[i].dis; cnt[v]=cnt[u]+1;
if(cnt[v]>n) return 1;
b[v]=1,q.push(v);
}
}
}
return 0;
}
int main()
{
n=read(),m=read();
for(i=1;i<=m;i++)
{
f=read();
if(f=='1') x=read(),y=read(),z=read(),build(y,x,z);
else if(f=='2') x=read(),y=read(),z=read(),build(x,y,-z);
else x=read(),y=read(),build(x,y,0);
}
for(i=1;i<=n;i++) build(n+1,i,0);
for(i=1;i<=n;i++) dis[i]=1e9;
dis[n+1]=0,b[n+1]=1,cnt[n+1]=1,q.push(n+1);
spfa()?puts("Yes"):puts("No");
return 0;
}
请注意读入部分