• [POJ 3422] Kaka's Matrix Travels


    [题目链接]

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

    [算法]

             费用流

    [代码]

            

    #include <algorithm>  
    #include <bitset>  
    #include <cctype>  
    #include <cerrno>  
    #include <clocale>  
    #include <cmath>  
    #include <complex>  
    #include <cstdio>  
    #include <cstdlib>  
    #include <cstring>  
    #include <ctime>  
    #include <deque>  
    #include <exception>  
    #include <fstream>  
    #include <functional>  
    #include <limits>  
    #include <list>  
    #include <map>  
    #include <iomanip>  
    #include <ios>  
    #include <iosfwd>  
    #include <iostream>  
    #include <istream>  
    #include <ostream>  
    #include <queue>  
    #include <set>  
    #include <sstream>  
    #include <stdexcept>  
    #include <streambuf>  
    #include <string>  
    #include <utility>  
    #include <vector>  
    #include <cwchar>  
    #include <cwctype>  
    #include <stack>  
    #include <limits.h>
    using namespace std;
    #define MAXN 310
    const int inf = 2e9;
    
    struct edge
    {
            int to,w,cost,nxt;
    } e[MAXN * MAXN * 8];
    
    int i,j,n,k,tot,val,ans,S,T;
    int head[MAXN * MAXN * 2],dist[MAXN * MAXN * 2],incf[MAXN * MAXN * 2],pre[MAXN * MAXN * 2];
    
    inline int id(int x,int y,int k)
    {
            return (x - 1) * n + y + n * n * k;
    }
    inline void addedge(int u,int v,int w,int cost)
    {
            tot++;
            e[tot] = (edge){v,w,cost,head[u]};
            head[u] = tot;
            tot++;
            e[tot] = (edge){u,0,-cost,head[v]};
            head[v] = tot;
    }
    inline bool spfa()
    {
            int i,u,v,w,cost,l,r;
            static int q[MAXN * MAXN * 2];
            static bool inq[MAXN * MAXN * 2];
            for (i = 1; i <= T; i++) 
            {
                    dist[i] = -inf;
                    incf[i] = inf;
            }
            memset(inq,false,sizeof(inq));
            inq[S] = true;
            pre[S] = 0;
            dist[S] = 0;
            incf[S] = inf;
            q[l = r = 1] = S;
            while (l <= r)
            {
                    u = q[l];
                    l++;
                    inq[u] = false;
                    for (i = head[u]; i; i = e[i].nxt)
                    {
                            v = e[i].to;
                            w = e[i].w;
                            cost = e[i].cost;
                            if (w && dist[u] + cost > dist[v])
                            {
                                    dist[v] = dist[u] + cost;
                                    incf[v] = min(incf[u],w);
                                    pre[v] = i;
                                    if (!inq[v])
                                    {
                                            inq[v] = true;
                                            q[++r] = v;
                                    }
                            }
                    }
            }
            if (dist[T] != -inf) return true;
            else return false;
    }
    inline void update()
    {
            int x = T,pos;
            while (x != S)
            {
                    pos = pre[x];
                    e[pos].w -= incf[T];
                    e[pos ^ 1].w += incf[T];
                    x = e[pos ^ 1].to;
            }
            ans += dist[T] * incf[T];
    }
    int main() 
    {
            
            scanf("%d%d",&n,&k);
            tot = 1;
            for (i = 1; i <= n; i++)
            {
                    for (j = 1; j <= n; j++)
                    {
                            scanf("%d",&val);
                            addedge(id(i,j,0),id(i,j,1),1,val);
                            addedge(id(i,j,0),id(i,j,1),k - 1,0);
                            if (i < n) addedge(id(i,j,1),id(i + 1,j,0),k,0);
                            if (j < n) addedge(id(i,j,1),id(i,j + 1,0),k,0);        
                    }        
            }
            S = 1;
            T = 2 * n * n;
            while (spfa()) update();
            printf("%d
    ",ans);
            
            return 0;
        
    }
  • 相关阅读:
    由一段代码说开去——set
    由一段代码说开去——list
    day 11
    day 16
    day 10
    day 13
    day 9
    day 18
    day 12
    day 14
  • 原文地址:https://www.cnblogs.com/evenbao/p/9426504.html
Copyright © 2020-2023  润新知