mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-23 18:08:47 +00:00
parent
47dc0cc260
commit
e03f98b65e
105
S2OJ/1788/1788.cpp
Normal file
105
S2OJ/1788/1788.cpp
Normal file
@ -0,0 +1,105 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 1e4 + 5,
|
||||
K = 11;
|
||||
const int INF = 0x3f3f3f3f;
|
||||
|
||||
int n, m, d, f[1 << K][N], f1[1 << K];
|
||||
std::vector<std::pair<int, int>> g[N];
|
||||
|
||||
int steiner(int s) {
|
||||
int k = 0;
|
||||
|
||||
memset(f, 0x3f, sizeof(f));
|
||||
|
||||
for (int i = 1; i <= d; i++) {
|
||||
std::queue<int> q;
|
||||
|
||||
if (s & (1 << (i - 1))) {
|
||||
f[1 << k++][i] = 0;
|
||||
f[1 << k++][n - i + 1] = 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int res = INF;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
res = std::min(res, f[(1 << k) - 1][i]);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n >> m >> d;
|
||||
|
||||
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(f1, 0x3f, sizeof(f1));
|
||||
|
||||
f1[0] = 0;
|
||||
|
||||
for (int s = 1; s < 1 << d; s++) {
|
||||
for (int t = s; t; t = (t - 1) & s) {
|
||||
f1[s] = std::min(f1[s], f1[s ^ t] + steiner(t));
|
||||
}
|
||||
}
|
||||
|
||||
cout << (f1[(1 << d) - 1] == INF ? -1 : f1[(1 << d) - 1]) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
S2OJ/1788/data/problem.conf
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/problem.conf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road1.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road1.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road10.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road10.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road2.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road2.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road3.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road3.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road4.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road4.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road5.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road5.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road6.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road6.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road7.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road7.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road8.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road8.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road9.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1788/data/road9.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1788/data/road9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user