因为有重边,请在判断完全可以松弛后再判断松弛次数是否过限。
即
if(dis[j] > dis[x] + val[i]){ dis[j] = dis[x] + val[i]; if(!vis[j]){ cnt[j] ++; if(cnt[j] > n){ cout << -1; exit(0); } q.push(j); vis[j] = 1; } }