0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

#161. 【模板】最小斯坦纳树

https://sjzezoj.com/submission/73105
This commit is contained in:
Baoshuo Ren 2023-03-02 21:37:04 +08:00
parent bcc7add1ea
commit 37cb0c04e9
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
24 changed files with 153 additions and 0 deletions

84
S2OJ/161/161.cpp Normal file
View File

@ -0,0 +1,84 @@
#include <iostream>
#include <cstring>
#include <queue>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 105,
K = 12;
const int INF = 0x3f3f3f3f;
int n, m, k, a[K], f[1 << K][N], ans = INF;
std::vector<std::pair<int, int>> g[N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
for (int i = 1, u, v, w; i <= m; i++) {
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
memset(f, 0x3f, sizeof(f));
for (int i = 1; i <= k; i++) {
cin >> a[i];
f[1 << (i - 1)][a[i]] = 0;
}
for (int s = 0; s < 1 << k; s++) {
std::queue<int> q;
std::vector<bool> vis(n + 1);
for (int i = 1; i <= n; i++) {
for (int t = s & (s - 1); t; t = (t - 1) & s) {
f[s][i] = std::min(f[s][i], f[t][i] + f[s ^ t][i]);
}
if (f[s][i] != INF) {
q.emplace(i);
vis[i] = true;
}
}
// SPFA
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (auto e : g[u]) {
int v = e.first,
w = e.second;
if (f[s][v] > f[s][u] + w) {
f[s][v] = f[s][u] + w;
if (!vis[v]) {
q.emplace(v);
vis[v] = true;
}
}
}
}
}
for (int i = 1; i <= k; i++) {
ans = std::min(ans, f[(1 << k) - 1][a[i]]);
}
cout << ans << endl;
return 0;
}

BIN
S2OJ/161/data/graph1.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph10.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph2.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph3.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph4.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph5.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph6.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph7.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph8.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph9.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/graph9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/problem.conf (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/std.cpp (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/161/data/val.cpp (Stored with Git LFS) Normal file

Binary file not shown.