Language: C++

#include <stdio.h>
#include <stdlib.h>

#define MAXNODE 100000*100000
#define MAXEDGE MAXNODE

#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))


typedef struct{
   int cost;
   int node[2];
} Edge;


typedef struct{
   int x,y;
} Node;


typedef struct unode{
   int rank;
   struct unode *p;
} UNode;


int makeEdge(Edge *edge,Node *node,int *p,int *q,int n){
   int i,j,k;

   k=0;
   for(i=0;i<n;++i){
      for(j=0;j<n;++j){
         if((node[i].y==node[j].y)&&(node[j].x-node[i].x==1)){
            edge[k].cost=p[node[i].x];
            edge[k].node[0]=i;
            edge[k].node[1]=j;
            ++k;
         }else if((node[i].x==node[j].x)&&(node[j].y-node[i].y==1)){
            edge[k].cost=q[node[i].x];
            edge[k].node[0]=i;
            edge[k].node[1]=j;
            ++k;
         }
      }
   }

   return k;
}


int cmp(const void *a,const void *b){
   Edge *A=(Edge*)a;
   Edge *B=(Edge*)b;
   return ((A->cost)>(B->cost))?1:-1;
}


void set(UNode *un){
   un->p=un;
   un->rank=0;

   return;
}


UNode *find(UNode *un){
   if(un!=(un->p)){
      un->p=find(un->p);
   }

   return un->p;
}


int same(UNode *x,UNode *y){
   return find(x)==find(y);
}


void link(UNode *x,UNode *y){
   if((x->rank)>(y->rank)){
      y->p=x;
   }else{
      x->p=y;
      if((x->rank)==(y->rank)){
         ++(y->rank);
      }
   }

   return;
}


void unite(UNode *x,UNode *y){
   link(find(x),find(y));

   return;
}


int kruskal(Edge *edge,int *used,int n,int m){
   int i,j;
   int r;
   UNode un[MAXNODE];

   for(i=0;i<n;++i){
      set(un+i);
   }

   r=-1;
   j=0;
   for(i=0;i<m;++i){
      if(same(un+edge[i].node[0],un+edge[i].node[1])){
         continue;
      }

      unite(un+edge[i].node[0],un+edge[i].node[1]);
      used[j++]=i;
      if((((un+edge[i].node[0])->p)->rank)==n-1){
         r=0;
         break;
      }
   }

   return r;
}


int main(){
   int w,h;
   int p[100000],q[100000];

   Node node[MAXNODE];
   int n;
   int i,j,k;
   int m;
   Edge edge[MAXEDGE];
   int used[MAXNODE-1];
   int ans;

   scanf("%d %d",&w,&h);
   for(i=0;i<w;++i){
      scanf("%d",p+i);
   }
   for(i=0;i<h;++i){
      scanf("%d",q+i);
   }

   k=0;
   for(i=0;i<w;++i){
      for(j=0;j<h;++j){
         node[k].x=i;
         node[k].y=j;
         ++k;
      }
   }

   n=w*h;
   m=makeEdge(edge,node,p,q,w*h);
   qsort(edge,m,sizeof(Edge),cmp);
   kruskal(edge,used,n,m);

   ans=0;
   for(i=0;i<n-1;++i){
      ans+=edge[used[i]].cost;
   }

   printf("%d\n",ans);

   return 0;
}


Compiler output:
a.cc: In function 'int kruskal(Edge*, int*, int, int)':
a.cc:4:23: warning: integer overflow in expression of type 'int' results in '1410065408' [-Woverflow]
    4 | #define MAXNODE 100000*100000
      |                 ~~~~~~^~~~~~~
a.cc:105:13: note: in expansion of macro 'MAXNODE'
  105 |    UNode un[MAXNODE];
      |             ^~~~~~~
a.cc:4:23: error: size of array 'un' exceeds maximum object size '9223372036854775807'
    4 | #define MAXNODE 100000*100000
      |                 ~~~~~~^~~~~~~
a.cc:105:13: note: in expansion of macro 'MAXNODE'
  105 |    UNode un[MAXNODE];
      |             ^~~~~~~
a.cc: In function 'int main()':
a.cc:4:23: warning: integer overflow in expression of type 'int' results in '1410065408' [-Woverflow]
    4 | #define MAXNODE 100000*100000
      |                 ~~~~~~^~~~~~~
a.cc:134:14: note: in expansion of macro 'MAXNODE'
  134 |    Node node[MAXNODE];
      |              ^~~~~~~
a.cc:4:23: error: size of array 'node' exceeds maximum object size '9223372036854775807'
    4 | #define MAXNODE 100000*100000
      |                 ~~~~~~^~~~~~~
a.cc:134:14: note: in expansion of macro 'MAXNODE'
  134 |    Node node[MAXNODE];
      |              ^~~~~~~
a.cc:4:23: warning: integer overflow in expression of type 'int' results in '1410065408' [-Woverflow]
    4 | #define MAXNODE 100000*100000
      |                 ~~~~~~^~~~~~~
a.cc:5:17: note: in expansion of macro 'MAXNODE'
    5 | #define MAXEDGE MAXNODE
      |                 ^~~~~~~
a.cc:138:14: note: in expansion of macro 'MAXEDGE'
  138 |    Edge edge[MAXEDGE];
      |              ^~~~~~~
a.cc:4:23: error: size of array 'edge' exceeds maximum object size '9223372036854775807'
    4 | #define MAXNODE 100000*100000
      |                 ~~~~~~^~~~~~~
a.cc:5:17: note: in expansion of macro 'MAXNODE'
    5 | #define MAXEDGE MAXNODE
      |                 ^~~~~~~
a.cc:138:14: note: in expansion of macro 'MAXEDGE'
  138 |    Edge edge[MAXEDGE];
      |              ^~~~~~~
a.cc:4:23: warning: integer overflow in expression of type 'int' results in '1410065408' [-Woverflow]
    4 | #define MAXNODE 100000*100000
      |                 ~~~~~~^~~~~~~
a.cc:139:13: note: in expansion of macro 'MAXNODE'
  139 |    int used[MAXNODE-1];
      |             ^~~~~~~
a.cc:139:20: error: size of array 'used' exceeds maximum object size '9223372036854775807'
  139 |    int used[MAXNODE-1];
      |                    ^