蒟蒻90分求救
查看原帖
蒟蒻90分求救
499231
Jacky2009楼主2021/11/18 00:36
#include<bits/stdc++.h>
using namespace std;
struct edge{
	int to,w,next;
}e[100005];
int head[100005],cnt,n,m;
void adde(int x,int y,int w){
	e[cnt++].to=y;
	e[cnt].next=head[x];
	e[cnt].w=w;
	head[x]=cnt;
}
int main(){
	cin>>n>>m;
	memset(head,-1,sizeof(head));
	for(int i=1;i<=m;i++){
		int x,y,z,c;
		cin>>z>>x>>y;
		if(z==3){
			adde(x,y,0);
			adde(y,x,0);
			cout<<x<<" "<<y<<" "<<0<<endl<<y<<" "<<x<<" "<<0<<endl;
		}
		if(z==1){
			cin>>c;
			adde(x,y,-c);
			cout<<x<<" "<<y<<" "<<-c<<endl;
		}
		if(z==2){
			cin>>c;
			adde(y,x,c);
			cout<<y<<" "<<x<<" "<<c<<endl;
		}
	}
	int b=n+1;
	for(int i=1;i<=n;i++)adde(b,i,0);
	queue<int>q;
	int dis[100005],mark[100005];
	for(int i=1;i<=n;i++)dis[i]=1073741824;
	memset(mark,0,sizeof(mark));
	dis[b]=0;
	mark[b]=1;
	bool vis[100005];
	memset(vis,0,sizeof(vis));
	vis[b]=1;
	q.push(b);
	while(!q.empty()){
		int top=q.front();
		q.pop();
		vis[top]=0;
		for(int i=head[top];i!=-1;i=e[i].next){
			if(dis[e[i].to]>dis[top]+e[i].w){
				dis[e[i].to]=dis[top]+e[i].w;
				mark[e[i].to]++;
					
				if(mark[e[i].to]>n){
					cout<<"No";
					return 0;
				}
				if(vis[e[i].to]==0){
					vis[e[i].to]=1;
					q.push(e[i].to);
				}
				
			
			}
		}
	}
	cout<<"Yes";
	return 0;
}
2021/11/18 00:36
加载中...