UOJ #4213. 贪吃蛇(上下界可行费用流)

思路:限制每个点的度数为2 ,边界点的度数可以花费1的代价减少1个度数 

坑点:我TM以后再也不用 ++ r 的优先级了,这个玩意在 C++ 和 C++11中不一样,而且TM错都不知道咋错的,我TM调了一个晚上就TM这么没了。。。。

#include <bits/stdc++.h>
using namespace std;
typedef int lint;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxr = 55;
const int maxc = 55;
struct EDGE {
    int from, to, next, cost, cap;  //  如果需要修改 cost为LL
};
namespace MFMC {
    const static int maxn = 100011;
    const static int maxm = 500005;
    EDGE edge[maxm];
    int tot, he[maxn], n;
    void init(int _n) {
        tot = 0;
        n = _n + 1;
        memset(he, -1, n * sizeof(int));
    }
    void Add(int u, int v, int cap,int cost) {   //  如果需要修改 cost为LL
        edge[tot] = EDGE{u, v, he[u], cost, cap};
        he[u] = tot++;
    }
    void add(int u, int v, int cap,int cost) {  //  如果需要修改 cost为LL
        Add(u, v,  cap,cost);
        Add(v, u,  0,-cost);
    }
//O(VE)
//record_e[i]是fa[i]->i的边的编号
    template<typename DT>
    void spfa(int s, DT dist[], int rec[]) {
        queue<int> q;
        static bool inq[maxn];

        memset(dist, 0x3f, n * sizeof(DT));
        memset(inq, 0, n * sizeof(bool));
        memset(rec, -1, n * sizeof(int));
        q.push(s);
        dist[s] = 0;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            inq[u] = 0;
            for (int e = he[u]; ~e; e = edge[e].next) {
                if (0 == edge[e].cap)
                    continue;
                int v = edge[e].to;
                if (dist[v] > dist[u] + edge[e].cost) {
                    dist[v] = dist[u] + edge[e].cost;
                    rec[v] = e;
                    if (!inq[v]) {
                        q.push(v);
                        inq[v] = 1;
                    }
                }
            }
        }
    }

    template<typename DT>
    void dijkstra(int s, DT dist[], int rec[]) {
        priority_queue<pair<DT, int> > q;//-dist, vertex

        memset(dist, 0x3f, n * sizeof(DT));
        memset(rec, -1, n * sizeof(int));
        dist[s] = 0;
        q.push(make_pair(0, s));
        while (!q.empty()) {
            s = q.top().second;
            DT c = -q.top().first;
            q.pop();
            if (c != dist[s]) continue;
            for (int e = he[s]; ~e; e = edge[e].next) {
                if (0 == edge[e].cap) continue;
                int v = edge[e].to;
                if (dist[v] > c + edge[e].cost) {
                    dist[v] = c + edge[e].cost;
                    rec[v] = e;
                    q.push(make_pair(-dist[v], v));
                }
            }
        }
    }

//Need dijkstra_GRAPH_EDGES_PQ
//O(FE log(E)),F is the maximum flow

    template<typename FT, typename CT>
    void mfmc(int s, int t, FT &maxflow, CT &mincost) {
        CT inf;
        memset(&inf, 0x3f, sizeof(CT));
        static CT dist[maxn];
        static int rec_e[maxn];
        maxflow = mincost = 0;
        CT realdist = 0;    //real distance from s to t

        bool first = true;
        while (1) {
            if (first) {
                spfa( s, dist, rec_e);
                first = false;
            } else {
                dijkstra( s, dist, rec_e);
            }
            if (inf == dist[t])
                break;
            FT minF = numeric_limits<FT>::max();
            for (int e = rec_e[t]; ~e; e = rec_e[edge[e].from])
                minF = min(minF, (FT) edge[e].cap);
            maxflow += minF;
            realdist += dist[t];
            mincost += minF * realdist;
            for (int e = rec_e[t]; ~e; e = rec_e[edge[e].from]) {
                edge[e].cap -= minF;
                edge[e ^ 1].cap += minF;
            }
            for (int e = 0; e < tot; ++e) {
                EDGE &ed = edge[e];
                ed.cost += dist[ed.from] - dist[ed.to];
            }
        }
    }
};
char str[maxr][maxc];
int dir[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };
int r,c;
int calc( int x,int y ){
    return (x-1)* c + y;
}
int main(){
    r = 1;
    while( EOF != scanf("%s",str[r]+1) ){
        r++;
    };
    r--;
    c= strlen(str[1]+1);
    int S = 0,T = r*c+1,SS = T+1,TT = T+2;
    MFMC::init(TT);
    MFMC::add( T,S,inf,0 );
    int res = 0;
    for( int i = 1;i <= r;i++ ){
        for( int j = 1;j <= c;j++ ){
            if( str[i][j] == '#' )continue;
            if( (i+j)%2 == 0 ){
                if(i==1||i==r||j==1||j==c)MFMC::add( calc(i,j),T,1,1 );
                MFMC::add( S,TT,2,0 );
                MFMC::add( SS,calc(i,j),2,0 );
                res += 2;
                for( int k = 0;k < 4;k++ ){
                    int ii = i+dir[k][0],jj = j+dir[k][1];
                    if( ii >= 1 && ii <= r && jj >= 1 && jj <= c ){
                        if( str[ii][jj] == '#' )continue;
                        MFMC::add( calc(i,j),calc( ii,jj ),1,0 );
                    }
                }
            }else {
                if(i==1||i==r||j==1||j==c)MFMC::add( S,calc( i,j ),1,1 );
                MFMC::add( calc(i,j),TT,2,0 );
                MFMC::add( SS,T,2,0 );
                res += 2;
            }
        }
    }
    int maxflow,mincost;
    MFMC::mfmc( SS,TT,maxflow,mincost );
    if( maxflow != res ){
        //cout << maxflow << " " << res << endl;
        puts("-1");
    }else{
        printf("%d\n",mincost/2);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值