結果

問題 No.177 制作進行の宮森あおいです!
ユーザー y_mazuny_mazun
提出日時 2015-04-02 23:39:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,001 bytes
コンパイル時間 801 ms
コンパイル使用メモリ 79,316 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 03:46:01
合計ジャッジ時間 1,598 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

#include <queue>
#include <iostream>
#include <algorithm>
#include <set>
#include <climits>

using namespace std;

struct Edge{
  int cap; // capacity
  int to;
  int rev; // reverse edge id

  Edge(){}
  Edge(int c, int t, int r) :
    cap(c), to(t), rev(r){}
};

template<class E> // Edge type
class Graph{
public:
  typedef std::vector<std::vector<E> > G;

private:
  G g;

public:
  Graph(int n) : g(G(n)) {}

  void addEdge(int from, int to, int cap){
    g[from].push_back(E(cap, to, g[to].size()));
    g[to].push_back(E(0, from, g[from].size() - 1));
  }

  void addEdge(int from, int to, int cap, int cost){
    g[from].push_back(E(cap, to, cost, g[to].size()));
    g[to].push_back(E(0, from, -cost, g[from].size() - 1));
  }

  G &getRowGraph(){
    return g;
  }
};


template<class E>
class Dinic{
  typedef typename Graph<E>::G G;
  G &g;
  std::size_t n; // size of graph

  std::vector<int> level;
  std::vector<int> iter;

  // other utilities

  // search length of shortest path from s
  void bfs(int s){
    std::queue<int> que;
    level = std::vector<int>(n, -1);

    level[s] = 0;
    que.push(s);

    while(!que.empty()){
      int v = que.front(); que.pop();
      for(int i = 0; i < (int)g[v].size(); i++){
        E &e = g[v][i];
        if(e.cap > 0 && level[e.to] < 0){
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }

  // search path
  int dfs(int v, int t, int f){
    if(v == t) return f;
    for(int &i = iter[v]; i < (int)g[v].size(); i++){
      E &e = g[v][i];
      if(e.cap > 0 && level[v] < level[e.to]){
        int d = dfs(e.to, t, min(f, e.cap));
        if(d > 0){
          e.cap -= d;
          g[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

public:
  Dinic(Graph<E> &graph) : g(graph.getRowGraph()){
    n = g.size();
  }

  // Max flow of the flow from s to t.
  int solve(int s, int t){
    int flow = 0;
    while(true){
      int f;
      bfs(s);
      if(level[t] < 0) return flow;
      iter  = std::vector<int>(n, 0);
      while((f = dfs(s, t, INT_MAX)) > 0){
        flow += f;
      }
    }
  }
};

template<class E>
int dinic(Graph<E> &g, int s, int d){
  return Dinic<E>(g).solve(s, d);
}


int main(){
  const int w = getInt();
  const int n = getInt();
  vector<int> j(n);
  REP(i,n) j[i] = getInt();

  const int m = getInt();
  vector<int> c(m);
  REP(i,m) c[i] = getInt();

  vector<vector<int> > x(m, vector<int>(n));
  REP(i,m){
    const int q = getInt();
    REP(k,q) x[i][getInt() - 1] = 1;
  }

  Graph<Edge> g(n + m + 2);
  const int src = n + m;
  const int dst = n + m + 1;

  REP(i,n){
    g.addEdge(src, i, j[i]);
    REP(k,m) if(!x[k][i]){
      g.addEdge(i, n + k, j[i]);
    }
  }
  REP(i,m){
    g.addEdge(n + i, dst, c[i]);
  }

  puts(dinic(g, src, dst) >= w ? "SHIROBAKO" : "BANSAKUTSUKITA");

  return 0;
}
0