0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 18:28:47 +00:00

#1586. 【2022.9.21 NOIP 模拟赛】口粮输送

https://sjzezoj.com/submission/57466
This commit is contained in:
Baoshuo Ren 2022-09-21 22:21:52 +08:00
parent 4a421c5716
commit b7526c13af
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
30 changed files with 215 additions and 0 deletions

128
S2OJ/1586/1586.cpp Normal file
View File

@ -0,0 +1,128 @@
#include <iostream>
#include <algorithm>
#include <array>
#include <numeric>
#include <tuple>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int INF = 0x3f3f3f3f;
class DSU {
private:
std::vector<int> fa;
public:
DSU(int _n = 0)
: fa(_n) {
std::iota(fa.begin(), fa.end(), 0);
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
bool merge(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
return x != y;
}
};
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
std::vector<int> a(n), b(n);
std::vector<std::tuple<int, int, int>> edges;
std::vector<bool> f(1 << n, false);
for (int i = 0, u, v, w; i < m; i++) {
cin >> u >> v >> w;
edges.emplace_back(u - 1, v - 1, w);
}
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
auto check = [&](int s) -> bool {
auto in_block = [&](int x) -> bool {
return s >> x & 1;
};
int sum = 0, root = 0;
DSU dsu(n);
std::vector<std::tuple<int, int, int>> block_edges;
for (auto& edge : edges) {
int u, v;
std::tie(u, v, std::ignore) = edge;
if (in_block(u) && in_block(v)) {
block_edges.emplace_back(edge);
}
}
std::sort(block_edges.begin(), block_edges.end(), [](auto& lhs, auto& rhs) {
return std::get<2>(lhs) < std::get<2>(rhs);
});
for (int i = 0; i < n; i++) {
if (in_block(i)) sum += a[i] - b[i];
}
for (auto& edge : block_edges) {
int u, v, w;
std::tie(u, v, w) = edge;
if (dsu.merge(u, v)) sum -= w;
}
for (int i = 0; i < n; i++) {
if (in_block(i) && dsu.find(i) == i) {
root = i;
}
}
for (int i = 0; i < n; i++) {
if (in_block(i) && dsu.find(i) != root) {
return false;
}
}
return sum >= 0;
};
f[0] = true;
for (int s = 1; s < 1 << n; s++) {
f[s] = check(s);
for (int i = s; i; i = (i - 1) & s) {
f[s] = f[s] || f[i] && f[s ^ i];
}
}
cout << (f[(1 << n) - 1] ? "Yes" : "No") << endl;
}
return 0;
}

BIN
S2OJ/1586/data/ex_trans1.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/ex_trans1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/ex_trans2.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/ex_trans2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/ex_trans3.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/ex_trans3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/ex_trans4.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/ex_trans4.in (Stored with Git LFS) Normal file

Binary file not shown.

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

Binary file not shown.

BIN
S2OJ/1586/data/trans1.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans10.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans2.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans3.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans4.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans5.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans6.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans7.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans8.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans9.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1586/data/trans9.in (Stored with Git LFS) Normal file

Binary file not shown.