码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ1119: [POI2009]SLO

时间:2014-09-08 10:42:56      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   ar   for   

1119: [POI2009]SLO

Time Limit: 30 Sec  Memory Limit: 162 MB
Submit: 379  Solved: 181
[Submit][Status]

Description

对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若干次交换的代价为每次交换的代价之和。请问将(ai)变为(bi)所需的最小代价是多少。

Input

第一行N。 第二行N个数表示wi。 第三行N个数表示ai。 第四行N个数表示bi。 2<=n<=1000000 100<=wi<=6500 1<=ai,bi<=n ai各不相等,bi各不相等 (ai)<>(bi) 样例中依次交换数字(2,5)(3,4)(1,5)

Output

一个数,最小代价。

Sample Input

6
2400 2000 1200 2400 1600 4000
1 4 5 3 6 2
5 3 2 4 6 1

Sample Output

11200

HINT

感谢MT大牛贡献译文.

Source

题解:

同 排序的代价,只不过要把数组元素重新编号。还要记得保存原来的元素值

代码:

bubuko.com,布布扣
 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 }
View Code

 

BZOJ1119: [POI2009]SLO

标签:des   style   blog   http   color   os   io   ar   for   

原文地址:http://www.cnblogs.com/zyfzyf/p/3961099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!