题意:
N,个点,M条路,W个虫洞,虫洞的边就是负的
如果出现负环代表YES
#include <iostream> #include <string.h> #include <queue> #include <vector> #include <utility> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define maxn 1000 const int INF = 1111111111; int N,M,W; int mp[maxn][maxn]; int dis[maxn]; int num[maxn]; int vis[maxn]; void init() { for(int i = 1; i <= N; i++) fill(mp[i],mp[i]+N+1,INF); fill(dis,dis+N+1,INF); fill(num,num+N+1,0); } int SPFA(int s) { queue<int> que; dis[s] = 0; que.push(s); while(!que.empty()) { int u = que.front(); que.pop(); if(++num[u] >= N) return 1; for(int i = 1; i <= N; i++) { if(dis[i] > dis[u] + mp[u][i]) { dis[i] = dis[u] + mp[u][i]; que.push(i); } } } return 0; } int main() { #ifdef xxz freopen("in.txt","r",stdin); #endif // xxz int T,S,E,Case; scanf("%d",&Case); while(Case--) { scanf("%d%d%d",&N,&M,&W); init();//好坑啊,把这个函数写到上面了,WA了十多发才发现....晕 for(int i = 0; i < M; i++) { scanf("%d%d%d",&S,&E,&T); if(T < mp[S][E]) mp[S][E] = mp[E][S] = T; } for(int i = 0; i < W; i++) { scanf("%d%d%d",&S,&E,&T); mp[S][E] = -T; } if(SPFA(1)) printf("YES\n"); else printf("NO\n"); } return 0; }
时间: 2024-10-27 06:23:35