#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair P; int main() { int n, m; cin>>n>>m; vector

g[1010]; int u[2525], v[2525], w[2525]; vector

wp(m); for(int i=0; i>u[i]>>v[i]>>w[i]; u[i]--; v[i]--; g[u[i]].push_back({v[i], i}); wp[i]=P(w[i], i); } sort(wp.begin(), wp.end()); int ans[1010]; fill(ans, ans+n, -2); int d[1010]; const int INF=1e9; fill(d, d+n, INF); d[0]=0; for(int i=0; id[x]+1){ d[y]=d[x]+1; } } } } for(int i=0; i que1; priority_queue, greater

> que; int i1=wp[i].second; int d2[1010]; fill(d2, d2+n, INF); d2[v[i1]]=0; que.push({0, v[i1]}); if(d[u[i1]]>=-INF/2 && ans[u[i1]]!=-1){ que.push({v[i1], 0}); while(!que.empty()){ P p=que.top(); que.pop(); int x=p.second; if(d2[x]d2[x]+e){ d2[y]=d2[x]+e; que.push({d2[y], y}); } } } } if(d2[u[i1]] que2; bool used2[1010]={}; used2[u[i1]]=1; que2.push(u[i1]); while(!que2.empty()){ int x=que2.front(); que2.pop(); for(auto q:g[x]){ int y=q.first; if(!used2[y]){ used2[y]=1; que2.push(y); } } } for(int x=1; xd1[x]+e){ d1[y]=d1[x]+e; que.push({d1[y], y}); } } } for(int x=0; x=-INF/2) d[x]+=d1[x]; else d[x]=-INF; if(ans[x]==-2 && d[x]<=0){ ans[x]=wp[i].first; } } } } for(int x=1; x