WC 2007 剪刀石头布

WC 2007 剪刀石头布

看到这个三元环的问题很容易可以考虑到求不合法的三元环的数量的最小值。

什么情况不合法?既然不合法,当且仅当三元环中有一个人赢了另外两个人。所以我们考虑对于一个人而言,如果她新增加了一场胜利,并且之前赢了 $ k $ 场,那么她的非剪刀石头布的胜利次数会增加 $ k $ 个。并且这样统计每个不合法情况正好只会被统计一遍。

这里就学到了一个非常厉害的建图方法,我们从每个人到 $ t $ 连很多条边,容量都是 1 ,费用设置为 $ 0,1,2,3,\dots $ 。然后从比赛向胜利者建立一条容量为 1 费用为 0 的边,从不确定的比赛向两方都建容量为 1 费用为 0 的边。

这样建图结束后,跑最小费用最大流就好了。

开始写的单路增广狂T。。。

#include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "queue"
using namespace std;
#define MAXN 1006

class mincmaxf {
#define maxn 60003
public:
#define N 10006
#define M 100006
#define INF 0x3f3f3f3f
    int tot, lnk[N], cur[N], ter[M], nxt[M], cap[M], cost[M], dis[N], ret;
    bool vis[N];
    void init( ) { tot = 1; }
    int add(int u, int v, int w, int c) {
        ter[++tot] = v, nxt[tot] = lnk[u], lnk[u] = tot, cap[tot] = w, cost[tot] = c;
        return tot;
    }
    int Ade(int u, int v, int w, int c) { add(v, u, 0, -c); return add(u, v, w, c); }
    bool spfa(int s, int t) {
        memset(dis, 0x3f, sizeof(dis));
        memcpy(cur, lnk, sizeof(lnk));
        std::queue<int> q;
        q.push(s), dis[s] = 0, vis[s] = 1;
        while (!q.empty()) {
            int u = q.front();
            q.pop(), vis[u] = 0;
            for (int i = lnk[u]; i; i = nxt[i]) {
                int v = ter[i];
                if (cap[i] && dis[v] > dis[u] + cost[i]) {
                    dis[v] = dis[u] + cost[i];
                    if (!vis[v]) q.push(v), vis[v] = 1;
                }
            }
        }
        return dis[t] != INF;
    }
    int dfs(int u, int t, int flow) {
        if (u == t) return flow;
        vis[u] = 1;
        int ans = 0;
        for (int &i = cur[u]; i && ans < flow; i = nxt[i]) {
            int v = ter[i];
            if (!vis[v] && cap[i] && dis[v] == dis[u] + cost[i]) {
                int x = dfs(v, t, std::min(cap[i], flow - ans));
                if (x) ret += x * cost[i], cap[i] -= x, cap[i ^ 1] += x, ans += x;
            }
        }
        vis[u] = 0;
        return ans;
    }
    int mcmf(int s, int t) {
        int ans = 0;
        while (spfa(s, t)) {
            int x;
            while ((x = dfs(s, t, INF))) ans += x;
        }
        return ret;
    }
} F ;
int n;
int A[MAXN][MAXN] , re[MAXN][MAXN][2];
int s = 10002 , t = 10003;
int kk[MAXN][MAXN] , cnt;
inline int id( int x , int y ) { return !kk[x][y] ? (kk[x][y] = ++ cnt) : kk[x][y]; }
int r[MAXN];
int main() {
//    freopen("6.in","r",stdin);
    cin >> n;
    cnt = n;
    F.init();
    for( int i = 1 ; i <= n ; ++ i )
        for( int j = 1 ; j <= n ; ++ j ) {
            scanf("%d",&A[i][j]);
            if( A[i][j] ) F.Ade( s , id( i , j ) , 1 , 0 );
            if( A[i][j] == 2 ) {
                if( i < j )
                    re[i][j][0] = F.Ade(id(i, j), i, 1, 0), re[i][j][1] = F.Ade(id(i, j), j, 1, 0);
            } else if( A[i][j] )
                F.Ade( id( i , j ) , i , 1 , 0 );
            else ++ r[i];
        }
    for( int i = 1 ; i <= n ; ++ i ) {
        for( int j = 0 ; j < n - r[i] ; ++ j )
            F.Ade( i , t , 1 , j );
    }
    cout << ( n * ( n - 1 ) / 2 * ( n - 2 ) / 3 ) - F.mcmf( s , t ) << endl;
    for( int i = 1 ; i <= n ; ++ i ) {
        for (int j = 1; j <= n; ++j) {
            if (A[i][j] != 2) { printf("%d ",A[i][j]); }
            else {
                int ki = i , kj = j;
                if( i > j ) swap( ki , kj );
                if( F.cap[re[ki][kj][0]] ) printf("%d ",i>j);
                else printf("%d ",i<j);
            }
        }
        puts("");
    }
}

posted @ 2020-02-20 15:16  yijan  阅读(110)  评论(0编辑  收藏  举报