hdu 1533 流流流

把每个点移动到对应位置的总费用最小。建图后费用流。继续水水练练手~

#include<iostream>
#include<queue>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int MAXN=20005, MAXE=4000000;
int e[MAXE][4];int head[MAXN];int nume;
int n,m;int N,M;
void inline adde(int i,int j,int c,int w)
{
    e[nume][0]=j;e[nume][1]=head[i];head[i]=nume;
    e[nume][2]=c;e[nume++][3]=w;
    e[nume][0]=i;e[nume][1]=head[j];head[j]=nume;
    e[nume][2]=0;e[nume++][3]=-w;
}
struct point
{
    int x,y;
    int id;
};
int inline getdis(point a,point b)
{
   return abs(a.x-b.x)+abs(a.y-b.y);
}
vector<point>man,house;

void build()
{
     N=man.size();M=house.size();
    for(int i=0;i<N;i++)
    {
        adde(0,i+1,1,0);
        for(int j=0;j<M;j++)
              adde(i+1,j+1+N,1,getdis(man[i],house[j]));
    }
    for(int i=0;i<M;i++)
      adde(i+N+1,N+M+1,1,0);
}
int inq[MAXN];int prv[MAXN];int pre[MAXN];
int d[MAXN];
bool spfa(int & sum)
{
   for(int i=0;i<=N+M+1;i++)
   {
       d[i]=inf;
       pre[i]=prv[i]=inq[i]=0;
   }
   queue<int>q;
   q.push(0);
   inq[0]=1;
   d[0]=0;
   while(!q.empty())
   {
       int cur=q.front();
       q.pop();
       inq[cur]=0;
       for(int j=head[cur];j!=-1;j=e[j][1])
       {
           int to=e[j][0];
           if(d[to]>d[cur]+e[j][3]&&e[j][2]>0)
           {
               d[to]=d[cur]+e[j][3];
               pre[to]=j;
               prv[to]=cur;
               if(!inq[to])
               {
                   q.push(to);
                   inq[to]=1;
               }
           }
       }
   }
   int minf=inf;
   if(d[N+M+1]==inf)return 0;
   int cur=N+M+1;
   while(cur!=0)
   {
       minf=min(minf,e[pre[cur]][2]);
       cur=prv[cur];
   }
   cur=N+M+1;
   while(cur!=0)
   {
       e[pre[cur]][2]-=minf;
       e[pre[cur]^1][2]+=minf;
       cur=prv[cur];
   }
   sum+=d[N+M+1]*minf;
   return 1;
}
int mincost()
{
    int sum=0;
    while(spfa(sum));
    return sum;
}
void init()
{
    for(int i=0;i<MAXN-1;i++)
    {
        head[i]=-1;
    }
    nume=0;
    man.clear();
    house.clear();
}
int main()
{
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        init();
        char t; point p;
        for(int i=0;i<n;i++)
         for(int j=0;j<m;j++)
         {
             cin>>t;
             if(t=='m')
             {
                 p.x=i;p.y=j;
                 p.id=1;
                 man.push_back(p);
             }
             else if(t=='H')
             {
                 p.x=i;p.y=j;
                 p.id=2;
                 house.push_back(p);
             }
         }
        build();
        int ans=mincost();
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-11-06 03:55:02

hdu 1533 流流流的相关文章

Going Home HDU - 1533 (费用流)

Going Home HDU - 1533 1 //费用流初探 2 #include <iostream> 3 #include <queue> 4 #include <cstring> 5 #include <cstdio> 6 #include <algorithm> 7 using namespace std; 8 const int inf = 0x3f3f3f3f; 9 const int maxn = 110; 10 char gra

POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 MCMF算法,我写的是MCMF算法,一开始想的是连10000个点,但是不会连那些大众点之间的边,只会连超级点和普通点之间的边.后来觉得只要连房子点和 人点就可以了.连从人到房子的边,容量是1,花费是他们之间的曼哈顿距离,然后超级源点和超级汇点像上面那样连接,注意连点的时候把他们每个点都具体化一下,就

【HDU 1533】 Going Home (KM)

Going Home Problem Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel f

HDU 1533 Going Home(KM完美匹配)

HDU 1533 Going Home 题目链接 题意:就是一个H要对应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,由于是要最小,所以边权建负数来处理即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MAXNODE = 105; typedef int Type; const T

hdu 1533 Going Home (KM算法)

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2715    Accepted Submission(s): 1366 Problem Description On a grid map there are n little men and n houses. In each unit time, every

POJ 2195 &amp; HDU 1533 Going Home(最小费用最大流)

题目链接: POJ:http://poj.org/problem?id=2195 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1533 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically,

hdu 1533 Going Home 最小费用最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need

HDU 1533 最小费用最大流(模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void init(int n) 这个函数一定要写 一开始忘记写这个WA了好几发 还有这个题很容易T掉,赋值建图要简化,一开始构建成网络流那种图一直T #include <stdio.h> #include <string.h> #include <iostream> #includ

【网络流#2】hdu 1533 最小费用最大流模板题

嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(House)作为汇点,各个源点与汇点分别连一条边,这条边的流量是1(因为每个源点只能走一条边到汇点),费用是 从源点走到汇点的步数,因为有多个源点与汇点,要建一个超级源点与超级汇点,超级源点与各个源点连一条流量为1,费用为0(要避免产生多余的费用)的边 按照这个图跑一发费用流即可 把代码挂上去,用的是前向星写的 1 #include<cstdio> 2 #include<cstr