zoukankan      html  css  js  c++  java
  • POJ 2195 Going Home(费用流)

    http://poj.org/problem?id=2195

    题意:

    在一个网格地图上,有n个小人和n栋房子。在每个时间单位内,每个小人可以往水平方向或垂直方向上移动一步,走到相邻的方格中。对每个小人,每走一步需要支付1美元,直到他走入到一栋房子里。每栋房子只能容纳一个小人。

    计算出让n个小人移动到n个不同的房子需要支付的最小费用。

    思路:

    源点和每个人相连,容量为1,费用为0。

    汇点和每栋房子相连,容量为1,费用为0。

    每个人和每栋房子相连,容量为1,费用为人和房子之间的距离。

    这样一来,跑一遍费用流即可。

      1 #include<iostream>
      2 #include<algorithm>
      3 #include<cstring>
      4 #include<cstdio>
      5 #include<sstream>
      6 #include<vector>
      7 #include<stack>
      8 #include<queue>
      9 #include<cmath>
     10 #include<map>
     11 #include<set>
     12 using namespace std;
     13 typedef long long ll;
     14 typedef long long ull;
     15 typedef pair<int,int> pll;
     16 const int INF = 0x3f3f3f3f;
     17 const int maxn = 1000 + 5;
     18 
     19 int n, m, k;
     20 
     21 struct Edge
     22 {
     23     int from, to, cap, flow, cost;
     24     Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w) {}
     25 };
     26 
     27 struct MCMF
     28 {
     29     int n, m;
     30     vector<Edge> edges;
     31     vector<int> G[maxn];
     32     int inq[maxn];
     33     int d[maxn];
     34     int p[maxn];
     35     int a[maxn];
     36 
     37     void init(int n)
     38     {
     39         this->n = n;
     40         for (int i = 0; i<n; i++) G[i].clear();
     41         edges.clear();
     42     }
     43 
     44     void AddEdge(int from, int to, int cap, int cost)
     45     {
     46         edges.push_back(Edge(from, to, cap, 0, cost));
     47         edges.push_back(Edge(to, from, 0, 0, -cost));
     48         m = edges.size();
     49         G[from].push_back(m - 2);
     50         G[to].push_back(m - 1);
     51     }
     52 
     53     bool BellmanFord(int s, int t, int &flow, int & cost)
     54     {
     55         for (int i = 0; i<n; i++) d[i] = INF;
     56         memset(inq, 0, sizeof(inq));
     57         d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
     58 
     59         queue<int> Q;
     60         Q.push(s);
     61         while (!Q.empty()){
     62             int u = Q.front(); Q.pop();
     63             inq[u] = 0;
     64             for (int i = 0; i<G[u].size(); i++){
     65                 Edge& e = edges[G[u][i]];
     66                 if (e.cap>e.flow && d[e.to]>d[u] + e.cost){
     67                     d[e.to] = d[u] + e.cost;
     68                     p[e.to] = G[u][i];
     69                     a[e.to] = min(a[u], e.cap - e.flow);
     70                     if (!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
     71                 }
     72             }
     73         }
     74 
     75         if (d[t] == INF) return false;
     76         flow += a[t];
     77         cost += d[t] * a[t];
     78         for (int u = t; u != s; u = edges[p[u]].from)
     79         {
     80             edges[p[u]].flow += a[t];
     81             edges[p[u] ^ 1].flow -= a[t];
     82         }
     83         return true;
     84     }
     85 
     86     int MincostMaxdflow(int s, int t){
     87         int flow = 0, cost = 0;
     88         while (BellmanFord(s, t, flow, cost));
     89         return cost;
     90     }
     91 }t;
     92 
     93 struct node
     94 {
     95     int x, y;
     96 }people[maxn],house[maxn];
     97 
     98 int main()
     99 {
    100     //freopen("in.txt","r",stdin);
    101     while(~scanf("%d%d",&n,&m) && n && m)
    102     {
    103         char c;
    104         int cnt_p=0, cnt_h=0;
    105         for(int i=0;i<n;i++)
    106         {
    107             for(int j=0;j<m;j++)
    108             {
    109                 cin>>c;
    110                 if(c=='H')       {house[++cnt_h].x=i;house[cnt_h].y=j;}
    111                 else if(c=='m')  {people[++cnt_p].x=i;people[cnt_p].y=j;}
    112             }
    113         }
    114 
    115         int n=cnt_h;
    116         int src=0, dst=2*n+1;
    117         t.init(dst+1);
    118 
    119         for(int i=1;i<=cnt_p;i++)  t.AddEdge(src,i,1,0);
    120         for(int i=1;i<=cnt_h;i++)  t.AddEdge(n+i,dst,1,0);
    121 
    122         for(int i=1;i<=cnt_p;i++)
    123         {
    124             for(int j=1;j<=cnt_h;j++)
    125             {
    126                 int dis=abs(people[i].x-house[j].x)+abs(people[i].y-house[j].y);
    127                 t.AddEdge(i,n+j,1,dis);
    128             }
    129         }
    130 
    131         printf("%d
    ",t.MincostMaxdflow(src,dst));
    132     }
    133     return 0;
    134 }
  • 相关阅读:
    gridview列前加复选框需要注意的一点
    为什么日历控件放在panel无法显示出来
    表格翻页
    The SDK platform-tools version ((21)) is too old to check APIs compiled with API 23
    win7怎么安装和启动 jboss
    (转)如何制作nodejs,npm “绿色”安装包
    Can't use Subversion command line client: svn
    (转)Android自定义属性时format选项( <attr format="reference" name="background" /> )
    本地拒绝服务漏洞修复建议
    (转)Android 读取联系人(详细)
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/7214561.html
Copyright © 2011-2022 走看看