zoukankan      html  css  js  c++  java
  • [HDOJ2255]奔小康赚大钱(二分图最大匹配, KM算法)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2255

    题意:很明显了,连图都是明摆着的。带权值的最大匹配,直接用km算法。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rs(a) scanf("%s", a)
     44 #define Cin(a) cin >> a
     45 #define FRead() freopen("in", "r", stdin)
     46 #define FWrite() freopen("out", "w", stdout)
     47 #define Rep(i, len) for(int i = 0; i < (len); i++)
     48 #define For(i, a, len) for(int i = (a); i < (len); i++)
     49 #define Cls(a) memset((a), 0, sizeof(a))
     50 #define Clr(a, x) memset((a), (x), sizeof(a))
     51 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
     52 #define lrt rt << 1
     53 #define rrt rt << 1 | 1
     54 #define pi 3.14159265359
     55 #define RT return
     56 #define lowbit(x) x & (-x)
     57 #define onecnt(x) __builtin_popcount(x)
     58 typedef long long LL;
     59 typedef long double LD;
     60 typedef unsigned long long ULL;
     61 typedef pair<int, int> pii;
     62 typedef pair<string, int> psi;
     63 typedef pair<LL, LL> pll;
     64 typedef map<string, int> msi;
     65 typedef vector<int> vi;
     66 typedef vector<LL> vl;
     67 typedef vector<vl> vvl;
     68 typedef vector<bool> vb;
     69 
     70 const int maxn = 330;
     71 const int inf = 0x3f3f3f3f;
     72 int n;
     73 int nx,ny;
     74 int G[maxn][maxn], mp[maxn][maxn];
     75 int linker[maxn],lx[maxn],ly[maxn];
     76 int slack[maxn];
     77 bool visx[maxn],visy[maxn];
     78 
     79 bool dfs(int x) {
     80     visx[x] = true;
     81     for(int y = 0; y < ny; y++) {
     82         if(visy[y])continue;
     83         int tmp = lx[x] + ly[y] - G[x][y];
     84         if(tmp == 0) {
     85             visy[y] = true;
     86             if(linker[y] == -1 || dfs(linker[y])) {
     87                 linker[y] = x;
     88                 return true;
     89             }
     90         }
     91         else if(slack[y] > tmp)
     92             slack[y] = tmp;
     93     }
     94     return false;
     95 }
     96 int km() {
     97     memset(linker,-1,sizeof(linker));
     98     memset(ly,0,sizeof(ly));
     99     for(int i = 0;i < nx;i++) {
    100         lx[i] = -inf;
    101         for(int j = 0;j < ny;j++)
    102             if(G[i][j] > lx[i])
    103                 lx[i] = G[i][j];
    104     }
    105     for(int x = 0;x < nx;x++) {
    106         for(int i = 0;i < ny;i++)
    107             slack[i] = inf;
    108         while(true) {
    109             memset(visx,false,sizeof(visx));
    110             memset(visy,false,sizeof(visy));
    111             if(dfs(x))break;
    112             int d = inf;
    113             for(int i = 0;i < ny;i++)
    114                 if(!visy[i] && d > slack[i])
    115                     d = slack[i];
    116             for(int i = 0;i < nx;i++)
    117                 if(visx[i])
    118                     lx[i] -= d;
    119             for(int i = 0;i < ny;i++) {
    120                 if(visy[i])ly[i] += d;
    121                 else slack[i] -= d;
    122             }
    123         }
    124     }
    125     int res = 0;
    126     for(int i = 0;i < ny;i++)
    127         if(linker[i] != -1)
    128             res += G[linker[i]][i];
    129     return res;
    130 }
    131 
    132 int main() {
    133     // FRead();
    134     while(~Rint(n)) {
    135         nx = ny = n;
    136         Rep(i, n) {
    137             Rep(j, n) {
    138                 Rint(G[i][j]);
    139             }
    140         }
    141         printf("%d
    ", km());
    142     }
    143     RT 0;
    144 }
  • 相关阅读:
    第一篇:白话tornado源码之一个脚本引发的血案
    第二篇:白话tornado源码之待请求阶段
    Python 面向对象(初级篇)
    python 面向对象(进阶篇)
    Csharp启动exe文件
    UNIX时间戳
    JS日期格式化代码
    Android之什么是Activity和常用的ADB命令以及Android项目结构的认识
    thread同步测试
    Fluent NHibernate使用小结:(1)通用配置文件创建方法
  • 原文地址:https://www.cnblogs.com/kirai/p/5806065.html
Copyright © 2011-2022 走看看