程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ訓練計劃2195_Going Home(網絡流/費用流)

POJ訓練計劃2195_Going Home(網絡流/費用流)

編輯:C++入門知識

POJ訓練計劃2195_Going Home(網絡流/費用流)


解題報告

題目傳送門

思路:

bfs建圖跑一下費用流就行。

#include 
#include 
#include 
#include 
#define inf 0x3f3f3f3f

using namespace std;
struct E {
    int v,cost,cap,next;
} edge[100000];
int head[1000],cnt,dis[1000],pre[1000],vis[1000],s,t,mmap[110][110],h,w,f[1100],cost,flow;
struct N {
    int x,y,step;
};
void add(int u,int v,int cost,int cap) {
    edge[cnt].v=v;
    edge[cnt].cost=cost;
    edge[cnt].cap=cap;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].cost=-cost;
    edge[cnt].cap=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int dx[]= {-1,0,1,0};
int dy[]= {0,1,0,-1};
void bfs(int x,int y) {
    queueq;
    int v[110][110];
    memset(v,0,sizeof(v));
    N next,now;
    now.x=x;
    now.y=y;
    now.step=0;
    v[x][y]=1;
    q.push(now);
    while(!q.empty()) {
        now=q.front();
        q.pop();
        if(mmap[now.x][now.y]>=101) {
            add(mmap[x][y],mmap[now.x][now.y],now.step,1);
        }
        for(int i=0; i<4; i++) {
            next.x=now.x+dx[i];
            next.y=now.y+dy[i];
            if(next.x>=0&&next.x=0&&next.yQ;
    Q.push(s);
    while(!Q.empty()) {
        int u=Q.front();
        Q.pop();
        vis[u]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next) {
            int v=edge[i].v;
            if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost) {
                pre[v]=i;
                dis[v]=dis[u]+edge[i].cost;
                f[v]=min(f[u],edge[i].cap);
                if(!vis[v]) {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    if(dis[t]==inf)return 0;
    flow+=f[t];
    cost+=f[t]*dis[t];
    for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v]) {
        edge[i].cap-=f[t];
        edge[i^1].cap+=f[t];
    }
    return 1;
}
void mcmf() {
    cost=flow=0;
    while(_spfa());
    printf("%d\n",cost);
}
int main() {
    int i,j,n,m;
    char str[1000];
    while(~scanf("%d%d",&h,&w)) {
        n=101,m=1;
        s=0,t=201;
        if(!h&&!w)break;
        memset(edge,0,sizeof(edge));
        memset(head,-1,sizeof(head));
        cnt=0;
        for(i=0; i=1&&mmap[i][j]<=100) {
                    add(0,mmap[i][j],0,1);
                    bfs(i,j);
                }
                if(mmap[i][j]>=101)
                    add(mmap[i][j],t,0,1);
            }
        }
        mcmf();
    }
    return 0;
}


Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18017 Accepted: 9185

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 fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.

Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point.
\

You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

Sample Input

2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

Sample Output

2
10
28

Source

Pacific Northwest 2004

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved