首页 技术 正文
技术 2022年11月8日
0 收藏 691 点赞 1,757 浏览 1093 个字

1119: [POI2009]SLO

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大牛贡献译文.

【分析】

  跟BZOJ 1119一模一样,记得用LL。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 1000010
#define INF 0xfffffff
#define LL long long LL w[Maxn]; struct node
{
LL a,b;
}t[Maxn]; bool cmp(node x,node y) {return x.a<y.a;}
LL mymin(LL x,LL y) {return x<y?x:y;} bool vis[Maxn]; int main()
{
LL n,mnn=INF;
scanf("%lld",&n);
for(LL i=;i<=n;i++) {scanf("%lld",&w[i]);mnn=mymin(mnn,w[i]);}
for(LL i=;i<=n;i++) scanf("%lld",&t[i].a);
for(LL i=;i<=n;i++) scanf("%lld",&t[i].b);
sort(t+,t++n,cmp);
memset(vis,,sizeof(vis));
LL ans=;
for(LL i=;i<=n;i++) if(vis[i]==)
{
LL x=i,mn=INF,h=;
LL cnt=;
while(vis[x]==)
{
mn=mymin(mn,w[x]);
vis[x]=;
cnt++;
h+=w[x];
x=t[x].b;
}
ans+=mymin(h+(cnt-)*(LL)mn,h+mn+(cnt+)*(LL)mnn);
}
printf("%lld\n",ans);
return ;
}

2017-01-12 20:13:51

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922