Language: C++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;
#define N 1000000001
#define inf 0x3f3f3f3f
#define M 10001
vector<pair<int, int> > graph;
int wei_x[M];
int wei_y[M];
int weight[N];
int visit[N];
int n, m;
int dir[8] = {1, 0, 0, 1, -1, 0, 0, -1};


bool is_ok(int x, int y)
{
	if (x >= 0 && x < n && y >= 0 && y < m && visit[x * n + y] == 0)
		return true;
	return false;
}
void update(int x, int y)
{
	if (is_ok(x - 1, y + 0))
	{
		if (weight[x * n + y] + wei_x[x - 1] < weight[(x - 1) * n + y + 0])
		{
			weight[(x - 1) * n + y + 0] = weight[x * n + y] + wei_x[x - 1];
		}
	}
	if (is_ok(x + 1, y + 0))
	{
		if (weight[x * n + y] + wei_x[x] < weight[(x + 1) * n + y + 0])
		{
			weight[(x + 1) * n + y + 0] = weight[x * n + y] + wei_x[x];
		}
	}
	if (is_ok(x + 0, y - 1))
	{
		if (weight[x * n + y] + wei_y[y - 1] < weight[(x + 0) * n + y - 1])
		{
			weight[(x + 0) * n + y - 1] = weight[x * n + y] + wei_y[y - 1];
		}
	}
	if (is_ok(x + 0, y + 1))
	{
		if (weight[x * n + y] + wei_y[y] < weight[(x + 0) * n + y + 1])
		{
			weight[(x + 0) * n + y + 1] = weight[x * n + y] + wei_y[y];
		}
	}
}

int choose(int &x, int &y)
{
	int min_v = inf;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (min_v > weight[i *n + j] && visit[i * n + j] == 0)
			{
				min_v = weight[i * n + j];
				x = i;
				y = j;
			}
		}
	}
	return min_v;
}
int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
		scanf("%d", &wei_x[i]);
	for (int j = 0; j < m; j++)
		scanf("%d", &wei_y[j]);
	n++; m++;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			weight[i * n + j] = inf;
		}
	}
	int vx = 0, vy = 0;
	int pre_x = vx;
	int pre_y = vy;
	weight[vx *n + vy] = 0;
	visit[vx * n + vy] = 1;
	update(vx, vy);
	int k = 1;
	int min_cos = 0;
	while (k < n * m)
	{
		int x, y;
		choose(x, y);
		if (pre_x + 1 == x)
		{
			min_cos += wei_x[pre_x];
		}
		else if (pre_x = x + 1)
		{
			min_cos += wei_x[x];
		}
		else if (pre_y + 1 == y)
		{
			min_cos = wei_y[pre_y];
		}
		else if (y + 1 == pre_y)
		{
			min_cos = wei_y[y];
		}
		pre_x = x;
		pre_y = y;
		visit[x * n + y] = 1;
		update(x, y);
		k++;
	}
	cout << min_cos << endl;
	return 0;
}

Compiler output:
/tmp/ccD0qqgR.o: in function `is_ok(int, int)':
a.cc:(.text+0x12): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccD0qqgR.o
a.cc:(.text+0x23): relocation truncated to fit: R_X86_64_PC32 against symbol `m' defined in .bss section in /tmp/ccD0qqgR.o
a.cc:(.text+0x2e): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccD0qqgR.o
a.cc:(.text+0x4a): relocation truncated to fit: R_X86_64_PC32 against symbol `visit' defined in .bss section in /tmp/ccD0qqgR.o
/tmp/ccD0qqgR.o: in function `update(int, int)':
a.cc:(.text+0x8d): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccD0qqgR.o
a.cc:(.text+0xd5): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccD0qqgR.o
a.cc:(.text+0xfb): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccD0qqgR.o
a.cc:(.text+0x140): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccD0qqgR.o
a.cc:(.text+0x17e): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccD0qqgR.o
a.cc:(.text+0x1c3): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccD0qqgR.o
a.cc:(.text+0x1e9): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status