Submission #110298


Source Code Expand

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>

#define FOR(i, a, n) for(int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)

using namespace std;

const int MAX_H = 110;
const int MAX_W = 110;
const int MAX_N = MAX_H + MAX_W;

int h, w, n;
int u[MAX_H][MAX_W];
int f[MAX_W];
int s[MAX_H];

vector<int> g[MAX_N];
bool used[MAX_N];
int match[MAX_N];

bool check(){
  vector< vector<int> > t(h, vector<int>(w));
  REP(i, h) REP(j, w){
    if(u[i][j]){
      t[i][j] =  min(s[i], f[j]);
      if(!t[i][j]) return false;
    }else{
      t[i][j] = 0;
    }
  }
  REP(i, w){
    int tf = 0;
    REP(j, h){
      tf = max(tf, t[j][i]);
    }
    if(tf != f[i]) return false;
  }
  REP(i, h){
    int ts = 0;
    REP(j, w){
      ts = max(ts, t[i][j]);
    }
    if(ts != s[i]) return false;
  }
  return true;
}

bool dfs(int v){
  used[v] = true;
  REP(i, (int)g[v].size()){
    int u = g[v][i], w = match[u];
    if(w == -1 || (!used[w] && dfs(w))){
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching(){
  int r = 0;
  memset(match, -1, sizeof(match));
  REP(i, n){
    if(match[i] == -1){
      memset(used, false, sizeof(used));
      if(dfs(i)) r++;
    }
  }
  return r;
}


int solve(){
  if(!check()) return -1;
  REP(i, h) REP(j, w){
    if(u[i][j] && s[i] == f[j]){
      g[i].push_back(h + j);
      g[h + j].push_back(i);
    }
  }
  bipartite_matching();
  int r = 0, ct = 0;
  REP(i, h) if(s[i]){
    r += s[i];
    ct++;
  }
  REP(i, w) if(f[i] && match[h + i] == -1){
    r += f[i];
    ct++;
  }
  REP(i, h) REP(j, w){
    r += u[i][j];
  }
  return r - ct;
}

int main(){
  cin >> h >> w;
  n = h + w;
  REP(i, n) g[i].clear();
  REP(i, h) REP(j, w) cin >> u[i][j];
  REP(i, w) cin >> f[i];
  REP(i, h) cin >> s[i];
  cout << solve() << endl;
}

Submission Info

Submission Time
Task C - Containers
User torimal
Language C++ (GCC 4.4.7)
Score 100
Code Size 1963 Byte
Status AC
Exec Time 26 ms
Memory 928 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 49
Set Name Test Cases
All 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 6, 7, 8, 9
Case Name Status Exec Time Memory
1 AC 21 ms 924 KB
10 AC 21 ms 920 KB
11 AC 22 ms 924 KB
12 AC 21 ms 920 KB
13 AC 23 ms 804 KB
14 AC 21 ms 920 KB
15 AC 23 ms 792 KB
16 AC 21 ms 928 KB
17 AC 21 ms 928 KB
18 AC 22 ms 928 KB
19 AC 23 ms 856 KB
2 AC 23 ms 796 KB
20 AC 24 ms 924 KB
21 AC 23 ms 808 KB
22 AC 24 ms 924 KB
23 AC 24 ms 924 KB
24 AC 24 ms 812 KB
25 AC 24 ms 928 KB
26 AC 24 ms 928 KB
27 AC 24 ms 924 KB
28 AC 24 ms 920 KB
29 AC 24 ms 924 KB
3 AC 23 ms 792 KB
30 AC 24 ms 792 KB
31 AC 22 ms 924 KB
32 AC 22 ms 920 KB
33 AC 21 ms 920 KB
34 AC 23 ms 920 KB
35 AC 22 ms 796 KB
36 AC 25 ms 920 KB
37 AC 22 ms 920 KB
38 AC 22 ms 928 KB
39 AC 23 ms 924 KB
4 AC 23 ms 928 KB
40 AC 22 ms 924 KB
41 AC 24 ms 912 KB
42 AC 21 ms 924 KB
43 AC 23 ms 792 KB
44 AC 26 ms 924 KB
45 AC 22 ms 920 KB
46 AC 22 ms 812 KB
47 AC 21 ms 928 KB
48 AC 19 ms 920 KB
49 AC 20 ms 812 KB
5 AC 21 ms 924 KB
6 AC 21 ms 928 KB
7 AC 21 ms 928 KB
8 AC 23 ms 792 KB
9 AC 23 ms 812 KB