QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#503440 | #7069. Farm | emt | WA | 186ms | 41536kb | C++20 | 3.4kb | 2024-08-03 18:50:57 | 2024-08-03 18:50:59 |
Judging History
answer
// #pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using ld = long double;
using i128 = __int128_t;
using u128 = __uint128_t;
#define int long long
#define mem(a, b) memset((a), (b), sizeof(a))
#define all(a) (a).begin(), (a).end()
#define inf 0x3f3f3f3f
#define lowbit(x) (-x) & x
#define debug cout<<"$"
#define ls u << 1
#define rs u << 1 | 1
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
using pii = pair<int,int>;
mt19937 rnd(time(0));
const int MOD = 998244353;
const ld eps = 1;
int qmi(int m, int k){int res = 1, t = m;while (k){if (k & 1) res = (res * t) % MOD;t = t * t % MOD;k >>= 1;}return res;}
const int N = 5e4 + 7;
void solve()
{
int n,m,q;
cin>>n>>m;
vector<array<int,4>> g(m+1);
vector<int> p(n+1);
vector<int> sp(m+1);
for(int i=1;i<=m;i++)
{
g[i][3] = i;
cin>>g[i][1]>>g[i][2]>>g[i][0];
}
cin>>q;
vector<array<int,2>> re(q);
for(auto &[c,d]:re) cin>>c>>d;
for(auto &[c,d]:re) sp[c] = sp[d] = 1;
iota(all(p),0);
auto find = [&] (auto &&self, int u)->int{if(p[u]!=u) p[u] = self(self,p[u]);return p[u];};
auto merge = [&] (int v, int u)->void{p[find(find,u)] = find(find,v);};
for(int i=1;i<=m;i++)
{
auto [c,u,v,id] = g[i];if(find(find,u)==find(find,v)) continue; merge(u,v);
}
for(int i=2;i<=n;i++) if(find(find,1)!=find(find,i)) return (cout<<"-1\n"), void();
iota(all(p),0);
for(int i=1;i<=m;i++) if(sp[i]) merge(g[i][1],g[i][2]);
int co = 0; auto edg = g; sort(edg.begin()+1,edg.end());
vector<int> E;
for(int i=1;i<=m;i++)
{
auto [c,u,v,id] = edg[i];
if(find(find,u)==find(find,v)) continue;
merge(u,v); co += c;
E.push_back(id);
}
vector<int> rst(n+1), bel(n+1);
iota(all(p),0);
for(auto id:E)
{
auto [c,u,v,_] = g[id];
merge(u,v);
}
int idx = 0; for(int i=1;i<=n;i++)
{
auto z = find(find,i); if(!rst[z]) rst[z] = ++idx;
bel[i] = rst[z];
}
map<array<int,2>,int> ma;
for(int i=1;i<=m;i++)
{
auto [c,u,v,id] = g[i]; u = bel[u], v = bel[v];
if(v==u) continue; if(u>v) swap(u,v);
if(!ma.count({u,v})) ma[{u,v}] = c; else ma[{u,v}] = min(ma[{u,v}],c);
}
vector<array<int,3>> left; for(auto [pp,c]:ma)
{
auto [u,v] = pp; left.push_back({c,u,v});
}
// for(auto [c,u,v]:left)
// debug<<u<<" "<<v<<" "<<c<<"\n";
p.resize(idx+1);
int ans = 1e9; for(int i=0;i<1<<q;i++)
{
int cur = 0;
iota(all(p),0);
vector<int> w;
set<int> se;
for(int j=0;j<q;j++) if((i>>j)&1) w.push_back(re[j][0]); else w.push_back(re[j][1]);
for(auto id:w) if(!se.count(id))
{
auto [c,u,v,_] = g[id];
u = bel[u], v = bel[v];
cur += c; merge(u,v);se.insert(id);
}
for(auto [c,u,v]:left)
{
if(find(find,u)==find(find,v)) continue;
merge(u,v);cur += c;
}
ans = min(ans,cur+co);
}
cout<<ans<<"\n";
}
signed main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin>>T;
while (T--)
solve();
}
/*
6677667766776677
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3692kb
input:
4 6 1 1 2 2 4 3 1 1 4 2 4 4 3 2 4 1 3 4 1 1 2
output:
11
result:
ok single line: '11'
Test #2:
score: 0
Accepted
time: 64ms
memory: 23524kb
input:
100000 500000 2516 13348 191 37713 25720 216 41568 13765 877 2116 27917 895 76904 65435 37 73053 24687 44 97127 44338 700 2251 85769 378 95166 20208 42 59303 57463 158 26863 18030 31 58613 6818 2 15455 18106 254 3232 13720 610 85677 16778 650 25618 72746 813 80365 162 47 10930 7403 645 79272 54568 6...
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 132ms
memory: 41536kb
input:
100000 500000 34497 87538 658 69862 2776 861 93620 16992 904 77910 81200 149 83935 83752 880 17602 75791 259 85887 53289 710 4200 79358 181 8518 19264 737 94665 47462 822 50632 51994 143 55224 59127 656 615 92858 150 48450 9465 58 35713 45287 140 64861 32248 517 70296 45113 153 11189 90316 809 40673...
output:
12148224
result:
ok single line: '12148224'
Test #4:
score: 0
Accepted
time: 141ms
memory: 38480kb
input:
1 500000 1 1 963 1 1 349 1 1 157 1 1 6 1 1 312 1 1 377 1 1 783 1 1 42 1 1 18 1 1 327 1 1 499 1 1 824 1 1 343 1 1 798 1 1 193 1 1 667 1 1 378 1 1 641 1 1 692 1 1 622 1 1 584 1 1 590 1 1 324 1 1 858 1 1 914 1 1 601 1 1 734 1 1 61 1 1 559 1 1 681 1 1 825 1 1 888 1 1 585 1 1 55 1 1 818 1 1 190 1 1 278 1...
output:
1605
result:
ok single line: '1605'
Test #5:
score: 0
Accepted
time: 182ms
memory: 38360kb
input:
5 500000 5 1 817 2 1 273 3 5 674 1 5 15 5 2 872 3 4 728 3 2 807 5 3 28 2 5 96 1 5 100 4 2 224 4 4 980 5 5 727 2 2 520 4 1 29 2 1 142 4 2 963 4 4 118 4 4 615 4 3 719 5 3 200 5 2 746 4 2 68 5 4 859 1 3 182 3 4 286 3 1 229 4 1 895 2 1 730 1 2 622 2 4 913 2 1 697 5 5 130 4 5 507 5 2 425 2 4 716 2 1 884 ...
output:
3097
result:
ok single line: '3097'
Test #6:
score: 0
Accepted
time: 182ms
memory: 38452kb
input:
10 500000 3 8 138 10 7 593 4 3 8 7 5 516 10 4 49 3 8 601 6 7 481 8 5 429 6 4 241 1 6 504 6 2 252 7 1 656 5 1 350 5 9 485 7 8 669 5 8 630 9 9 324 1 3 427 1 2 309 5 10 236 4 6 926 8 7 34 5 1 336 7 5 581 4 5 228 10 3 909 2 9 726 4 2 444 10 1 55 1 2 244 5 8 261 2 7 556 10 2 165 6 3 657 7 5 580 7 1 827 1...
output:
1533
result:
ok single line: '1533'
Test #7:
score: 0
Accepted
time: 186ms
memory: 38364kb
input:
100 500000 10 46 133 79 13 987 26 2 743 8 47 390 79 19 737 11 64 197 16 65 207 73 9 944 77 58 841 50 3 245 81 100 293 21 12 713 60 65 155 89 87 865 88 67 278 9 15 920 46 52 704 26 26 731 44 98 525 20 68 346 14 95 932 84 19 697 41 21 290 83 24 750 3 71 369 54 80 396 20 70 208 25 55 456 40 22 938 90 1...
output:
2209
result:
ok single line: '2209'
Test #8:
score: 0
Accepted
time: 24ms
memory: 4248kb
input:
1000 10000 186 620 701 360 808 963 181 434 297 873 511 550 949 641 670 36 318 299 635 543 56 284 519 439 816 900 877 84 189 141 393 679 222 169 669 974 826 703 651 201 659 644 1000 388 69 263 104 625 278 386 526 35 262 697 776 871 702 407 153 783 130 857 596 78 140 46 391 173 636 8 419 879 804 197 7...
output:
60430
result:
ok single line: '60430'
Test #9:
score: 0
Accepted
time: 39ms
memory: 10276kb
input:
1000 100000 64 501 272 114 900 116 858 404 897 442 351 101 232 419 117 803 929 38 451 759 769 39 387 881 906 961 105 82 652 795 657 958 636 55 986 248 623 912 446 326 513 863 886 207 977 908 104 591 932 132 666 239 166 495 772 97 650 22 485 978 584 308 526 548 70 979 359 993 226 462 881 398 134 222 ...
output:
7739
result:
ok single line: '7739'
Test #10:
score: 0
Accepted
time: 125ms
memory: 38380kb
input:
1000 500000 869 767 467 831 564 631 555 269 463 490 912 126 978 305 712 940 979 160 339 21 141 511 430 140 80 937 427 286 983 680 880 302 160 143 691 411 360 439 853 797 37 867 804 299 98 651 278 495 590 213 229 768 741 351 286 343 573 935 641 155 747 707 468 653 148 489 936 284 780 737 138 947 978 ...
output:
3777
result:
ok single line: '3777'
Test #11:
score: 0
Accepted
time: 138ms
memory: 38948kb
input:
10000 500000 831 9646 869 7698 1808 110 1692 824 716 1949 126 562 7824 2747 105 2753 2203 63 6720 5207 251 2425 3114 828 1073 5865 688 5748 7968 709 2843 5863 162 239 1523 688 6219 4445 289 5919 7428 216 4462 4775 865 2397 3242 384 459 5263 590 6806 5824 442 4834 3960 398 2802 1664 722 5875 6346 526...
output:
127714
result:
ok single line: '127714'
Test #12:
score: 0
Accepted
time: 65ms
memory: 23596kb
input:
100000 500000 99571 415 842 88646 76834 340 7335 47388 939 29506 84999 845 6493 2762 484 61459 21964 192 48115 21757 648 47229 69919 198 31212 80973 812 41884 81592 730 22948 57810 760 51775 97910 854 75732 989 810 9540 13078 465 76070 7833 486 66356 54068 56 24313 31298 9 11873 17766 532 66456 7046...
output:
-1
result:
ok single line: '-1'
Test #13:
score: 0
Accepted
time: 154ms
memory: 41536kb
input:
100000 500000 12797 57359 174 19769 96152 455 96587 35545 660 45759 66401 996 82264 92296 828 49738 92800 886 6264 91012 156 8370 26551 923 47840 28952 983 86182 40818 337 36097 67441 984 16458 53827 896 72519 77300 503 44464 19421 386 78386 12255 967 72406 29902 239 17363 27044 112 11359 42336 780 ...
output:
12108785
result:
ok single line: '12108785'
Test #14:
score: -100
Wrong Answer
time: 53ms
memory: 3920kb
input:
1000 10000 224 608 374 666 376 667 338 310 763 941 335 581 29 148 960 715 103 365 229 965 998 761 784 956 278 173 523 943 668 997 922 959 17 785 873 618 77 715 587 249 424 358 411 822 724 493 60 375 702 629 182 106 905 43 609 830 323 353 290 814 69 512 391 102 982 797 902 381 935 1 124 858 318 239 9...
output:
62208
result:
wrong answer 1st lines differ - expected: '62175', found: '62208'