标签:line lan max clu http ons for end main
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_6_D&lang=jp
最小成本排序:
#include <bits/stdc++.h> using namespace std; static const int maxn = 1000; static const int vmax = 10000; int n,A[maxn],s; int B[maxn],T[vmax+1]; int solve() { int ans=0; bool V[maxn]; for(int i=0;i<n;i++){ B[i]=A[i]; V[i]=false; } sort(B,B+n); for(int i=0;i<n;i++) { T[B[i]]=i; } for(int i=0;i<n;i++) { if(V[i]) continue; int cur=i; int S=0; int m=vmax; int an=0; while(1) { V[cur]=true; an++; int v=A[cur]; m=min(m,v); S+=v; cur=T[v]; if(V[cur]) break; } ans+=min(S+(an-2)*m,m+S+(an+1)*m); } return ans; } int main() { cin>>n; s=vmax; for(int i=0;i<n;i++) { cin>>A[i]; s=min(s,A[i]); } int ans=solve(); cout<<ans<<endl; return 0; }
标签:line lan max clu http ons for end main
原文地址:http://www.cnblogs.com/masterchd/p/7208481.html