QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#359486 | #410. Telegraph | KrzychXyz | 0 | 1ms | 3700kb | C++14 | 2.3kb | 2024-03-20 18:27:38 | 2024-03-20 18:28:31 |
answer
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100002;
const long long inf = 9223372036854775800;
int N;
int kier[maxN];
long long ceny[maxN];
int wchd[maxN];
int wchdMin = 0;
set<int> cyklsofar;
set<int> thispath;
long long minonpath = 0;
long long koszt;
/*void debug() {
cout << wchdMin << " " << cyklsofar.size() << "\n";
for(auto kv : cyklsofar) cout << kv << " ";
cout << "\n";
}*/
void input() {
cin >> N;
for(int i = 1; i <= N; i++) {
cin >> kier[i] >> ceny[i];
}
ceny[0] = inf;
}
void findinitial() {
int curr = 1;
while(1) {
cyklsofar.insert(curr);
curr = kier[curr];
if(cyklsofar.count(curr)) {
cyklsofar.clear();
break;
}
}
cyklsofar.insert(curr);
int revval = 0;
while(1) {
revval = curr;
curr = kier[curr];
wchd[curr] = revval;
if(ceny[revval] < ceny[wchdMin]) wchdMin = revval;
if(cyklsofar.count(curr)) {
break;
}
cyklsofar.insert(curr);
}
//debug();
}
void addvert(int X) {
if(cyklsofar.count(X)) return;
long long kosztA = ceny[wchd[kier[X]]];;
long long kosztB = ceny[X] + ceny[wchdMin];
if(kosztA > kosztB) {
wchd[X] = wchdMin;
wchd[kier[wchdMin]] = X;
kier[X] = kier[wchdMin];
kier[wchdMin] = X;
ceny[X] = 0;
ceny[wchdMin] = 0;
koszt+= kosztB;
}
else {
ceny[wchd[kier[X]]] = 0;
kier[wchd[kier[X]]] = X;
wchd[X] = wchd[kier[X]];
wchd[kier[X]] = X;
koszt+=kosztA;
}
cyklsofar.insert(X);
}
void addtocycle(int X) {
if(!cyklsofar.count(kier[X])) {
if (thispath.count(X)) {
addvert(minonpath);
}
if(ceny[minonpath] > ceny[X]) minonpath = X;
thispath.insert(X);
addtocycle(kier[X]);
}
if(cyklsofar.count(kier[X])) {
addvert(X);
}
}
signed main() {
//ios_base::sync_with_stdio(0);
//cin.tie(0);
input();
findinitial();
for(int i = 1; i <= N; i++) {
if(!cyklsofar.count(i)) {
addtocycle(i);
}
}
cout << koszt << "\n";
//cout << koszt << "\n";
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3700kb
input:
10 3 85377744 3 191391530 4 553475509 8 344887257 9 812158120 1 880268352 4 686078706 7 182546394 4 220137367 3 89957933
output:
1264098144
result:
wrong answer 1st lines differ - expected: '1081551750', found: '1264098144'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%