版本比较
负环
正文
增加 40 行删除 40 行
15 行未改动1617181920202122232425262627282930313232333435362 行未改动3940414243434445454647484915 行未改动656667686969707172727374757576777879808182838384858687888889909192939494959697989910010110110210310410510610610710810911011111111211311411511511611711811912012112212212312412512612612712812913013113113213313413513613713813913914014114114214314414514614714814914915015115115215315315415515615715815916016116216316316416516516616716816912 行未改动1821831841851861861871881891905 行未改动19619719819920020020120220320420520520620720820921021121121221321421521621721821821922022122222322322422522622722822923023123223223323423523623723823923924024124224324324424524624724824824925025125225325425525625725825825926026126226326426526526626726726826927027127227327427527627727727827928028128128215 行未改动void dfs(int x){ if(x > n) { if(!(suan == 1 && ku == 0)) if(!(suan == 1 && ku == 0)) { mini = min(mini, abs(suan - ku)); } return ; }
suan *= s[x]; ku += b[x]; dfs(x + 1); suan /= s[x]; ku -= b[x];
dfs(x + 1);}
int main()2 行未改动 for(int i = 1; i <= n; i++) { cin >> s[i] >> b[i]; }
dfs(1);
cout << mini; return 0;}```15 行未改动struct Node{ int x; double w;
bool operator < (const Node & tmp) const { return w > tmp.w; return w > tmp.w; }};vector<Node> g[35];vector<Node> g[35];
bool spfa(int s){ memset(dis, 0, sizeof dis); memset(cnt, 0, sizeof cnt); memset(vis, 0, sizeof vis); queue<int> q;
q.push(s); dis[s] = 1; cnt[s] = 0; vis[s] = 1;
while(q.empty() == 0) { int c = q.front(); q.pop(); vis[c] = 0;
for(int i = 0; i < g[c].size(); i++) { if(dis[g[c][i].x] < dis[c] * g[c][i].w) { dis[g[c][i].x] = dis[c] * g[c][i].w; cnt[g[c][i].x] = cnt[c] + 1;
if(cnt[g[c][i].x] == n) { return 1; }
if(vis[g[c][i].x] == 0) { vis[g[c][i].x] = 1; q.push(g[c][i].x); } } } } }
return 0;}
int main(){ int cnt = 0;
while(cin >> n) { cnt++;
if(n == 0) { return 0; }
for(int i = 1; i <= n; i++) { string s; cin >> s; a[s] = i; g[a[s]].clear(); }
cin >> m;
for(int i = 1; i <= m; i++) { string s1, s2; double x; cin >> s1 >> x >> s2; g[a[s1]].push_back({a[s2], x}); }
string o = "No";
cout << "Case " << cnt << ": ";
for(int i = 1; i <= n; i++) { bool v = spfa(i); if(v == 1) { o = "Yes"; break; } }
cout << o << "\n"; } } return 0;}```# [P2850 [USACO06DEC] Wormholes G](https://www.luogu.com.cn/problem/P2850)12 行未改动
struct Node{ int x, w;
bool operator < (const Node & tmp) const { return w > tmp.w; }5 行未改动 memset(dis, 0x3f, sizeof dis); memset(vis, 0, sizeof vis); memset(cnt, 0, sizeof cnt); queue<int> q;
dis[s] = 0; vis[s] = 1; cnt[s] = 0; q.push(s);
while(q.empty() == 0) { int c = q.front(); q.pop(); vis[c] = 0;
for(int i = 0; i < g[c].size(); i++) { if(dis[g[c][i].x] > dis[c] + g[c][i].w) { dis[g[c][i].x] = dis[c] + g[c][i].w; cnt[g[c][i].x] = cnt[c] + 1;
if(cnt[g[c][i].x] == n) { return 1; }
if(vis[g[c][i].x] == 0) { vis[g[c][i].x] = 1; q.push(g[c][i].x); } } } }
return 0;}
signed main(){ cin >> f;
while(f--) { cin >> n >> m >> w;
for(int i = 1; i <= n; i++) { g[i].clear(); }
memset(a, 0x3f, sizeof a); for(int i = 1; i <= m; i++) { int x, y, t; cin >> x >> y >> t; a[x][y] = a[y][x] = min(a[x][y], t); g[x].push_back({y, a[x][y]}); g[y].push_back({x, a[x][y]}); }
for(int i = 1; i <= w; i++) { int x, y, t; cin >> x >> y >> t; g[x].push_back({y, -t}); }
string s = "NO";
for(int i = 1; i <= n; i++) { bool v = spfa(i); if(v == 1) { s = "YES"; break; } }
cout << s << "\n"; } return 0;}} ```