Poj 3270 cow sorting (replacement group)

Source: Internet
Author: User
/* For each group, we have two types of hair exchange: 1. in the group for, take the smallest number t in the group and the other each number exchange, a total of K-1 times, the cost is: Sum + (K-2) * t.2. will be the smallest number of the series Minn, pulled into the group, exchange the smallest number of T with the group, then exchange the K-1 with the smallest number of others, and then replace the Minn with the T, which costs: sum + T + (k + 1) * Minn so minimum cost we take the smallest of the two, that is, sum + min {(K-2) * t, t + (k + 1) * Minn }. */# include <stdio. h> # include <algorithm> # include <string. h ># include <iostream> using namespace STD; struct node {int ID; int num ;}; struct node A [10010]; bool CMP (node A1, node A2) {return a1.num <a2.num ;} Int main () {int sum1, I, Minn, N; int B [10010], C [10010]; while (~ Scanf ("% d", & N) {sum1 = 0; Minn = 100010; for (I = 1; I <= N; I ++) {scanf ("% d", & B [I]); A [I]. num = B [I]; A [I]. id = I; sum1 + = B [I]; If (B [I] <Minn) Minn = B [I]; C [I] = I ;} sort (a + 1, A + n + 1, CMP); for (I = 1; I <= N; I ++) {int T; if (B [I]! = 0) {int COUNT = 1; t = B [I]; int d = A [C [I]. ID; If (B [d] <t) t = B [d]; while (D! = I) {count ++; D = A [D]. ID; If (B [d] <t) t = B [d];} int v = (count-2) * t; int W = (count + 1) * Minn + T; sum1 + = min (V, W); B [I] = 0 ;}} printf ("% d \ n", sum1 );} return 0 ;}

Poj 3270 cow sorting (replacement group)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.