Going Home HDU - 1533

标签: 费用流 HDU
2人阅读 评论(0) 收藏 举报
分类:

点击打开链接

费用流模板

一开始建的图太烂了 每个点和四周的点建边 其实只对m与H建边即可 边的费用即为两点曼哈顿距离

#include <bits/stdc++.h>
using namespace std;
#define N 0x3f3f3f3f

struct node1
{
    int v;
    int w;
    int c;
    int next;
};

struct node2
{
    int x;
    int y;
};

queue <int> que;
node1 edge[40010];
node2 pre1[10010],pre2[10010];
int first[10010],dis[10010],book[10010],pre[10010];
char mp[110][110];
int n,m,num,s,e,cnt1,cnt2;

void addedge(int u,int v,int w,int c)
{
    edge[num].v=v;
    edge[num].w=w;
    edge[num].c=c;
    edge[num].next=first[u];
    first[u]=num++;
    return;
}

void build()
{
    int next[4][2]={0,-1,-1,0,0,1,1,0};
    int i,j,k,tx,ty,u,v;
    s=n*m+1,e=n*m+2,cnt1=0,cnt2=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(mp[i][j]=='m')
            {
                cnt1++;
                pre1[cnt1].x=i;
                pre1[cnt1].y=j;
            }
            else if(mp[i][j]=='H')
            {
                cnt2++;
                pre2[cnt2].x=i;
                pre2[cnt2].y=j;
            }
        }
    }
    memset(first,-1,sizeof(first));
    num=0;
    for(i=1;i<=cnt1;i++)
    {
        u=s,v=pre1[i].x*m+pre1[i].y+1;
        addedge(u,v,1,0);
        addedge(v,u,0,0);
    }
    for(i=1;i<=cnt2;i++)
    {
        u=pre2[i].x*m+pre2[i].y+1,v=e;
        addedge(u,v,1,0);
        addedge(v,u,0,0);
    }
    for(i=1;i<=cnt1;i++)
    {
        for(j=1;j<=cnt2;j++)
        {
            u=pre1[i].x*m+pre1[i].y+1,v=pre2[j].x*m+pre2[j].y+1;
            addedge(u,v,1,abs(pre1[i].x-pre2[j].x)+abs(pre1[i].y-pre2[j].y));
            addedge(v,u,0,-abs(pre1[i].x-pre2[j].x)-abs(pre1[i].y-pre2[j].y));
        }
    }
    return;
}

int spfa()
{
    int i,u,v,w,c;
    while(!que.empty()) que.pop();
    memset(dis,0x3f,sizeof(dis));
    memset(book,0,sizeof(book));
    memset(pre,-1,sizeof(pre));
    que.push(s);
    dis[s]=0,book[s]=1;
    while(!que.empty())
    {
        u=que.front();
        que.pop();
        book[u]=0;
        for(i=first[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].v,w=edge[i].w,c=edge[i].c;
            if(w>0&&dis[v]>dis[u]+c)
            {
                dis[v]=dis[u]+c,pre[v]=i;
                if(!book[v])
                {
                    que.push(v);
                    book[v]=1;
                }
            }
        }
    }
    return dis[e];
}

int mcmf()
{
    int res,i,u,v,w,c,cost,minn;
    cost=0;
    while(1)
    {
        res=spfa();
        if(res==N) break;
        cost+=res;
        minn=N;
        for(i=pre[e];i!=-1;i=pre[v])
        {
            v=edge[i^1].v,w=edge[i].w;
            minn=min(minn,w);
        }
        for(i=pre[e];i!=-1;i=pre[v])
        {
            v=edge[i^1].v,c=edge[i].c;
            edge[i].w-=minn;
            edge[i^1].w+=minn;
        }
    }
    return cost;
}

int main()
{
    int i;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        for(i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
        }
        build();
        printf("%d\n",mcmf());
    }
    return 0;
}

查看评论

HDU 1533 Going Home(最小费用流)

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To...
  • u010372095
  • u010372095
  • 2015-06-23 22:33:02
  • 1075

【最小费用最大流】【HDU1533】【Going Home】

题意  给你一个类似这样的图 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... ...
  • zy691357966
  • zy691357966
  • 2015-04-05 19:23:58
  • 2037

hdu acm 1533 Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submissi...
  • hyf20144055065
  • hyf20144055065
  • 2016-02-09 14:41:22
  • 691

HDU-1533 Going Home

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 题目大意: 给你一个N行M列的矩阵,其中“.”代表空地,“H”代表房子,“m”代表人,其中有...
  • niushuai666
  • niushuai666
  • 2012-01-03 12:44:28
  • 2831

HDU1533Going Home(KM匹配之最小值匹配)

题意:求最小花费。KM通常是来求最大完美匹配,这里只需要把权重变为负数。最后再变回来即可 #include #include #include #include #include #include #...
  • u013167299
  • u013167299
  • 2015-08-30 20:57:20
  • 729

hdu 1533 Going Home【KM匹配】

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot...
  • mengxiang000000
  • mengxiang000000
  • 2016-08-31 13:54:52
  • 208

hdu 1533 Going Home

This is my first min_cost network max_flow problems; From start_point to every 'm' point ,capacity ...
  • NMfloat
  • NMfloat
  • 2015-07-06 14:45:58
  • 219

HDU 1533 Going Home

HDU 1533 Going Home 题目:HDU 1533 题意: m代表人,H代表家,将全部人安排最短的路线回家。 思路:   将“人”与每个“家”连一条线,并且以两者的横纵...
  • u013075699
  • u013075699
  • 2015-05-18 11:09:41
  • 214

Hdu 1533 Going Home

Going HomeTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...
  • zhangliu463884153
  • zhangliu463884153
  • 2018-04-21 14:32:00
  • 0

hdu 1533 Going Home 最小费用流

建图很简单 bfs预处理地图,距离就为费用 源点到所有m建边,流量1费用0 m到所有H建边,流量1费用为距离 H到所有汇点建边,流量1费用0 #include #include #inclu...
  • t1019256391
  • t1019256391
  • 2014-06-26 10:35:37
  • 700
    个人资料
    持之以恒
    等级:
    访问量: 1万+
    积分: 1877
    排名: 2万+
    文章分类
    友情链接
    http://www.cnblogs.com/yzm10