QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#810154 | #7216. Boxes on tree | Liuxizai | WA | 1ms | 3816kb | C++20 | 3.1kb | 2024-12-11 20:06:57 | 2024-12-11 20:06:58 |
Judging History
answer
#include <bits/stdc++.h>
#define File(name) freopen(#name".in", "r", stdin); freopen(#name".out", "w", stdout);
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<typename T>
inline T read(){
T n = 0; int f = 1; char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)){
n = n * 10 + ch - '0';
ch = getchar();
}
return f * n;
}
template<typename T>
void write(T n){
if(n < 0) return putchar('-'), write(-n);
if(n / 10) write(n / 10);
putchar(n % 10 + '0');
}
void input() {}
template<typename Type, typename... Types>
void input(Type &arg, Types &...args){
arg = read<Type>();
input(args...);
}
namespace Main{
const int N = 105;
int n, m, r, g[N][N], ans, bel[N];
pair<int, int> mn[N];
vector<int> vec;
bool del[N], vis[N];
void merge(int v, int u){
bel[v] = u;
vec.erase(find(vec.begin(), vec.end(), v));
for(int i: vec) g[u][i] = min(g[u][i], g[v][i]);
for(int i: vec) g[i][u] = min(g[i][u], g[i][v]);
}
void Main(){
input(n, m, r);
memset(g, 0x3f, sizeof(g));
for(int i = 0, u, v, w; i < m; i++){
input(u, v, w);
g[u][v] = min(g[u][v], w);
}
vec.resize(n);
iota(vec.begin(), vec.end(), 1);
for(int i: vec) if(i != r){
mn[i] = {1e9, -1};
for(int j: vec) if(i != j) mn[i] = min(mn[i], make_pair(g[j][i], j));
if(mn[i].second == -1) { puts("-1"); return; }
}
vis[r] = true;
iota(bel + 1, bel + n + 1, 1);
while(true){
for(int i: vec) if(i != r) vis[i] = false, mn[i].second = bel[mn[i].second];
bool ok = true;
vector<int> _vec(vec);
for(int i: _vec){
if(i == r) continue;
vector<int> path;
int p = i;
while(!vis[p]){
vis[p] = true;
path.push_back(p);
p = mn[p].second;
}
auto it = find(path.begin(), path.end(), p);
if(it != path.end()){
ok = false;
int s = *it;
while(it != path.end()){
for(int j: vec) g[j][*it] -= mn[*it].first;
ans += mn[*it].first;
if(*it != s) merge(*it, s);
it++;
}
mn[s] = {1e9, -1};
for(int j: vec) if(j != s) mn[s] = min(mn[s], make_pair(g[j][s], j));
if(mn[s].second == -1) { puts("-1"); return; }
}
}
if(ok){
for(int i: vec) if(i != r) ans += mn[i].first;
break;
}
}
write(ans);
return;
}
} // namespace Main
int main(){
#ifdef Liuxizai
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif // Liuxizai
Main::Main();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3816kb
input:
3 1 3 2 1 2 2 3
output:
-1
result:
wrong answer 1st numbers differ - expected: '4', found: '-1'