voiddij(int s){ memset(dis, 0x3f, sizeof(dis)); dis[s] = 0; Q.push(make_pair(0, s)); while (!Q.empty()) { int u = Q.top().second; Q.pop(); if (vis[u]) continue; vis[u] = 1; for (auto [v, w] : e[u]) if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; Q.push(make_pair(dis[v], v)); } } }
inlinevoidadd(int u, int v, int w){ e[u].push_back(make_pair(v, w)); }
intmain(){ n = rd(); int m = rd(), k = rd(); for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(), w = rd(); for (int j = 0; j <= k; ++j) { add(j * n + u, j * n + v, w); add(j * n + v, j * n + u, w); if (j == k) continue; add(j * n + u, (j + 1) * n + v, 0); add(j * n + v, (j + 1) * n + u, 0); } } dij(1); ll ans = 1e18; for (int i = 0; i <= k; ++i) ans = min(ans, dis[i * n + n]); printf("%lld\n", ans); return0; }
voiddij(int s){ memset(dis, 0x3f, sizeof(dis)); dis[s] = 0; Q.push(make_pair(0, s)); while (!Q.empty()) { int u = Q.top().second; Q.pop(); if (vis[u]) continue; vis[u] = 1; for (auto [v, w] : e[u]) if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; Q.push(make_pair(dis[v], v)); } } }
inlinevoidadd(int u, int v, int w){ e[u].push_back(make_pair(v, w)); }
intmain(){ int n = rd(), m = rd(), s = rd(), t = rd(); for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(), w = rd(); char c = getchar(); for (; c != 'G' && c != 'B'; c = getchar()) ; for (int j = 0; j <= 20; ++j) { if (c == 'B') add(j * n + u, v, (w + (1 << j) - 1) / (1 << j)); else add(j * n + u, j * n + v, (w + (1 << j) - 1) / (1 << j)); } } int p = rd(); for (int i = 1; i <= p; ++i) { int x = rd(), c = rd(); for (int j = 0; j < 20; ++j) add(j * n + x, (j + 1) * n + x, c); } dij(s); ll ans = 1e18; for (int i = 0; i <= 20; ++i) ans = min(ans, dis[i * n + t]); printf("%lld\n", ans == 1e18 ? -1 : ans); return0; }
vector<pair<int, int> > e[N]; int dis[N], vis[N]; queue<int> Q;
voidspfa(int s){ memset(dis, 0xcf, sizeof(dis)); dis[s] = 0; Q.push(s); while (!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for (auto [v, w] : e[u]) if (dis[v] < dis[u] + w) { dis[v] = dis[u] + w; if (!vis[v]) {Q.push(v); vis[v] = 1;} } } }
inlinevoidadd(int u, int v, int w){e[u].push_back(make_pair(v, w));}
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { int c = rd(); add(i, n + i, -c); add(n + i, n * 2 + i, c); } for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(), z = rd(); add(u, v, 0); add(n + u, n + v, 0); add(u + 2 * n, v + 2 * n, 0); if (z == 2) {add(v, u, 0); add(n + v, n + u, 0); add(v + 2 * n, u + 2 * n, 0);} } spfa(1); printf("%d\n", max(0, dis[2 * n + n])); return0; }
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(); a[u][v] = 1; } // floyd for (int k = 1; k <= n; ++k) for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (a[i][k] && a[k][j]) a[i][j] = 1; // 统计有多少个点 int cnt = 0; for (int i = 1; i <= n; ++i) { bool f = 1; for (int j = 1; j <= n; ++j) if (i != j && !a[i][j] && !a[j][i]) f = 0; if (f) cnt++; } printf("%d\n", cnt); return0; }
Post title:最短路
Post author:Eva.Q
Create time:2022-07-03 10:21:51
Post link:https://qyy/2022/07/03/Algorithm/co-shortest-path/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.