提出 #911085


ソースコード 拡げる

#include <bits/stdc++.h>
#define MAX_V 30000
#define INF 1e7
using namespace std;

struct edge{ int to,cap,rev; };

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap) {
  G[from].push_back((edge){to,cap,G[to].size()});
  G[to].push_back((edge){from,0,G[from].size()-1});
}

int flow_dfs(int v, int t, int f){
  if(v == t) return f;
  used[v] = true;
  for(int i=0;i<(int)G[v].size();i++){
    edge &e = G[v][i];
    if(!used[e.to] && e.cap > 0){
      int d = flow_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;
}

int max_flow(int s,int t){
  if(s == t) return INF;
  int flow = 0;
  while(1){
    memset(used,0,sizeof(used));
    int f = flow_dfs(s,t,INF);
    if(f == 0) break;
    flow += f;
    if(flow >= INF)return INF;
  }
  return flow;
}



int main(){
  char mp[100][100];
  int w,h;
  cin>>h>>w;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin>>mp[i][j];
    }
  }

  int s=2*h*w,t=s+1;
  int nx[4] = {0,1,0,-1},ny[4] = {-1,0,1,0};
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(mp[i][j] == 'X'){
        add_edge(s,i*w+j,INF);
        add_edge( i*w+j , i*w+j+h*w , INF );
      }
      else add_edge( i*w+j , i*w+j+h*w , 1 );
      for(int k=0;k<4;k++){
        int ni=i+ny[k],nj=j+nx[k];
        if(ni<0 || ni>=h || nj<0 || nj>=w) add_edge(i*w+j+w*h,t,INF);
        else add_edge(i*w+j+w*h,ni*w+nj,INF);
      }
    }
  }

  int ans = max_flow(s,t);
  if(ans == INF) cout<<-1<<endl;
  else cout<<ans<<endl;

  return 0;
}

提出情報

提出日時
問題 E - 柵
ユーザ mgr0204
言語 C++14 (GCC 5.4.1)
得点 150
コード長 1668 Byte
結果 AC
実行時間 137 ms
メモリ 3968 KiB

コンパイルエラー

./Main.cpp: In function ‘void add_edge(int, int, int)’:
./Main.cpp:12:45: warning: narrowing conversion of ‘G[to].std::vector<_Tp, _Alloc>::size<edge, std::allocator<edge> >()’ from ‘std::vector<edge>::size_type {aka long unsigned int}’ to ‘int’ inside { } [-Wnarrowing]
   G[from].push_back((edge){to,cap,G[to].size()});
                                             ^
./Main.cpp:13:47: warning: narrowing conversion of ‘(G[from].std::vector<_Tp, _Alloc>::size<edge, std::allocator<edge> >() + 18446744073709551615ul)’ from ‘std::vector<edge>::size_type {aka long unsigned int}’ to ‘int’ inside { } [-Wnarrowing]
   G[to].push_back((edge){from,0,G[from].size()-1});
                                               ^

ジャッジ結果

セット名 All
得点 / 配点 150 / 150
結果
AC × 19
セット名 テストケース
All 00_sample.txt, 01_sample.txt, 02_sample.txt, 10_rand_00.txt, 10_rand_01.txt, 10_rand_02.txt, 10_rand_03.txt, 10_rand_04.txt, 10_rand_05.txt, 10_rand_06.txt, 10_rand_07.txt, 10_rand_08.txt, 11_hashi_00.txt, 11_hashi_01.txt, 11_hashi_02.txt, 12_rect_00.txt, 12_rect_01.txt, 12_rect_02.txt, 99_all_one.txt
ケース名 結果 実行時間 メモリ
00_sample.txt AC 3 ms 1024 KiB
01_sample.txt AC 3 ms 1024 KiB
02_sample.txt AC 3 ms 1024 KiB
10_rand_00.txt AC 4 ms 1024 KiB
10_rand_01.txt AC 4 ms 1024 KiB
10_rand_02.txt AC 20 ms 1792 KiB
10_rand_03.txt AC 15 ms 1792 KiB
10_rand_04.txt AC 107 ms 3968 KiB
10_rand_05.txt AC 137 ms 3328 KiB
10_rand_06.txt AC 6 ms 1792 KiB
10_rand_07.txt AC 49 ms 2432 KiB
10_rand_08.txt AC 78 ms 3072 KiB
11_hashi_00.txt AC 7 ms 1920 KiB
11_hashi_01.txt AC 5 ms 1408 KiB
11_hashi_02.txt AC 7 ms 2048 KiB
12_rect_00.txt AC 34 ms 2944 KiB
12_rect_01.txt AC 8 ms 1536 KiB
12_rect_02.txt AC 70 ms 3328 KiB
99_all_one.txt AC 11 ms 3328 KiB