Submission #3006858


Source Code Expand

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <set>
#include <map>
#define REP(i,n) for(ll i = 0; i < (ll)n; i++)
#define INF 1000000000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;

struct UnionFind{
  vector<ll> par;
  vector<ll> rank;
  vector<ll> sizes;

  UnionFind(ll n){
    init(n);
  }

  void init(ll n){
    par.resize(n);
    rank.resize(n);
    sizes.resize(n);
    REP(i,n){
      par[i] = i;
      rank[i] = 0;
      sizes[i] = 1;
    }
  }

  ll find(ll x){
    if(par[x] == x){
      return x;
    }else{
      return par[x] = find(par[x]);
    }
  }

  void unite(ll x, ll y){
    x = find(x);
    y = find(y);
    if(x==y) return;
    if(rank[x]<rank[y]){
      par[x] = y;
      sizes[y] += sizes[x];
    }else{
      par[y] = x;
      sizes[x] += sizes[y];
      if(rank[x]==rank[y]) rank[x]++;
    }
  }

  bool same(ll x, ll y){
    return find(x)==find(y);
  }

  ll size(ll x){
    return sizes[find(x)];
  }
};
struct edge{ll from, to, cost;};
bool compare_edge(const edge& e1, const edge& e2){
  return e1.cost<e2.cost;
}
struct graph{
  vector<edge> es;
  ll V;
  graph(ll n){
    init(n);
  }
  void init(ll n){
    V = n;
  }
  void add_edge(ll from, ll to, ll cost){
    edge e;
    e.from = from, e.to = to, e.cost = cost;
    es.push_back(e);
  }
  ll kruskal(){
    sort(es.begin(),es.end(),compare_edge);
    UnionFind uf(V);
    ll res = 0;
    for(auto e : es){
      if(!uf.same(e.from,e.to)){
        uf.unite(e.from,e.to);
        res += e.cost;
      }
    }
    return res;
  }
};

ll h,w;
ll calc_pos(ll y, ll x){
  return y*w+x;
}

int main(){
  cin >> h >> w;
  ll sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  graph g(h*w);
  ll p[h][w];
  ll point_sum = 0;
  REP(i,h)REP(j,w){
    cin >> p[i][j];
    point_sum += p[i][j];
  }
  REP(i,h-1)REP(j,w){
    g.add_edge(calc_pos(i,j),calc_pos(i+1,j),-p[i][j]*p[i+1][j]);
  }
  REP(i,h)REP(j,w-1){
    g.add_edge(calc_pos(i,j),calc_pos(i,j+1),-p[i][j]*p[i][j+1]);
  }
  ll max_bonus = -g.kruskal();
  cout << point_sum + max_bonus << endl;
  return 0;
}

Submission Info

Submission Time
Task D - Game on a Grid
User nexusuica
Language C++14 (GCC 5.4.1)
Score 100
Code Size 2306 Byte
Status AC
Exec Time 6 ms
Memory 1276 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 32
Set Name Test Cases
Sample subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt
All subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_20.txt, subtask1_21.txt, subtask1_22.txt, subtask1_23.txt, subtask1_24.txt, subtask1_25.txt, subtask1_26.txt, subtask1_27.txt, subtask1_28.txt, subtask1_29.txt
Case Name Status Exec Time Memory
subtask0_sample_01.txt AC 1 ms 256 KB
subtask0_sample_02.txt AC 1 ms 256 KB
subtask0_sample_03.txt AC 1 ms 256 KB
subtask1_01.txt AC 1 ms 256 KB
subtask1_02.txt AC 1 ms 256 KB
subtask1_03.txt AC 1 ms 256 KB
subtask1_04.txt AC 1 ms 256 KB
subtask1_05.txt AC 1 ms 256 KB
subtask1_06.txt AC 1 ms 256 KB
subtask1_07.txt AC 5 ms 1276 KB
subtask1_08.txt AC 5 ms 1276 KB
subtask1_09.txt AC 6 ms 1276 KB
subtask1_10.txt AC 5 ms 1276 KB
subtask1_11.txt AC 5 ms 1276 KB
subtask1_12.txt AC 5 ms 1276 KB
subtask1_13.txt AC 5 ms 1276 KB
subtask1_14.txt AC 5 ms 1276 KB
subtask1_15.txt AC 5 ms 1276 KB
subtask1_16.txt AC 5 ms 1276 KB
subtask1_17.txt AC 1 ms 256 KB
subtask1_18.txt AC 1 ms 256 KB
subtask1_19.txt AC 1 ms 256 KB
subtask1_20.txt AC 4 ms 1276 KB
subtask1_21.txt AC 5 ms 1276 KB
subtask1_22.txt AC 5 ms 1276 KB
subtask1_23.txt AC 5 ms 1276 KB
subtask1_24.txt AC 1 ms 256 KB
subtask1_25.txt AC 1 ms 256 KB
subtask1_26.txt AC 1 ms 256 KB
subtask1_27.txt AC 1 ms 256 KB
subtask1_28.txt AC 1 ms 256 KB
subtask1_29.txt AC 1 ms 256 KB