ickchen2

PKU 3270 置换群

/*一道USACO 2007的题...很好的一道利用置换群去排序的题*/
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define M 1000
using namespace std;
struct cow
{
    int weight;
    int id;
};
cow c[M];
int cmp(const cow a,const cow b)
{
    return a.weight<b.weight;
}
int main()
{
    freopen("3270.txt","r",stdin);
    int n;
    scanf("%d",&n);

    for(int i=0;i<n;i++)
    {
        scanf("%d",&c[i].weight);
        c[i].id=i;
    }
    sort(c,c+n,cmp);
    int m=c[0].weight;
    int ans=0;
    for(int i=0;i<n;i++)
    {
        if(c[i].id!=-1)
        {
            int j=c[i].id;
            int w=c[i].weight;
            int k=1;
            while(j!=i)
            {
                ans+=c[j].weight;
                int nxt=c[j].id;
                c[j].id=-1;
                j=nxt;
                k++;
            }
            c[i].id=-1;
            ans+=min((k-1)*w,(k-1)*m+2*(m+w));
        }
    }
    printf("%d\n",ans);
    return 0;
}

posted on 2008-09-08 09:17 神之子 阅读(403) 评论(0)  编辑 收藏 引用 所属分类: ACM题解


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜