Submission #2852237


Source Code Expand

#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <utility>

#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
const int inf = 1e9;
const ll linf = 1e18;

int h, w;
int sx, sy, gx, gy;
int p[101][101];

typedef struct edge_ {
		int from, to, cost;
		edge_(int f, int t, int c): from(f), to(t), cost(c){};
} edge;

int dx[2] = {-1, 0};
int dy[2] = {0, -1};

vector<edge> vec;

bool comp(const edge &ed1, const edge &ed2){
		return ed1.cost > ed2.cost;
}

int par[10001];
int r[10001];

void init(){
		for(int i = 0; i < 10001; i++){
				par[i] = i;
		}
}

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

void unite(int x, int y){
		int xp = find(x);
		int yp = find(y);
		if(xp == yp)return;
		if(r[xp] > r[yp]){
				par[yp] = xp;
		}else{
				par[xp] = yp;
				if(r[xp] == r[yp])r[yp]++;
		}
}

int main(int argc, char const* argv[])
{
	cin >> h >> w;
	cin >> sx >> sy >> gx >> gy;
	swap(sx, sy);
	swap(gx, gy);
	ll sum = 0;
	for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
					cin >> p[i][j];
					sum += p[i][j];
					for(int k = 0; k < 2; k++){
							int nx = dx[k] + i;
							int ny = dy[k] + j;
							if(nx >= 0 && nx < h && ny >= 0 && ny < w){
									vec.pb(edge(i * w + j, nx * w + ny, p[i][j] * p[nx][ny]));
							}
					}
			}
	}
	sort(vec.begin(), vec.end(), comp);
	init();
	for(auto itr = vec.begin(); itr != vec.end(); ++itr){
			int from = (*itr).from;
			int to = (*itr).to;
			int cost = (*itr).cost;
			if(find(from) != find(to)){
					sum += cost;
					unite(from, to);
			}
	}
	cout << sum << endl;
	return 0;
}

Submission Info

Submission Time
Task D - Game on a Grid
User fumiphys
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1883 Byte
Status AC
Exec Time 6 ms
Memory 832 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 6 ms 832 KB
subtask1_08.txt AC 6 ms 832 KB
subtask1_09.txt AC 5 ms 832 KB
subtask1_10.txt AC 6 ms 832 KB
subtask1_11.txt AC 6 ms 832 KB
subtask1_12.txt AC 6 ms 832 KB
subtask1_13.txt AC 5 ms 832 KB
subtask1_14.txt AC 5 ms 832 KB
subtask1_15.txt AC 6 ms 832 KB
subtask1_16.txt AC 5 ms 832 KB
subtask1_17.txt AC 1 ms 384 KB
subtask1_18.txt AC 1 ms 256 KB
subtask1_19.txt AC 1 ms 384 KB
subtask1_20.txt AC 4 ms 832 KB
subtask1_21.txt AC 5 ms 832 KB
subtask1_22.txt AC 5 ms 832 KB
subtask1_23.txt AC 5 ms 832 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