标签:des style blog http color os io ar for
感谢MT大牛贡献译文.
题解:
同 排序的代价,只不过要把数组元素重新编号。还要记得保存原来的元素值
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 1000000+1000 14 #define maxm 500+100 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 using namespace std; 22 inline int read() 23 { 24 int x=0,f=1;char ch=getchar(); 25 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 26 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 27 return x*f; 28 } 29 ll n,mi=inf,cs=0,a[maxn],b[maxn],c[maxn],d[maxn],w[maxn]; 30 bool check[maxn]; 31 int main() 32 { 33 freopen("input.txt","r",stdin); 34 freopen("output.txt","w",stdout); 35 n=read(); 36 for1(i,n)w[i]=read(),mi=min(w[i],mi); 37 for1(i,n)d[i]=a[i]=read(); 38 for1(i,n)b[i]=read(); 39 for1(i,n)c[b[i]]=i; 40 for1(i,n)a[i]=c[a[i]]; 41 ll ans=0; 42 for1(i,n) 43 if(!check[i]) 44 { 45 ll j=i,len=0,tmp=inf,sum=0; 46 while(!check[j]) 47 { 48 check[j]=1; 49 tmp=min(tmp,w[d[j]]); 50 sum+=w[d[j]]; 51 len++; 52 j=a[j]; 53 } 54 ans+=sum+min((len-2)*tmp,mi*(len+1)+tmp); 55 } 56 cout<<ans<<endl; 57 return 0; 58 }
标签:des style blog http color os io ar for
原文地址:http://www.cnblogs.com/zyfzyf/p/3961099.html