stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <cstdio>

#include <iostream>

#include <algorithm>

#include <cstring>

#include <queue>



using namespace std;



struct edge

{

    int v,w,nxt;

}e[400050];



int head[100050],ecnt;



void adde(int u,int v,int w)

{

    e[++ecnt].v = v;

    e[ecnt].w = w;

    e[ecnt].nxt = head[u];

    head[u] = ecnt;

}



int N,M;

int l[200050],r[200050];

int d[200050];

long long dist[100005];

queue<int> que;

bool vis[100050],inq[100050];



bool spfa(int st)

{

    que.push(st);

    inq[st] = true;

    dist[st] = 0;

    long long curmax = 0,curmin = 0;

    while(!que.empty())

    {

        int u = que.front();

        que.pop();

        for(int i = head[u];i;i = e[i].nxt)

        {

            int v = e[i].v;

            if(dist[v] == 12345678987654321)

            {

                dist[v] = dist[u] + e[i].w;

                curmax = max(curmax,dist[v]);

                curmin = min(curmin,dist[v]);

                if(curmax - curmin > 1000000000)

                    return false;

                vis[v] = 1;

                if(!inq[v])

                    que.push(v);

            }

            else if(dist[v] != dist[u] + e[i].w)

                return false;

        }

        inq[u] = false;

    }

    return true;

}



int main()

{

    for(int i = 1;i <= 100000; ++ i)

        dist[i] = 12345678987654321;

    scanf("%d%d",&N,&M);

    for(int i = 1;i <= M; ++ i)

    {

        scanf("%d%d%d",&l[i],&r[i],&d[i]);

        adde(l[i],r[i],d[i]);

        adde(r[i],l[i],-d[i]);

    }

    for(int i = 1;i <= N; ++ i)

    {

        if(!vis[i])

        {

            vis[i] = 1;bool l = spfa(i);

            if(!l)

            {

                printf("No\n");

                return 0;

            }

        }

    }

    printf("Yes\n");

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.8052062868369352x):
#include<bits/stdc++.h>

using namespace std;

int n,m;

struct edge{

	int next;

	int to;

	int w;

}e[4000020];

int head[2000020];

bool dijia(int s);

int dis[2000020];

bool book[2000020],vis[2000020];

int main(){

	scanf("%d %d",&n,&m);

	int u,v,w;

	for(int x=1;x<=m;x++){

		scanf("%d %d %d",&u,&v,&w);

		e[x].to=v;

		e[x].w=w;

		e[x].next=head[u];

		head[u]=x;

		e[x+m].next=head[v];

		e[x+m].to=u;

		e[x+m].w=-w;

		head[v]=x+m;

	}

	for(int x=1;x<=n;x++){

		if(vis[x]){

			continue;

		}

		if(!dijia(x)){

			printf("No");

			return 0;

		}

		else{

			continue;

		}

	}

	printf("Yes");

	return 0;

}

bool dijia(int s){

	dis[s]=0;

	queue<int> q;

	q.push(s);

	book[s]=1;

	while(q.size()){

		int k=q.front();

		q.pop();

		book[k]=0;

		for(int x=head[k];x;x=e[x].next){

			if(!vis[e[x].to]){

				dis[e[x].to]=dis[k]+e[x].w;

				q.push(e[x].to);

				book[e[x].to]=1;

				vis[e[x].to]=1;

			}

			else{

				if(dis[e[x].to]!=dis[k]+e[x].w){

					return false;

				}

			}

		}

	}

	return true;

}
// Slower C++ solution:
# include <cstring>

# include <cstdio>

# include <iostream>

# include <queue>

# define N 100050

# define M 200050 

# define WQD 0x7f7f7f7f

int n;

int m;

struct Edge{int next,to, w;}  e[M<<1];

int head[N+1]; 

int dis[N+1];

bool vis[N+1];

bool book[ N + 1 ] ;

bool spfa(int s);

using namespace std;

int main()

{

	memset(dis,0x7f,sizeof(dis));

	memset(book,0,sizeof(book));

	ios::sync_with_stdio ( false ) ;

	int u,v,w;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	for(int i=1; i<=m; i++)

	{

		cin>>u>>v>>w;

		e[i]=(Edge){head[u],v,w};head[u]=i;

		e[m+i]=(Edge){head[v],u,-w};head[v]=m+i;

	}

	for(int i=1; i<=n; i++)

		if(!vis[i]) 

		{

			vis[i]=1;

			if(!spfa(i))  {cout<<"No";return 0;}

		}

	cout<<"Yes";

	return 0;

}

bool spfa(int s)

{

 	queue<int>q;

//	memset(book,0,sizeof(book));

//	memset(dis,0x7f,sizeof(dis));

	dis[s]=0;

	q.push(s);book[s]=1;

	while(q.size()>0)

	{

		int k=q.front();q.pop();

		book[k]=0;

		for(int i=head[k]; i!=-1; i=e[i].next)

		{

			int v=e[i].to;

			if(dis[v]==WQD ) 

			{				

				dis[v]=dis[k]+e[i].w;

				if( ! book[v])	{q.push(v);	book[v]=1;}

				vis[v]=1;

			}

			else if(  (dis[v]!=dis[k]+e[i].w) )	return false;

		}		

    }

	return true;

}

// Faster C++ solution for the same problem (measured speedup: 1.9319601459444289x):
#include<bits/stdc++.h>

using namespace std;

int n,m;

struct edge{

	int next;

	int to;

	int w;

}e[4000020];

int head[2000020];

bool dijia(int s);

int dis[2000020];

bool book[2000020],vis[2000020];

int main(){

	scanf("%d %d",&n,&m);

	int u,v,w;

	for(int x=1;x<=m;x++){

		scanf("%d %d %d",&u,&v,&w);

		e[x].to=v;

		e[x].w=w;

		e[x].next=head[u];

		head[u]=x;

		e[x+m].next=head[v];

		e[x+m].to=u;

		e[x+m].w=-w;

		head[v]=x+m;

	}

	for(int x=1;x<=n;x++){

		if(vis[x]){

			continue;

		}

		if(!dijia(x)){

			printf("No");

			return 0;

		}

		else{

			continue;

		}

	}

	printf("Yes");

	return 0;

}

bool dijia(int s){

	dis[s]=0;

	queue<int> q;

	q.push(s);

	book[s]=1;

	while(q.size()){

		int k=q.front();

		q.pop();

		book[k]=0;

		for(int x=head[k];x;x=e[x].next){

			if(!vis[e[x].to]){

				dis[e[x].to]=dis[k]+e[x].w;

				q.push(e[x].to);

				book[e[x].to]=1;

				vis[e[x].to]=1;

			}

			else{

				if(dis[e[x].to]!=dis[k]+e[x].w){

					return false;

				}

			}

		}

	}

	return true;

}
// Slower C++ solution:
# include <cstring>

# include <cstdio>

# include <iostream>

# include <queue>

# define N 100050 //N为顶点个数 

# define M 200050 //N为顶点个数 

# define WQD 0x7f7f7f7f//无穷大 

int n;//图的顶点个数

int m;//图的边数 

struct Edge{int next,to, w;}  e[M<<1];//不用0号单元 

int head[N+1]; 

int dis[N+1];//存放源点到其他点的路径

bool vis[N+1];

bool book[N+1];

bool spfa(int s);//差分约束判断 

using namespace std;

int main()

{

	ios::sync_with_stdio ( false ) ;

	int u,v,w;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	for(int i=1; i<=m; i++)  //本题约束关系为=，要添加两条边 

	{

		cin>>u>>v>>w;//输入第i条边 ,i为编号 

		e[i]=(Edge){head[u],v,w};head[u]=i;

		e[m+i]=(Edge){head[v],u,-w};head[v]=m+i;

	}

	memset(book,0,sizeof(book));

	memset(dis,0x7f,sizeof(dis));

	for(int i=1; i<=n; i++)

		if(!vis[i]) 

		{

			vis[i]=1;

			if(spfa(i)==false)  {cout<<"No";return 0;}

		}

	cout<<"Yes";

	return 0;

}

bool spfa(int s)

{

 	queue<int>q;

 	//memset(book,0,sizeof(book));

	//memset(dis,0x7f,sizeof(dis));

	dis[s]=0;

	q.push(s);book[s]=1;

	while(q.size()>0)//如果没松弛就松弛，如果松弛过就判断是否符合约束条件，如果自相矛盾就是不可能的数据 

	{

		int k=q.front();q.pop();//对头元素出队 

		book[k]=0;

		for(int i=head[k]; i!=-1; i=e[i].next)

		{

			int v=e[i].to;

			if(dis[v]==WQD )//如果v点没松弛过，就松弛更新dis[v]的值 

				//题目的约束条件是= 

				{				

					dis[v]=dis[k]+e[i].w;//更新 

					if(book[v]==0)	{q.push(v);	book[v]=1;}//入队标记

					vis[v]=1;//标记该点已经松弛过 

				}

			else if(dis[v]!=dis[k]+e[i].w)	return false;//这里也可以用不等于，根据题意理解差分系统约束条件 

		}		

    }

	return true;//别掉了这句 

}

// Faster C++ solution for the same problem (measured speedup: 1.9295795677799608x):
#include<bits/stdc++.h>

using namespace std;

int n,m;

struct edge{

	int next;

	int to;

	int w;

}e[4000020];

int head[2000020];

bool dijia(int s);

int dis[2000020];

bool book[2000020],vis[2000020];

int main(){

	scanf("%d %d",&n,&m);

	int u,v,w;

	for(int x=1;x<=m;x++){

		scanf("%d %d %d",&u,&v,&w);

		e[x].to=v;

		e[x].w=w;

		e[x].next=head[u];

		head[u]=x;

		e[x+m].next=head[v];

		e[x+m].to=u;

		e[x+m].w=-w;

		head[v]=x+m;

	}

	for(int x=1;x<=n;x++){

		if(vis[x]){

			continue;

		}

		if(!dijia(x)){

			printf("No");

			return 0;

		}

		else{

			continue;

		}

	}

	printf("Yes");

	return 0;

}

bool dijia(int s){

	dis[s]=0;

	queue<int> q;

	q.push(s);

	book[s]=1;

	while(q.size()){

		int k=q.front();

		q.pop();

		book[k]=0;

		for(int x=head[k];x;x=e[x].next){

			if(!vis[e[x].to]){

				dis[e[x].to]=dis[k]+e[x].w;

				q.push(e[x].to);

				book[e[x].to]=1;

				vis[e[x].to]=1;

			}

			else{

				if(dis[e[x].to]!=dis[k]+e[x].w){

					return false;

				}

			}

		}

	}

	return true;

}
// Slower C++ solution:
# include <cstdio>

# include <iostream>

# include <cstring>

# include <algorithm>

# include <queue>

# define N 100001

# define M 200001

# define _cin ios::sync_with_stdio(false)

# define Inf 0x7f7f7f7f

using namespace std;

struct ege

{

	int next;

	int to;

	int w;

}e[M<<1];

int n;//人数 

int m;//信息数 

int book[N+1];

int head[N+1];

int dis[N+1];

bool vis[N+1];

bool spfa(int x);

int main()

{

	_cin;

	

	int u,v,w;

	cin>>n>>m;

	memset(head,-1,sizeof(head));

	for(int i=1;i<=m;i++)	

	{

		cin>>u>>v>>w;

		//建立双向边 

		/*e[i].next=head[u];

		e[i].to=v;

		e[i].w=w;

		head[u]=i;

		e[i+m].next=head[v];

		e[i+m].to=u;

		e[i+m].w=-w;

		head[v]=i+m;

		*/

		e[i]=(ege){head[u],v,w};head[u]=i;

		e[m+i]=(ege){head[v],u,-w};head[v]=m+i;		

	}

	memset(book,0,sizeof(book));

	memset(dis,0x7f,sizeof(dis));

	for(int i=1;i<=n;i++)

	if(!vis[i])//表示两点连通

	{

		vis[i]=1;//连通就变成真值 

		if(spfa(i)==false)	{cout<<"No"<<endl;return 0;}

	} 

	cout<<"Yes"<<endl;

	return 0;

}

bool spfa(int x)

{

	queue <int>q;

	dis[x]=0;

	q.push(x);book[x]=1;//标记已经进队列的点 

	while(q.size()>0)

	{

		int k=q.front();//取头元素

		q.pop();//头元素出队列

		book[k]=0;

		for(int i=head[k]; i!=-1; i=e[i].next)

		{

			if(dis[e[i].to]==Inf && dis[k]<Inf)

			{

				dis[e[i].to]=dis[k]+e[i].w;

				//如果下一个点没有被标记，将这个点标记，进队列 

				if(book[e[i].to]==0)	{q.push(e[i].to);book[e[i].to]=1;}

				vis[e[i].to]=1;

			} 

			//与已知矛盾，即任意两点的距离都不能有两个值 

			//表示k点已被改变值，下一个点也已经改变值，但是新值与旧值不等 

			else if(dis[k]<Inf && dis[e[i].to]<Inf && dis[e[i].to]!=dis[k]+e[i].w) return false;

		} 

	}

	return true;

}

// Faster C++ solution for the same problem (measured speedup: 2.239714566376649x):
#include<bits/stdc++.h>

using namespace std;

int n,m;

struct edge{

	int next;

	int to;

	int w;

}e[4000020];

int head[2000020];

bool dijia(int s);

int dis[2000020];

bool book[2000020],vis[2000020];

int main(){

	scanf("%d %d",&n,&m);

	int u,v,w;

	for(int x=1;x<=m;x++){

		scanf("%d %d %d",&u,&v,&w);

		e[x].to=v;

		e[x].w=w;

		e[x].next=head[u];

		head[u]=x;

		e[x+m].next=head[v];

		e[x+m].to=u;

		e[x+m].w=-w;

		head[v]=x+m;

	}

	for(int x=1;x<=n;x++){

		if(vis[x]){

			continue;

		}

		if(!dijia(x)){

			printf("No");

			return 0;

		}

		else{

			continue;

		}

	}

	printf("Yes");

	return 0;

}

bool dijia(int s){

	dis[s]=0;

	queue<int> q;

	q.push(s);

	book[s]=1;

	while(q.size()){

		int k=q.front();

		q.pop();

		book[k]=0;

		for(int x=head[k];x;x=e[x].next){

			if(!vis[e[x].to]){

				dis[e[x].to]=dis[k]+e[x].w;

				q.push(e[x].to);

				book[e[x].to]=1;

				vis[e[x].to]=1;

			}

			else{

				if(dis[e[x].to]!=dis[k]+e[x].w){

					return false;

				}

			}

		}

	}

	return true;

}
// Slower C++ solution:
# include <cstring>

# include <cstdio>

# include <iostream>

# include <queue>

# define N 100050 //N为顶点个数 

# define M 200050 //N为顶点个数 

# define WQD 0x7f7f7f7f//无穷大 

int n;//图的顶点个数

int m;//图的边数 

struct Edge{int next,to, w;}  e[M<<1];//不用0号单元 

int head[N+1]; 

int dis[N+1];//存放源点到其他点的路径

bool vis[N+1];

bool book[N+1];

bool spfa(int s);//差分约束判断 

using namespace std;

int main()

{

	ios::sync_with_stdio ( false ) ;

	int u,v,w;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	for(int i=1; i<=m; i++)  //本题约束关系为=，要添加两条边 

	{

		cin>>u>>v>>w;//输入第i条边 ,i为编号 

		e[i]=(Edge){head[u],v,w};head[u]=i;

		e[m+i]=(Edge){head[v],u,-w};head[v]=m+i;

	}

	memset(book,0,sizeof(book));

	memset(dis,0x7f,sizeof(dis));

	for(int i=1; i<=n; i++)//每一片的连通区域都要判断 

		if(!vis[i]) 

		{

			vis[i]=1;

			if(spfa(i)==false)  {cout<<"No";return 0;}//有约束自相矛盾 

		}

	cout<<"Yes";//所以的点都符合条件 

	return 0;

}

bool spfa(int s)

{

 	queue<int>q;

 	//memset(book,0,sizeof(book));

	memset(dis,0x7f,sizeof(dis));

	dis[s]=0;

	q.push(s);book[s]=1;	

	//如果没松弛就松弛，如果松弛过就判断是否符合约束条件，如果自相矛盾就是不可能的数据

	while(q.size()>0) 

	{

		int k=q.front();q.pop();//对头元素出队 

		book[k]=0;

		for(int i=head[k]; i!=-1; i=e[i].next)

		{

			int v=e[i].to;

			if(dis[v]==WQD && dis[k]<WQD)//如果v点没松弛过，就松弛更新dis[v]的值 				

			{	//题目的约束条件是= 			

				dis[v]=dis[k]+e[i].w;//更新 

				if(book[v]==0)	{q.push(v);	book[v]=1;}//入队标记

				vis[v]=1;//标记该点已经松弛过 

			}

			//据题意理解差分系统约束条件，不相等，自相矛盾 

			else if(dis[v]<WQD && dis[k]<WQD && dis[v]!=dis[k]+e[i].w)	return false;

		}		

    }

	return true;//别掉了这句 

}

// Faster C++ solution for the same problem (measured speedup: 1.9735955655346618x):
#include<bits/stdc++.h>

using namespace std;

int n,m;

struct edge{

	int next;

	int to;

	int w;

}e[4000020];

int head[2000020];

bool dijia(int s);

int dis[2000020];

bool book[2000020],vis[2000020];

int main(){

	scanf("%d %d",&n,&m);

	int u,v,w;

	for(int x=1;x<=m;x++){

		scanf("%d %d %d",&u,&v,&w);

		e[x].to=v;

		e[x].w=w;

		e[x].next=head[u];

		head[u]=x;

		e[x+m].next=head[v];

		e[x+m].to=u;

		e[x+m].w=-w;

		head[v]=x+m;

	}

	for(int x=1;x<=n;x++){

		if(vis[x]){

			continue;

		}

		if(!dijia(x)){

			printf("No");

			return 0;

		}

		else{

			continue;

		}

	}

	printf("Yes");

	return 0;

}

bool dijia(int s){

	dis[s]=0;

	queue<int> q;

	q.push(s);

	book[s]=1;

	while(q.size()){

		int k=q.front();

		q.pop();

		book[k]=0;

		for(int x=head[k];x;x=e[x].next){

			if(!vis[e[x].to]){

				dis[e[x].to]=dis[k]+e[x].w;

				q.push(e[x].to);

				book[e[x].to]=1;

				vis[e[x].to]=1;

			}

			else{

				if(dis[e[x].to]!=dis[k]+e[x].w){

					return false;

				}

			}

		}

	}

	return true;

}
// Slower C++ solution:
# include <cstring>

# include <cstdio>

# include <iostream>

# include <queue>

# define N 100050 //N为顶点个数 

# define M 200050 //N为顶点个数 

# define WQD 0x7f7f7f7f//无穷大 

int n;//图的顶点个数

int m;//图的边数 

struct Edge{int next,to, w;}  e[M<<1];//不用0号单元 

int head[N+1]; 

int dis[N+1];//存放源点到其他点的路径

bool vis[N+1];

bool book[N+1];

bool spfa(int s);//差分约束判断 

using namespace std;

int main()

{

	ios::sync_with_stdio ( false ) ;

	int u,v,w;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	for(int i=1; i<=m; i++)  //本题约束关系为=，要添加两条边 

	{

		cin>>u>>v>>w;//输入第i条边 ,i为编号 

		e[i]=(Edge){head[u],v,w};head[u]=i;

		e[m+i]=(Edge){head[v],u,-w};head[v]=m+i;

	}

	memset(book,0,sizeof(book));

	memset(dis,0x7f,sizeof(dis));

	for(int i=1; i<=n; i++)//每一个点都要校验到 

		if(!vis[i]) //如果i点没有校验，就用spfa校验和i点连通的区域 

		{

			vis[i]=1;

			if(spfa(i)==false)  {cout<<"No";return 0;}//有约束自相矛盾 

		}

	cout<<"Yes";//所有的点都符合条件 

	return 0;

}

bool spfa(int s)

{

 	queue<int>q;

 	//memset(book,0,sizeof(book));

	//memset(dis,0x7f,sizeof(dis));

	dis[s]=0;

	q.push(s);book[s]=1;	

	//如果没松弛就松弛，如果松弛过就判断是否符合约束条件，如果自相矛盾就是不可能的数据

	while(q.size()>0) 

	{

		int k=q.front();q.pop();//对头元素出队 

		book[k]=0;

		for(int i=head[k]; i!=-1; i=e[i].next)

		{

			int v=e[i].to;

			//题目的约束条件是= ，不相等，自相矛盾 

			if(dis[v]<WQD && dis[k]<WQD && dis[v]!=dis[k]+e[i].w)	return false;

			else if(dis[v]==WQD && dis[k]<WQD)//如果v点没松弛过，就松弛更新dis[v]的值 				

			{				

				dis[v]=dis[k]+e[i].w;//更新 

				if(book[v]==0)	{q.push(v);	book[v]=1;}//入队标记

				vis[v]=1;//标记该点已经检验过 

			}

			

		}		

    }

	return true;//别掉了这句 

}

// Faster C++ solution for the same problem (measured speedup: 1.9293053606511368x):
#include<bits/stdc++.h>

using namespace std;

int n,m;

struct edge{

	int next;

	int to;

	int w;

}e[4000020];

int head[2000020];

bool dijia(int s);

int dis[2000020];

bool book[2000020],vis[2000020];

int main(){

	scanf("%d %d",&n,&m);

	int u,v,w;

	for(int x=1;x<=m;x++){

		scanf("%d %d %d",&u,&v,&w);

		e[x].to=v;

		e[x].w=w;

		e[x].next=head[u];

		head[u]=x;

		e[x+m].next=head[v];

		e[x+m].to=u;

		e[x+m].w=-w;

		head[v]=x+m;

	}

	for(int x=1;x<=n;x++){

		if(vis[x]){

			continue;

		}

		if(!dijia(x)){

			printf("No");

			return 0;

		}

		else{

			continue;

		}

	}

	printf("Yes");

	return 0;

}

bool dijia(int s){

	dis[s]=0;

	queue<int> q;

	q.push(s);

	book[s]=1;

	while(q.size()){

		int k=q.front();

		q.pop();

		book[k]=0;

		for(int x=head[k];x;x=e[x].next){

			if(!vis[e[x].to]){

				dis[e[x].to]=dis[k]+e[x].w;

				q.push(e[x].to);

				book[e[x].to]=1;

				vis[e[x].to]=1;

			}

			else{

				if(dis[e[x].to]!=dis[k]+e[x].w){

					return false;

				}

			}

		}

	}

	return true;

}
// Slower C++ solution:
# include <cstring>

# include <cstdio>

# include <iostream>

# include <queue>

# define N 100050

# define M 200050 

# define WQD 0x7f7f7f7f

int n;

int m;

struct Edge{int next,to, w;}  e[M<<1];

int head[N+1]; 

int dis[N+1];

bool vis[N+1];

bool book[ N + 1 ] ;

bool spfa(int s);

using namespace std;

int main()

{

	memset(dis,0x7f,sizeof(dis));

	memset(book,0,sizeof(book));

	ios::sync_with_stdio ( false ) ;

	int u,v,w;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	for(int i=1; i<=m; i++)

	{

		cin>>u>>v>>w;

		e[i]=(Edge){head[u],v,w};head[u]=i;

		e[m+i]=(Edge){head[v],u,-w};head[v]=m+i;

	}

	for(int i=1; i<=n; i++)

		if(!vis[i]) 

		{

			vis[i]=1;

			if(!spfa(i))  {cout<<"No";return 0;}

		}

	cout<<"Yes";

	return 0;

}

bool spfa(int s)

{

 	queue<int>q;

//	memset(book,0,sizeof(book));

//	memset(dis,0x7f,sizeof(dis));

	dis[s]=0;

	q.push(s);book[s]=1;

	while(q.size()>0)

	{

		int k=q.front();q.pop();

		book[k]=0;

		for(int i=head[k]; i!=-1; i=e[i].next)

		{

			int v=e[i].to;

			if(dis[v]==WQD ) 

			{				

				dis[v]=dis[k]+e[i].w;

				if( ! book[v])	{q.push(v);	book[v]=1;}

				vis[v]=1;

			}

			else if(  (dis[v]!=dis[k]+e[i].w) )	return false;

		}		

    }

	return true;

}

// Faster C++ solution for the same problem (measured speedup: 1.9323359833950302x):
#include<cstdio>

#include<iostream>

#include<cstring>

#include<algorithm>

#include<queue>

int dis[100005],flag[100005],vis[100005],h[100005];

int n,m,cnt;

struct wf{

    int to,nxt,w;

}edge[200005*2];

using namespace std;

queue < int > Q;

void add(int u,int v,int w)

{

    edge[++cnt].to=v;

    edge[cnt].w=w;

    edge[cnt].nxt=h[u];

    h[u]=cnt;

}

bool spfa(int s)

{

    dis[s]=0;

    //vis[s]=1;

    Q.push(s);

    flag[s]=1;

    while(!Q.empty())

    {

        int u=Q.front();

        Q.pop();

        //vis[u]=0;

        for (int j=h[u];j;j=edge[j].nxt)

        {

            int v=edge[j].to;

            if (dis[v]==99999999)

            {

                dis[v]=dis[u]+edge[j].w;

                vis[v]=1;

                if (!flag[v]) Q.push(v);

            }

            else if (dis[v]!=dis[u]+edge[j].w) return false;

        }

        flag[u]=0;

    }

    return true;

}

int main()

{

    scanf("%d%d",&n,&m);

    for (int i=0;i<=n;i++)

	  dis[i]=99999999;

    for (int i=1;i<=m;i++)

    {

        int k,l,q;

        scanf("%d%d%d",&k,&l,&q);

        add(k,l,q);

        add(l,k,-q);

    }

    for (int i=1;i<=n;i++)

    {

        if (!vis[i])

        {

            vis[i]=1;

            if (!spfa(i))

            {

                printf("No");

                return 0;

            }

        }

    }

    printf("Yes");

    //while(1);

    return 0;

}
// Slower C++ solution:
# include <cstring>

# include <cstdio>

# include <iostream>

# include <queue>

# define N 100050 //N为顶点个数 

# define M 200050 //N为顶点个数 

# define WQD 0x7f7f7f7f//无穷大 

int n;//图的顶点个数

int m;//图的边数 

struct Edge{int next,to, w;}  e[M<<1];//不用0号单元 

int head[N+1]; 

int dis[N+1];//存放源点到其他点的路径

bool vis[N+1];

bool book[N+1];

bool spfa(int s);//差分约束判断 

using namespace std;

int main()

{

	ios::sync_with_stdio ( false ) ;

	int u,v,w;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	for(int i=1; i<=m; i++)  //本题约束关系为=，要添加两条边 

	{

		cin>>u>>v>>w;//输入第i条边 ,i为编号 

		e[i]=(Edge){head[u],v,w};head[u]=i;

		e[m+i]=(Edge){head[v],u,-w};head[v]=m+i;

	}

	memset(book,0,sizeof(book));

	memset(dis,0x7f,sizeof(dis));

	for(int i=1; i<=n; i++)

		if(!vis[i]) 

		{

			vis[i]=1;

			if(spfa(i)==false)  {cout<<"No";return 0;}

		}

	cout<<"Yes";

	return 0;

}

bool spfa(int s)

{

 	queue<int>q;

 	//memset(book,0,sizeof(book));

	//memset(dis,0x7f,sizeof(dis));

	dis[s]=0;

	q.push(s);book[s]=1;

	while(q.size()>0)//如果没松弛就松弛，如果松弛过就判断是否符合约束条件，如果自相矛盾就是不可能的数据 

	{

		int k=q.front();q.pop();//对头元素出队 

		book[k]=0;

		for(int i=head[k]; i!=-1; i=e[i].next)

		{

			int v=e[i].to;

			if(dis[v]==WQD )//如果v点没松弛过，就松弛更新dis[v]的值 

				//题目的约束条件是= 

				{				

					dis[v]=dis[k]+e[i].w;//更新 

					if(book[v]==0)	{q.push(v);	book[v]=1;}//入队标记

					vis[v]=1;//标记该点已经松弛过 

				}

			else if(dis[v]!=dis[k]+e[i].w)	return false;//这里也可以用不等于，根据题意理解差分系统约束条件 

		}		

    }

	return true;//别掉了这句 

}

// Faster C++ solution for the same problem (measured speedup: 1.9299549421203732x):
#include<cstdio>

#include<iostream>

#include<cstring>

#include<algorithm>

#include<queue>

int dis[100005],flag[100005],vis[100005],h[100005];

int n,m,cnt;

struct wf{

    int to,nxt,w;

}edge[200005*2];

using namespace std;

queue < int > Q;

void add(int u,int v,int w)

{

    edge[++cnt].to=v;

    edge[cnt].w=w;

    edge[cnt].nxt=h[u];

    h[u]=cnt;

}

bool spfa(int s)

{

    dis[s]=0;

    //vis[s]=1;

    Q.push(s);

    flag[s]=1;

    while(!Q.empty())

    {

        int u=Q.front();

        Q.pop();

        //vis[u]=0;

        for (int j=h[u];j;j=edge[j].nxt)

        {

            int v=edge[j].to;

            if (dis[v]==99999999)

            {

                dis[v]=dis[u]+edge[j].w;

                vis[v]=1;

                if (!flag[v]) Q.push(v);

            }

            else if (dis[v]!=dis[u]+edge[j].w) return false;

        }

        flag[u]=0;

    }

    return true;

}

int main()

{

    scanf("%d%d",&n,&m);

    for (int i=0;i<=n;i++)

	  dis[i]=99999999;

    for (int i=1;i<=m;i++)

    {

        int k,l,q;

        scanf("%d%d%d",&k,&l,&q);

        add(k,l,q);

        add(l,k,-q);

    }

    for (int i=1;i<=n;i++)

    {

        if (!vis[i])

        {

            vis[i]=1;

            if (!spfa(i))

            {

                printf("No");

                return 0;

            }

        }

    }

    printf("Yes");

    //while(1);

    return 0;

}