求调-bellman-ford
查看原帖
求调-bellman-ford
677142
limeng911楼主2024/3/31 11:54
#include <bits/stdc++.h>
using namespace std;
struct edge{
	int u, v, w;
};
const int N=1e4+5;
int dis[N];int x=0;
vector<edge> e;
void man(int s,int n)
{
	
	for (int i = 1; i <= n; i++) dis[i] = 1e9;
	dis[s] = 0;
	for (int i = 1; i <= n; i++) {
		for (auto it : e){
			int u1 = it.u, v1 = it.v, w1 = it.w;
			if (dis[v1] > dis[u1] + w1){
				dis[v1] = dis[u1] + w1;
				if(i==n ) {
					x=1;
				}
			}
		}
	}
	
	/*for (int i = 1; i <= n; i++) cout << dis[i] << " ";*/
}
int main(){
	int u,v,w;
	int n,m,s;
	int shu;
	cin>>shu;
	while(shu--){
		cin>>n>>m;
		for(int i=1;i<=m;i++){
			cin>>u>>v>>w;
			e.push_back({u,v,w});
			if(x==1) cout<<"Yes";
			else cout<<"No";
		}
		man(1,n);
	}
	
    return 0;
}
2024/3/31 11:54
加载中...