QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#292639 | #1295. Fastest Speedrun | snow_trace | WA | 1ms | 3900kb | C++14 | 2.9kb | 2023-12-28 10:04:32 | 2023-12-28 10:04:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 2505;
#define int long long
int c[N][N],mn_cycle[N][N],mnpos[N][N];vector<int>cycle[N];int cc[N][N];
vector<int>p[N],p1[N];
int t[N];
int dis[N];int n;
int col[N],vis[N],ok[N];int nw;vector<int>s;
void dfs1(int now){
col[now] = nw;
for(int i =0;i<p[now].size();i++)if(!col[p[now][i]])dfs1(p[now][i]);
}void dfs2(int now,int c,int id){
if(vis[now] == id){
//cout << 111 << endl;
while(s.back()!=now){
cycle[c].push_back(s.back());s.pop_back();
}cycle[c].push_back(now);s.pop_back();
// cout << now << endl;
// for(int x:cycle[c])cout << x << " ";cout << endl;
return;
}vis[now] = id;s.push_back(now);
for(int i =0;i<p1[now].size();i++){
//cout<< ' '<<p1[now][i] << " " << vis[p1[now][i]] << endl;
if(!vis[p1[now][i]] or vis[p1[now][i]]==id)dfs2(p1[now][i],c,id);
}if(s.size())s.pop_back();
return;
}void dfs3(int now,int fa){
//cout<< ' '<<now<< ' '<<fa<<endl;
for(int i= 0;i<=n;i++)cc[now][i] = min(cc[now][i],cc[fa][i]);
for(int i = 0;i<p1[now].size();i++){
if(ok[p1[now][i]] == 0)dfs3(p1[now][i],now);
}
return;
}
signed main(){
cin >> n;vector<int>pp;
for(int i = 1;i<=n;i++){
int x;cin >> x;p1[x].push_back(i);p[x].push_back(i);p[i].push_back(x);
cin >> t[i];//pp.push_back(x);
for(int j = 0;j<=n;j++)cin >> c[i][j];
}//if(n == 10 and t[1] == 59834){
// for(int x:pp)cout<<x << " ";cout << endl;
// }
for(int i =0;i<=n;i++){
if(!col[i]){
nw++;dfs1(i);
}
}for(int i =0;i<=n;i++){
if(!vis[i]){
dfs2(i,col[i],i+1);s.clear();
}
}for(int i = 1;i<=n;i++)for(int j =0;j<=n;j++)cc[i][j] = c[i][j]-t[i];
int res = 0;
for(int i =1;i<=nw;i++){
for(int j = 0;j<=n;j++){
int mn = 2000000005,pos = 0,mn1= 2000000005;;
for(int x:cycle[i]){
// cout << x << " ";
if(c[x][j]<mn)mn = c[x][j],pos = x;mn1 = min(mn1,c[x][j]-t[x]);
}if(i == 1)pos = mn =mn1= 0;
mn_cycle[i][j] = mn;mnpos[i][j] = pos;if(j == n)res+=mn1;
//cout << endl;
}
}for(int i = 1;i<=nw;i++){
for(int x:cycle[i])ok[x]=1;
}for(int i = 1;i<=nw;i++){
for(int x:cycle[i])dfs3(x,x);
}
for(int i = 1;i<=n;i++)res+=t[i];
//for(int i = 1;i<=nw;i++)res+=(mn_cycle[i][n]-t[mnpos[i][n]]);
//cout << res << endl;
for(int i =0;i<=n;i++)dis[i] = 1e18;dis[0] = 0;
for(int i = 1;i<=n;i++){
for(int j =0;j<i;j++){
int cost = min(mn_cycle[col[i]][j]-mn_cycle[col[i]][n],cc[i][j]);
dis[i] = min(dis[i],dis[j]+cost);
}
}int ans = res+dis[n];
cout << ans << endl;
return 0;
}
/*
10
6 6 4 10 3 2 7 9 8 3
*/
// 经典套路:连边以后是一颗外向基环树,在环里选一个点买下来就能全部跳过激活。
// 然后我们假设最大值一开始就是 n,考虑答案与其差的最小值
// 本质上是我们需要求一个最短路也就是最少多花费多少以后最大的选的点就变成 n
// 激活一个点,两种方式:激活环,激活树上父亲 ,激活树上父亲
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3832kb
input:
1 1 46655 801493 606509
output:
801493
result:
ok single line: '801493'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3676kb
input:
2 0 30959 840157 382961 250706 2 73638 750573 497639 377934
output:
528598
result:
ok single line: '528598'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3672kb
input:
3 0 70473 73268 72749 71642 71006 3 67615 69789 68595 67615 67615 1 84421 87580 86225 85864 84856
output:
222509
result:
ok single line: '222509'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3640kb
input:
5 4 36983 880873 542403 447855 310109 309120 289166 1 2573 758485 525354 524133 517284 430873 171522 4 49956 741491 612741 566137 402069 396666 241786 5 65511 904417 741480 615115 539173 294813 236885 2 48676 922325 870948 453391 375291 328714 268288
output:
959611
result:
ok single line: '959611'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3684kb
input:
10 8 73343 860150 839235 822724 821373 638663 593660 572760 491466 485375 258547 150021 9 9556 847952 819225 785472 662051 595729 566426 541364 454369 356846 236212 136029 0 2380 827000 584529 547699 482964 455015 448063 416973 327715 263254 81443 55243 8 95810 879235 875497 854558 716249 481134 469...
output:
636684
result:
ok single line: '636684'
Test #6:
score: -100
Wrong Answer
time: 1ms
memory: 3900kb
input:
10 6 59834 921756 640795 635654 612984 542500 436518 342887 271826 221615 129388 63231 6 58985 821959 752681 712339 707545 662700 649466 532250 400709 278832 175367 174881 4 83588 926028 915496 892254 878746 388992 358414 332074 329060 183331 131380 125892 10 59032 645556 639858 585525 238505 234457...
output:
1405674
result:
wrong answer 1st lines differ - expected: '1409055', found: '1405674'