题目描述
给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数。
请你判断图中是否存在负权回路。
输入格式
第一行包含整数n和m。
接下来m行每行包含三个整数x,y,z,表示点x和点y之间存在一条有向边,边长为z。
输出格式
如果图中存在负权回路,则输出“Yes”,否则输出“No”。
数据范围
1≤n≤2000,
1≤m≤10000,
图中涉及边长绝对值均不超过10000。
输入样例:
3 3
1 2 -1
2 3 4
3 1 -4
输出样例:
Yes
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1e5+10;
int h[N], e[N], w[N], ne[N], idx;
int d[N], cnt[N];
bool st[N];
int n, m;
void add(int a, int b, int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
bool spfa(){
memset(d, 0x3f, sizeof d);
d[1] = 0;
queue<int> q;
for(int i=1; i<=n; i++){
q.push(i), st[i] = true;
}
while(q.size()){
int t = q.front();
q.pop(), st[t] = false;
for(int i= h[t]; i!=-1; i = ne[i]){
int j = e[i];
if(d[j]>d[t]+w[i]){
d[j] = d[t]+w[i];
if(++cnt[j] >= n) return true;
if(!st[j]) q.push(j), st[j] = true;
}
}
}
return false;
}
int main(){
memset(h, -1, sizeof h);
int a, b, c;
cin >> n >> m;
while(m--){
cin >> a >> b >> c;
add(a, b, c);
}
if(spfa()) cout<< "Yes";
else cout << "No";
return 0;
}