Submission #3176456
Source Code Expand
#include<bits/stdc++.h> #pragma warning(disable:4996) using namespace std; using ll = long long; const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; //#define int long long int H, W, sx, sy, gx, gy; vector < vector<int>>grid; class union_find_tree { private: vector<int>par, rank_; public: union_find_tree(int n) :par(n), rank_(n) { for (int i = 0; i < n; i++) { par[i] = i; rank_[i] = 0; } } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) { return; } if (rank_[x] < rank_[y]) { par[x] = y; } else if (rank_[x] == rank_[y]) { par[y] = x; rank_[x]++; } else if (rank_[y] < rank_[x]) { par[y] = x; } } int root(int x) { if (par[x] == x) { return x; } return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } }; struct edge { int from, to, cost; bool operator<(const edge& other)const { //return cost < other.cost; return other.cost < cost; } }; using edges = vector<edge>; int kruskal(union_find_tree& uf, edges& es, int V) { sort(es.begin(), es.end()); int E = es.size(); int res = 0; for (int i = 0; i < E; i++) { if (!uf.same(es[i].from, es[i].to)) { uf.unite(es[i].from, es[i].to); res += es[i].cost; } } return res; } signed main(){ cin >> H >> W >> sx >> sy >> gx >> gy; grid.resize(H); int sum = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int p; cin >> p; sum += p; grid[i].push_back(p); } } vector<edge>es; union_find_tree uf(H*W); for (int i = 0; i < H; i++) { for (int j = 0; j < W - 1; j++) { edge e; e.cost = grid[i][j] * grid[i][j + 1]; e.from = i * W + j; e.to = i * W + j + 1; es.push_back(e); edge e1; e1.cost= grid[i][j] * grid[i][j + 1]; e1.from = i * W + j+1; e1.to = i * W + j; es.push_back(e1); } } for (int i = 0; i < W; i++) { for (int j = 0; j < H - 1; j++) { edge e; e.cost = grid[i][j] * grid[i+1][j]; e.from = i * W + j; e.to = (i+1) * W + j; es.push_back(e); edge e1; e1.cost = grid[i+1][j] * grid[i][j]; e1.from = i * W + j; e1.to = (i+1) * W + j; es.push_back(e1); } } int res = kruskal(uf, es, H*W); cout << res + sum << endl; }
Submission Info
Submission Time | |
---|---|
Task | D - Game on a Grid |
User | doikimihiro |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 2340 Byte |
Status | RE |
Exec Time | 100 ms |
Memory | 1276 KB |
Judge Result
Set Name | Sample | All | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 100 | ||||||||||
Status |
|
|
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 | RE | 97 ms | 256 KB |
subtask0_sample_03.txt | RE | 97 ms | 256 KB |
subtask1_01.txt | AC | 1 ms | 256 KB |
subtask1_02.txt | RE | 96 ms | 256 KB |
subtask1_03.txt | AC | 1 ms | 256 KB |
subtask1_04.txt | RE | 98 ms | 256 KB |
subtask1_05.txt | RE | 97 ms | 256 KB |
subtask1_06.txt | RE | 97 ms | 256 KB |
subtask1_07.txt | RE | 100 ms | 1276 KB |
subtask1_08.txt | RE | 100 ms | 1276 KB |
subtask1_09.txt | RE | 99 ms | 1276 KB |
subtask1_10.txt | RE | 100 ms | 1276 KB |
subtask1_11.txt | RE | 100 ms | 1276 KB |
subtask1_12.txt | RE | 99 ms | 1276 KB |
subtask1_13.txt | RE | 99 ms | 1276 KB |
subtask1_14.txt | RE | 100 ms | 1276 KB |
subtask1_15.txt | RE | 100 ms | 1276 KB |
subtask1_16.txt | RE | 100 ms | 1276 KB |
subtask1_17.txt | WA | 1 ms | 256 KB |
subtask1_18.txt | RE | 97 ms | 256 KB |
subtask1_19.txt | WA | 1 ms | 256 KB |
subtask1_20.txt | RE | 99 ms | 1276 KB |
subtask1_21.txt | RE | 99 ms | 1276 KB |
subtask1_22.txt | RE | 99 ms | 1276 KB |
subtask1_23.txt | RE | 100 ms | 1276 KB |
subtask1_24.txt | RE | 97 ms | 256 KB |
subtask1_25.txt | RE | 97 ms | 256 KB |
subtask1_26.txt | RE | 98 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 |