1119: [Poi2009]slo Description
For a 1-n arrangement (AI), each time you can exchange two number of Ax with AY (x<>y), the cost of W (ax) +w (ay) is the sum of the cost of each interchange. What is the minimum cost of turning (AI) into (BI)?
Input
First row N. The second row n number represents WI. The third row n number represents AI. The four-row n number represents bi. 2<=n<=1000000 100<=wi<=6500 1<=ai,bi<=n ai are unequal, bi-unequal (AI) <> (BI) samples in turn exchange numbers (2,5) (3,4) (1,5)
Output
A number, the minimum cost.
Sample Input 6
2400 2000 1200 2400 1600 4000
1 4 5 3 6 2
5 3 2 4 6 1Sample Output 11200HINT
Thank Mt Daniel for contributing the translation.
Analysis
As with Bzoj 11,191, remember to use LL.
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 #defineMAXN 10000108 #defineINF 0XFFFFFFF9 #defineLL Long LongTen One LL W[MAXN]; A - structnode - { the LL A, b; - }T[MAXN]; - - BOOLCMP (node X,node y) {returnx.a<y.a;} +ll Mymin (ll X,ll y) {returnX<y?x:y;} - + BOOLVIS[MAXN]; A at intMain () - { -LL n,mnn=INF; -scanf"%lld",&n); - for(LL i=1; i<=n;i++) {scanf ("%lld", &w[i]); mnn=mymin (Mnn,w[i]);} - for(LL i=1; i<=n;i++) scanf ("%lld",&t[i].a); in for(LL i=1; i<=n;i++) scanf ("%lld",&t[i].b); -Sort (t+1, t+1+n,cmp); tomemset (Vis,0,sizeof(Vis)); +LL ans=0; - for(LL i=1; i<=n;i++)if(vis[i]==0) the { *LL x=i,mn=inf,h=0; $LL cnt=0;Panax Notoginseng while(vis[x]==0) - { themn=mymin (mn,w[x]); +vis[x]=1; Acnt++; theh+=W[x]; +x=t[x].b; - } $Ans+=mymin (H + (cnt-2) * (LL) mn,h+mn+ (cnt+1)*(LL) mnn); $ } -printf"%lld\n", ans); - return 0; the}
View Code
2017-01-12 20:13:51
"Bzoj 1119" 1119: [Poi2009]slo (Replacement)