hdu 2282 Chocolate【KM匹配+建图】

来源:互联网 发布:mac系统 high sierra 编辑:程序博客网 时间:2024/06/01 14:40

Chocolate

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 665    Accepted Submission(s): 330

Problem Description

Lethe loves eating chocolates very much. In Lethe's birthday, her good friend echo brings N boxes to her, and makes the boxes on the circle. Furthermore, echo tells Lethe that there are many chocolates in the boxes, but the total number of chocolates doesn't exceed N. Also, echo wants Lethe to displace the chocolates in such a way that in each box remains no more than one chocolate. In one move she can shift one chocolate from current box to its neighboring box. (Each box has two neighboring boxes). Can you tell Lethe the minimum number of move to achieve this goal?

Input

There are multi-cases (The total number of cases won't exceed 20). First line is an integer N(1<=N<=500), the total number of boxes. Then N lines follow, each line includes a number, indicates the number of chocolates in the box.

Output

Output the minimum number of move.

Sample Input

10

1

3

3

0

0

2

0

0

0

0

Sample Output

9

Source

HDU 8th Programming Contest Site2

 

题目大意:

有n个盒子,每个盒子中可能有巧克力,也可能没有巧克力,现在将这些盒子围成一个圈,然后每一单位时间可以取任意一个盒子中的一个巧克力挪到相邻的位子上。

问最少操作次数,使得每个盒子只有一个巧克力(当然允许有盒子没有巧克力)(题目保证巧克力总数<=n)。


思路:


1、首先:将巧克力看成左集合,将每个盒子看成右集合。这样就出现了一个二部图。


2、然后每个巧克力对应到每一个盒子将要花费的单位时间都是不同的,那么这个二部图就加上了权值。


3、问题就转化到:有<=n个巧克力,有n个盒子,将这些个巧克力分配到n个盒子中,每个盒子最多只能保存一个巧克力,问最少权值花费,那么其问题就是一个最小权值匹配问题。那么建图,跑KM即可


4、因为我们要求的是最小匹配问题,那么我们可以选择一个极大值(n*10足够了)-a【i】【j】,使得原先的较小值变成了较大值,最小值变成了最大值,那么现在跑一遍最大权值匹配KM算法,得到的最大匹配,其实就是原图中的最小匹配。ans=n*n*10-最大匹配值。


Ac代码:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int a[650][650];int val[650];int lx[650];int ly[650];int vx[650];int vy[650];int match[650];int n,low;int abs(int aaa){    if(aaa<0)return -aaa;    else return aaa;}void getmap(){    memset(a,0,sizeof(a));    int cont=0;    for(int i=0;i<n;i++)    {        for(int j=0;j<val[i];j++)        {            for(int k=0;k<n;k++)            {                a[cont][k]=abs(i-k);                a[cont][k]=min(n-abs(i-k),a[cont][k]);            }            cont++;        }    }}int find(int u){    vx[u]=1;    for(int i=0;i<n;i++)    {        if(vy[i]==1)continue;        int tmp=lx[u]+ly[i]-a[u][i];        if(tmp==0)        {            vy[i]=1;            if(match[i]==-1||find(match[i]))            {                match[i]=u;                return 1;            }        }        else if(tmp<low)low=tmp;    }    return 0;}void KM(){    memset(match,-1,sizeof(match));    memset(lx,0,sizeof(lx));    memset(ly,0,sizeof(ly));    for(int i=0;i<n;i++)    {        for(int j=0;j<n;j++)        {            a[i][j]=n*10-a[i][j];            lx[i]=max(lx[i],a[i][j]);        }    }    for(int i=0;i<n;i++)    {        while(1)        {            low=0x3f3f3f3f;            memset(vx,0,sizeof(vx));            memset(vy,0,sizeof(vy));            if(find(i))break;            for(int j=0;j<n;j++)            {                if(vx[j])lx[j]-=low;                if(vy[j])ly[j]+=low;            }        }    }    int sum=0;    for(int i=0;i<n;i++)    {        sum+=a[match[i]][i];    }    printf("%d\n",n*n*10-sum);}int main(){    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)        {            scanf("%d",&val[i]);        }        getmap();        KM();    }}





0 0