程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDU 1533 Going Home(最小費用流)

HDU 1533 Going Home(最小費用流)

編輯:關於C++

Going Home

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3278 Accepted Submission(s): 1661



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 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

題意:給一個地圖,標了人m和房H的位置,人數和房子數量相等,現在所有人要回各自的家,一個房只能容一個人。問所有人走的步數總和最少是多少?每一步只能走相鄰的格子。

解題:最小費用流。

分3類點:1:源點S,匯點T。 2:人M。3:房H。

建圖:(u , v , cap, cost):u-->v邊容為cap,花費為cost

1):(S , M , 1 , 0)

2):(M , H , 1 , mindis):mindis表示人到房的最短距離

3:(H , T , 1 , 0)

 

#include
#include
#include
using namespace std;
const int MAXN = 10010;
const int MAXM = 100100;
const int INF = 1<<30;
struct EDG{
    int to,next,cap,flow;
    int cost;  //單價
}edg[MAXM];
int head[MAXN],eid;
int pre[MAXN], cost[MAXN]  ; //點0~(n-1)

void init(){
    eid=0;
    memset(head,-1,sizeof(head));
}
void addEdg(int u,int v,int cap,int cst){
    edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = cst;
    edg[eid].cap=cap; edg[eid].flow=0; head[u]=eid++;

    edg[eid].to=u; edg[eid].next=head[v]; edg[eid].cost = -cst;
    edg[eid].cap=0; edg[eid].flow=0; head[v]=eid++;
}

bool inq[MAXN];
bool spfa(int sNode,int eNode,int n){
    queueq;
    for(int i=0; i0 && cost[v]>cost[u]+edg[i].cost){ //在滿足可增流的情況下,最小花費
                cost[v] = cost[u]+edg[i].cost;
                pre[v]=i;   //記錄路徑上的邊
                if(!inq[v])
                    q.push(v),inq[v]=1;
            }
        }
    }
    return cost[eNode]!=INF;    //判斷有沒有增廣路
}
//反回的是最大流,最小花費為minCost
int minCost_maxFlow(int sNode,int eNode ,int& minCost,int n){
    int ans=0;
    while(spfa(sNode,eNode,n)){
        int mint=INF;
        for(int i=pre[eNode]; i!=-1; i=pre[edg[i^1].to]){
            if(mint>edg[i].cap-edg[i].flow)
                mint=edg[i].cap-edg[i].flow;
        }
        ans+=mint;
        for(int i=pre[eNode]; i!=-1; i=pre[edg[i^1].to]){
            edg[i].flow+=mint; edg[i^1].flow-=mint;
            minCost+=mint*edg[i].cost;
        }
    }
    return ans;
}

int abs(int a){ return a>0?a:-a; }
int buildGraph(char mapt[105][105],int n,int m){

    int id[105][105] , k=1 ;

    for(int i=0; i0&&(n||m)){
            for(int i=0; i

 

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