Submission #110296


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]);
    }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 0
Code Size 1928 Byte
Status WA
Exec Time 25 ms
Memory 928 KB

Judge Result

Set Name All
Score / Max Score 0 / 100
Status
AC × 48
WA × 1
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 WA 21 ms 920 KB
11 AC 21 ms 924 KB
12 AC 21 ms 924 KB
13 AC 21 ms 800 KB
14 AC 25 ms 896 KB
15 AC 22 ms 928 KB
16 AC 21 ms 800 KB
17 AC 22 ms 844 KB
18 AC 22 ms 924 KB
19 AC 22 ms 920 KB
2 AC 23 ms 848 KB
20 AC 23 ms 924 KB
21 AC 23 ms 872 KB
22 AC 23 ms 920 KB
23 AC 22 ms 920 KB
24 AC 23 ms 920 KB
25 AC 24 ms 800 KB
26 AC 24 ms 924 KB
27 AC 24 ms 924 KB
28 AC 25 ms 928 KB
29 AC 25 ms 924 KB
3 AC 23 ms 796 KB
30 AC 22 ms 928 KB
31 AC 22 ms 928 KB
32 AC 22 ms 924 KB
33 AC 22 ms 928 KB
34 AC 20 ms 920 KB
35 AC 22 ms 916 KB
36 AC 22 ms 852 KB
37 AC 22 ms 924 KB
38 AC 22 ms 924 KB
39 AC 24 ms 796 KB
4 AC 21 ms 800 KB
40 AC 22 ms 876 KB
41 AC 24 ms 824 KB
42 AC 22 ms 924 KB
43 AC 21 ms 928 KB
44 AC 22 ms 920 KB
45 AC 21 ms 924 KB
46 AC 22 ms 920 KB
47 AC 22 ms 924 KB
48 AC 22 ms 916 KB
49 AC 22 ms 920 KB
5 AC 21 ms 924 KB
6 AC 22 ms 924 KB
7 AC 20 ms 924 KB
8 AC 22 ms 928 KB
9 AC 21 ms 928 KB