HDU1533 Going Home

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1533 Going Home相关的知识,希望对你有一定的参考价值。

mininum:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<memory.h>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=310;
const int inf=0x7ffffff;
int map[maxn][maxn];
int vis1[maxn],vis2[maxn];
int ex1[maxn],ex2[maxn];
int lack[maxn];
int link[maxn];
int ans,n;
int x1[maxn],x2[maxn],y[maxn],y2[maxn];
int Abs(int v){
    if(v<0) v=-v;
    return v;
}
bool _bfs(int v)
{
    vis1[v]=true;
    for(int i=1;i<=n;i++){
        if(!vis2[i]){
            int tmp=ex1[v]+ex2[i]-map[v][i];
            if(tmp==0){
              vis2[i]=true;
              if(!link[i]||_bfs(link[i])){
                 link[i]=v;
                 return true;
              }
            }
            else if(tmp<lack[i]) lack[i]=tmp;
        }
    }
    return false;
}
void _KM()
{
    int t,i,j;
    memset(link,0,sizeof(link));
    memset(ex2,0,sizeof(ex2));
    for(i=1;i<=n;i++){
        ex1[i]=map[i][1];
        for(j=2;j<=n;j++)
         if(ex1[i]<map[i][j]) ex1[i]=map[i][j];
    }
    for(t=1;t<=n;t++){
        for(i=1;i<=n;i++) lack[i]=inf;//思考:为什么在这里? 
        while(true){ 
            memset(vis1,0,sizeof(vis1));
            memset(vis2,0,sizeof(vis2));
            if(_bfs(t))  break;
            int gap=inf;
            for(i=1;i<=n;i++)
             if(!vis2[i]&&lack[i]<gap) gap=lack[i];
            for(i=1;i<=n;i++) if(vis1[i]) ex1[i]-=gap;
            for(i=1;i<=n;i++) if(vis2[i]) ex2[i]+=gap;
            for(i=1;i<=n;i++) if(!vis2[i])lack[i]-=gap;
        }
    }
    ans=0;
    for(i=1;i<=n;i++)
       ans-=map[link[i]][i];
    ans=ans;
    printf("%d\n",ans);
}
int main()
{
    int i,j,m,cnt1,cnt2;
    char c;
    while(~scanf("%d%d",&n,&m)){
        if(n==0&&m==0) return 0;
        cnt1=cnt2=0;
        for(i=1;i<=n;i++)
         for(j=1;j<=m;j++) {
                cin>>c;
                if(c==H) {
                    x1[++cnt1]=i;y[cnt1]=j;
                }
                if(c==m){
                    x2[++cnt2]=i;y2[cnt2]=j;
                }
         }
         n=cnt1;
         for(i=1;i<=n;i++)
          for(j=1;j<=n;j++){
                map[i][j]=-(Abs(x1[i]-x2[j])+Abs(y[i]-y2[j]));
          }
        _KM();
    }
    return 0;
} 

 

以上是关于HDU1533 Going Home的主要内容,如果未能解决你的问题,请参考以下文章

Hdu 1533 Going Home

Going Home HDU - 1533 (费用流)

hdu 1533 Going Home (最小费用最大流)

HDU1533 Going Home

Going Home HDU - 1533(最大费用最小流)

hdu 1533 Going Home (Dinic, 1)