这题预处理稍微动动脑,其实还是个裸的置换群=-=,没什么压力。
1 const maxn=1000001;
2 var n,i,j,minx,tem,now,tmin,len:longint;
3 cursum,sum:int64;
4 pos,num,tnum,ys:array[0..maxn] of longint;
5 p:array[0..maxn] of boolean;
6 function min(a,b:longint):longint;
7 begin
8 if a>b then exit(b)
9 else exit(a);
10 end;
11 begin
12 readln(n);
13 minx:=maxn;
14 for i:=1 to n do
15 begin
16 read(tnum[i]);
17 minx:=min(minx,tnum[i]);
18 end;
19 readln;
20 for i:=1 to n do
21 begin
22 read(tem);
23 num[i]:=tnum[tem];
24 ys[tem]:=i;
25 end;
26 readln;
27 for i:=1 to n do
28 begin
29 read(tem);
30 pos[ys[tem]]:=i;
31 end;
32 fillchar(p,sizeof(p),true);
33 for i:=1 to n do
34 if p[i] then
35 begin
36 cursum:=0;
37 now:=i;
38 len:=0;
39 tmin:=num[i];
40 while p[now] do
41 begin
42 inc(len);
43 tmin:=min(tmin,num[now]);
44 inc(cursum,num[now]);
45 p[now]:=false;
46 now:=pos[now];
47 end;
48 if len>1 then inc(sum,cursum);
49 if len>2 then inc(sum,min(tmin*(len-2),minx*(len+1)+tmin));
50 end;
51 writeln(sum);
52 end.
(转载请注明出处:http://www.cnblogs.com/Kalenda/)