原题链接:
解析:找了四五小时bug,打错俩个字母,想吃口屎冷静一下。
代码示例:
#include#include #include #include #include using namespace std;const int INF = 2e9;const int maxn = 1005;vector G[maxn]; struct Edge{ int from,to,dis;};vector edges;int dist[maxn];bool vis[maxn];int cnt[maxn];int n;void add_edge(int from,int to,int dis){ edges.push_back(Edge{from,to,dis}); int m = edges.size()-1; G[from].push_back(m);}void init(){ for(int i = 0;i < maxn;i++) G[i].clear(); for(int i = 0;i < maxn;i++) dist[i] = INF; edges.clear(); memset(vis,false,sizeof vis); memset(cnt,0,sizeof cnt);}bool spfa(int s){ dist[s] = 0; queue q; q.push(s); while(!q.empty()){ int x = q.front(); q.pop(); vis[x] = false; for(int i = 0;i < G[x].size();i++){ Edge& e = edges[G[x][i]]; if(dist[e.to] > dist[x] + e.dis){ dist[e.to] = dist[x] + e.dis; cnt[e.to] = cnt[x] + 1; if(cnt[e.to] >= n) return false; if(!vis[e.to]) q.push(e.to),vis[e.to] = true; } } } return true;}int main(){ int t; scanf("%d",&t); while(t--){ init(); int m,w; scanf("%d%d%d",&n,&m,&w); for(int i = 0;i < m;i++){ int u,v,val; scanf("%d%d%d",&u,&v,&val); add_edge(u,v,val); add_edge(v,u,val); } for(int i = 0;i < w;i++){ int u,v,val; scanf("%d%d%d",&u,&v,&val); add_edge(u,v,-val); } if(spfa(1)) puts("NO"); else puts("YES"); } return 0;}