zoukankan      html  css  js  c++  java
  • UVA11383 Golden Tiger Claw

    题目大意:给定一个N*N矩阵,每个格子(i,j)有正权值w(i,j),要求你为每一行r、每一列c确定一个数l(r)、l(c),要求对任一格子(i,j)有l(r) + l(c) >= w(i, j),要求所有l之和尽量小

    题解:KM裸题,不学KM可能会死很惨

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <map>
     9 #include <string> 
    10 #include <cmath> 
    11 #define min(a, b) ((a) < (b) ? (a) : (b))
    12 #define max(a, b) ((a) > (b) ? (a) : (b))
    13 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
    14 template<class T>
    15 inline void swap(T &a, T &b)
    16 {
    17     T tmp = a;a = b;b = tmp;
    18 }
    19 inline void read(int &x)
    20 {
    21     x = 0;char ch = getchar(), c = ch;
    22     while(ch < '0' || ch > '9') c = ch, ch = getchar();
    23     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
    24     if(c == '-') x = -x;
    25 }
    26 const int INF = 0x3f3f3f3f;
    27 const int MAXN = 500 + 10;
    28 int g[MAXN][MAXN], n1, n2, lab1[MAXN], lab2[MAXN], lk1[MAXN], lk2[MAXN], pre[MAXN], vis[MAXN], sla[MAXN];
    29 void cal(int x)
    30 {
    31     memset(vis, 0, sizeof(vis)), memset(pre, 0, sizeof(pre)), memset(sla, 0x3f, sizeof(sla)), vis[0] = 1;
    32     int y;
    33     do
    34     {
    35         y = 0;
    36         for(int i = 1;i <= n2;++ i)
    37         {
    38             if(vis[i]) continue;
    39             if(lab1[x] + lab2[i] - g[x][i] < sla[i]) sla[i] = lab1[x] + lab2[i] - g[x][i], pre[i] = x;
    40             if(sla[i] < sla[y]) y = i;
    41         }
    42         int d = sla[y];
    43         for(int i = 1;i <= n1;++ i) if(vis[lk1[i]]) lab1[i] -= d;
    44         for(int i = 1;i <= n2;++ i) if(vis[i]) lab2[i] += d; else sla[i] -= d;
    45         vis[y] = 1;
    46     }while(x = lk2[y]);
    47     for(;y;swap(y, lk1[lk2[y] = pre[y]]));
    48 }
    49 int KM()
    50 {
    51     for(int i = 1;i <= n1;++ i) cal(i); int ans = 0;
    52     for(int i = 1;i <= n1;++ i) ans += g[i][lk1[i]]; return ans; 
    53 } 
    54 int main()
    55 {
    56     while(scanf("%d", &n1) != EOF)
    57     {
    58         n2 = n1;memset(g, 0, sizeof(g)), memset(lab1, 0, sizeof(lab1)), memset(lab2, 0, sizeof(lab2)), memset(lk1, 0, sizeof(lk1)), memset(lk2, 0, sizeof(lk2));
    59         for(int i = 1;i <= n1;++ i)
    60             for(int j = 1;j <= n2;++ j)
    61                 read(g[i][j]), lab1[i] = max(lab1[i], g[i][j]);
    62         int ans = KM();
    63         for(int i = 1;i < n1;++ i) printf("%d ", lab1[i]); printf("%d
    ", lab1[n1]);
    64         for(int i = 1;i < n2;++ i) printf("%d ", lab2[i]); printf("%d
    ", lab2[n2]);
    65         printf("%d
    ", ans);
    66     }
    67     return 0;
    68 }
    UVA11383
  • 相关阅读:
    OpenCv 014---图像插值(Image interpolation)
    基础正则
    Base -快捷键|通配符|特殊符号|输出(正确与错误的保存)
    linux 软链接 硬链接
    shell 别名alias
    yii2 源码分析1从入口开始
    php const static define 基本用法和区别
    php-fpm, nginx ,fastcgi ,php-cgi 关系粗解
    php扩展开发3--扩展类传参数
    php扩展开发2--添加类
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/8384822.html
Copyright © 2011-2022 走看看