ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#200099 | #2260. speedrun | snow_trace | 100 | 1321ms | 17272kb | C++11 | 2.8kb | 2023-12-28 10:20:08 | 2023-12-28 13:55:29 |
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1005;
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){
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(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
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];
}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] = mn1;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;
}
// 经典套路:连边以后是一颗外向基环树,在环里选一个点买下来就能全部跳过激活。
// 然后我们假设最大值一开始就是 n,考虑答案与其差的最小值
// 本质上是我们需要求一个最短路也就是最少多花费多少以后最大的选的点就变成 n
// 激活一个点,两种方式:激活环,激活树上父亲 以后最大的选的点就变成 n
详细
小提示:点击横条可展开更详细的信息
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 0ms
memory: 1540kb
input:
20 5 13929885 905968038 902465839 877284493 873226739 857380082 807817887 802863226 745191489 731682...
output:
1770026980
result:
ok "1770026980"
Test #2:
score: 0
Accepted
time: 1ms
memory: 1528kb
input:
20 8 41650813 982733904 965010316 947828639 931996408 890812482 882195345 874127465 843426203 840191...
output:
572871488
result:
ok "572871488"
Test #3:
score: 0
Accepted
time: 0ms
memory: 1540kb
input:
20 7 5609736 992686743 762471617 729573133 694448052 565847128 561937708 494490080 480492380 4529956...
output:
914997037
result:
ok "914997037"
Subtask #2:
score: 30
Accepted
Test #4:
score: 30
Accepted
time: 95ms
memory: 17176kb
input:
1000 1000 1514669 999821548 999208019 999058039 997885811 997519963 996887939 992771285 991618116 99...
output:
1867648404
result:
ok "1867648404"
Test #5:
score: 0
Accepted
time: 105ms
memory: 17172kb
input:
1000 1000 1346542 999240460 998282474 996725148 996189387 995687123 994922739 994615389 994427312 99...
output:
1826553056
result:
ok "1826553056"
Test #6:
score: 0
Accepted
time: 97ms
memory: 17176kb
input:
1000 1000 175777 997492053 997058970 996726517 996602523 995311350 995251035 995182486 993676274 993...
output:
1830991954
result:
ok "1830991954"
Subtask #3:
score: 30
Accepted
Test #7:
score: 30
Accepted
time: 8ms
memory: 3672kb
input:
200 177 9381925 995829624 983336723 979813951 977460662 975836272 967058907 961441255 960141236 9527...
output:
1816020211
result:
ok "1816020211"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3688kb
input:
200 30 4286552 996621468 995826876 995519905 985655327 981252734 967441410 966383152 965652807 96527...
output:
1809301989
result:
ok "1809301989"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
200 173 3808221 999851000 986003435 985595659 979562075 963889418 943173147 935907573 934100679 9287...
output:
1827678229
result:
ok "1827678229"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
200 139 549058 998491959 994834766 984031612 982116934 978606391 977630480 976313462 976032220 97027...
output:
861008894
result:
ok "861008894"
Subtask #4:
score: 30
Accepted
Test #11:
score: 30
Accepted
time: 108ms
memory: 17224kb
input:
1000 621 114499 997721313 996514597 996308290 996253926 995993402 995523141 994699743 993937693 9925...
output:
898630932
result:
ok "898630932"
Test #12:
score: 0
Accepted
time: 106ms
memory: 17272kb
input:
1000 262 399801 998446888 997833841 996957270 996508115 996280316 995786496 995706706 994730053 9933...
output:
1068160867
result:
ok "1068160867"
Test #13:
score: 0
Accepted
time: 104ms
memory: 17240kb
input:
1000 1000 2737489 997513594 996345497 995871410 993469950 990161990 989691913 989437751 988018235 98...
output:
1003512081
result:
ok "1003512081"
Test #14:
score: 0
Accepted
time: 107ms
memory: 17264kb
input:
1000 326 201735 998550730 997518027 996614837 996266576 994608532 994005807 993493977 992120785 9901...
output:
843170570
result:
ok "843170570"
Test #15:
score: 0
Accepted
time: 103ms
memory: 17236kb
input:
1000 796 535702 995325443 994668895 994479064 994460977 994102432 993195360 990429156 989318285 9888...
output:
838963063
result:
ok "838963063"
Test #16:
score: 0
Accepted
time: 97ms
memory: 17208kb
input:
1000 846 531482 999379549 997730884 997022561 996149895 995981413 992507358 990807173 990643156 9902...
output:
1829819841
result:
ok "1829819841"
Test #17:
score: 0
Accepted
time: 89ms
memory: 17268kb
input:
1000 831 1169729 999868611 999536097 999117102 998006582 995979435 995748971 994460584 993676740 992...
output:
1820715424
result:
ok "1820715424"
Test #18:
score: 0
Accepted
time: 96ms
memory: 17224kb
input:
1000 139 247568 998351797 996223682 995706470 994165883 993650660 993378098 991868206 990091599 9898...
output:
1817999412
result:
ok "1817999412"
Test #19:
score: 0
Accepted
time: 106ms
memory: 17232kb
input:
1000 774 83343 999994666 999533673 999009643 997973120 997593294 996028006 992186045 991754669 98946...
output:
1852424307
result:
ok "1852424307"
Test #20:
score: 0
Accepted
time: 99ms
memory: 17208kb
input:
1000 309 1033613 999958159 999112545 998716374 997750098 997749385 997697598 994730198 994296209 993...
output:
1818623142
result:
ok "1818623142"