#abc371c. C - Make Isomorphic

C - Make Isomorphic

Score : 300300 points

问题陈述

你得到了两个简单的无向图 GGHH,每个图都有 NN 个顶点:顶点 11, 22, \ldots, NN。图 GGMGM_G 条边,它的第 ii 条边 (1iMG)(1\leq i\leq M_G) 连接顶点 uiu_iviv_i。图 HHMHM_H 条边,它的第 ii 条边 (1iMH)(1\leq i\leq M_H) 连接顶点 aia_ibib_i

你可以对图 HH 执行以下操作任意次数,可能为零次。

  • 选择一对整数 (i,j)(i,j) 满足 1i<jN1\leq i<j\leq N。支付 Ai,jA_{i,j} 日元,并且如果顶点 iijjHH 中没有边,则添加一条;如果有,则移除它。

找出使 GGHH 同构所需的最小总成本。

什么是简单的无向图?

简单无向图 是一个没有自环或多边的图,其中边没有方向。

图同构是什么意思?

如果存在一个排列 (P1,P2,,PN)(P_1,P_2,\ldots,P_N)(1,2,,N)(1,2,\ldots,N),使得对于所有 1i<jN1\leq i\lt j\leq N

  • 如果图 GG 中顶点 iijj 之间存在边,则图 HH 中顶点 PiP_iPjP_j 之间也存在边。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given simple undirected graphs GG and HH, each with NN vertices: vertices 11, 22, \ldots, NN. Graph GG has MGM_G edges, and its ii-th edge (1iMG)(1\leq i\leq M_G) connects vertices uiu_i and viv_i. Graph HH has MHM_H edges, and its ii-th edge (1iMH)(1\leq i\leq M_H) connects vertices aia_i and bib_i.

You can perform the following operation on graph HH any number of times, possibly zero.

  • Choose a pair of integers (i,j)(i,j) satisfying 1i<jN1\leq i<j\leq N. Pay Ai,jA_{i,j} yen, and if there is no edge between vertices ii and jj in HH, add one; if there is, remove it.

Find the minimum total cost required to make GG and HH isomorphic.

What is a simple undirected graph?

A simple undirected graph is a graph without self-loops or multi-edges, where edges have no direction.

What does it mean for graphs to be isomorphic?

Two graphs GG and HH with NN vertices are isomorphic if and only if there exists a permutation (P1,P2,,PN)(P_1,P_2,\ldots,P_N) of (1,2,,N)(1,2,\ldots,N) such that for all 1i<jN1\leq i\lt j\leq N:

  • an edge exists between vertices ii and jj in GG if and only if an edge exists between vertices PiP_i and PjP_j in HH.

Constraints

  • 1N81\leq N\leq8
  • 0MGN(N1)20\leq M _ G\leq\dfrac{N(N-1)}2
  • 0MHN(N1)20\leq M _ H\leq\dfrac{N(N-1)}2
  • 1ui<viN (1iMG)1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M _ G)
  • $(u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M _ G)$
  • 1ai<biN (1iMH)1\leq a _ i\lt b _ i\leq N\ (1\leq i\leq M _ H)
  • $(a _ i,b _ i)\neq(a _ j,b _ j)\ (1\leq i\lt j\leq M _ H)$
  • 1Ai,j106 (1i<jN)1\leq A _ {i,j}\leq 10 ^ 6\ (1\leq i\lt j\leq N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

MGM _ G

u1u _ 1 v1v _ 1

u2u _ 2 v2v _ 2

\vdots

uMGu _ {M _ G} vMGv _ {M _ G}

MHM _ H

a1a _ 1 b1b _ 1

a2a _ 2 b2b _ 2

\vdots

aMHa _ {M _ H} bMHb _ {M _ H}

A1,2A _ {1,2} A1,3A _ {1,3} \ldots A1,NA _ {1,N}

A2,3A _ {2,3} \ldots A2,NA _ {2,N}

\vdots

AN1,NA _ {N-1,N}

Output

Print the answer.

Sample Input 1

5
4
1 2
2 3
3 4
4 5
4
1 2
1 3
1 4
1 5
3 1 4 1
5 9 2
6 5
3

Sample Output 1

9

The given graphs are as follows:

For example, you can perform the following four operations on HH to make it isomorphic to GG at a cost of 99 yen.

  • Choose (i,j)=(1,3)(i,j)=(1,3). There is an edge between vertices 11 and 33 in HH, so pay 11 yen to remove it.
  • Choose (i,j)=(2,5)(i,j)=(2,5). There is no edge between vertices 22 and 55 in HH, so pay 22 yen to add it.
  • Choose (i,j)=(1,5)(i,j)=(1,5). There is an edge between vertices 11 and 55 in HH, so pay 11 yen to remove it.
  • Choose (i,j)=(3,5)(i,j)=(3,5). There is no edge between vertices 33 and 55 in HH, so pay 55 yen to add it.

After these operations, HH becomes:

You cannot make GG and HH isomorphic at a cost less than 99 yen, so print 9.

Sample Input 2

5
3
1 2
2 3
3 4
4
1 2
2 3
3 4
4 5
9 1 1 1
1 1 1
1 1
9

Sample Output 2

3

For example, performing the operations (i,j)=(2,3),(2,4),(3,4)(i,j)=(2,3),(2,4),(3,4) on HH will make it isomorphic to GG.

Sample Input 3

5
3
1 2
2 3
3 4
4
1 2
2 3
3 4
4 5
5 4 4 4
4 4 4
4 4
5

Sample Output 3

5

For example, performing the operation (i,j)=(4,5)(i,j)=(4,5) once will make GG and HH isomorphic.

Sample Input 4

2
0
0
371

Sample Output 4

0

Note that GG and HH may have no edges.

Also, it is possible that no operations are needed.

Sample Input 5

8
13
1 8
5 7
4 6
1 5
7 8
1 6
1 2
5 8
2 6
5 6
6 7
3 7
4 8
15
3 5
1 7
4 6
3 8
7 8
1 2
5 6
1 6
1 5
1 4
2 8
2 6
2 4
4 7
1 3
7483 1694 5868 3296 9723 5299 4326
5195 4088 5871 1384 2491 6562
1149 6326 2996 9845 7557
4041 7720 1554 5060
8329 8541 3530
4652 3874
3748

Sample Output 5

21214