代码如下
#include <bits/stdc++.h>
const int maxn = 15005;
using namespace std;
int n, m;
int dis[maxn];
int cnt[maxn];
bool vis[maxn];
int x, y, v, pd;
queue<int>st;
vector<pair<int , int >>in[maxn];
int read();
bool spfa(int s) {
vis[s] = true;
dis[s] = 0;
st.push(s);
while (!st.empty()) {
for(int i=1;i<=n;i++){dis[i]=0x7fffffff;}
int now = st.front();
vis[now] = false;
st.pop();
for (pair<int , int > v : in[now]) {
if (dis[v.first] > dis[now] + v.second) {
dis[v.first] = dis[now] + v.second;
if (!vis[v.first]) {
st.push(v.first);
vis[v.first] = true;
cnt[v.first]++;
if (cnt[v.first] >= n-1)return false;
}
}
}
}
return true;
}
int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
n = read(); m = read();
for (int i = 1; i <= n; i++) {in[0].push_back({i, 0});}
for (int i = 1; i <= m; i++) {
pd = read();
x = read(), y = read();
if (pd == 1) {
v = read();
in[x].push_back({y, -v});
} else if (pd == 2) {
in[y].push_back({x, v});
} else if (pd == 3) {
in[y].push_back({x, 0});
in[x].push_back({y, 0});
}
}
if (spfa(0)) {puts("Yes");return 0;};
cout<<"No"<<'\n';
return 0;
}
int read() {
int x = 0, w = 1; char ch = 0;
while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0'); ch = getchar();}
return x * w;
}
就35分QwQ