#abc341f. F - Breakdown

F - Breakdown

Score: 475475 points

问题描述

你被给予一个由 NN 个顶点和 MM 条边构成的简单无向图。对于 i=1,2,,Mi = 1, 2, \ldots, M,第 ii 条边连接顶点 uiu_iviv_i。同时,对于 i=1,2,,Ni = 1, 2, \ldots, N,顶点 ii 被赋予了一个正整数 WiW_i,并在其上放置了 AiA_i 个棋子。

只要图上还有棋子,就重复以下操作:

  • 首先,选择并移除图上的一个棋子,并令 xx 为该棋子所在的顶点。
  • 然后选择一个(可能为空)与顶点 xx 相邻的顶点集合 SS,满足 ySWy<Wx\sum_{y \in S} W_y < W_x,并在 SS 中每个顶点上各放置一个棋子。

输出能够进行上述操作的最大次数。

可以证明,无论怎样执行操作,在有限次迭代后,图上将不再有棋子。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a simple undirected graph consisting of NN vertices and MM edges. For i=1,2,,Mi = 1, 2, \ldots, M, the ii-th edge connects vertices uiu_i and viv_i. Also, for i=1,2,,Ni = 1, 2, \ldots, N, vertex ii is assigned a positive integer WiW_i, and there are AiA_i pieces placed on it.

As long as there are pieces on the graph, repeat the following operation:

  • First, choose and remove one piece from the graph, and let xx be the vertex on which the piece was placed.
  • Choose a (possibly empty) set SS of vertices adjacent to xx such that ySWy<Wx\sum_{y \in S} W_y \lt W_x, and place one piece on each vertex in SS.

Print the maximum number of times the operation can be performed.

It can be proved that, regardless of how the operation is performed, there will be no pieces on the graph after a finite number of iterations.

Constraints

  • All input values are integers.
  • 2N50002 \leq N \leq 5000
  • 1Mmin{N(N1)/2,5000}1 \leq M \leq \min \lbrace N(N-1)/2, 5000 \rbrace
  • 1ui,viN1 \leq u_i, v_i \leq N
  • uiviu_i \neq v_i
  • $i \neq j \implies \lbrace u_i, v_i \rbrace \neq \lbrace u_j, v_j \rbrace$
  • 1Wi50001 \leq W_i \leq 5000
  • 0Ai1090 \leq A_i \leq 10^9

Input

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

NN MM

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uMu_M vMv_M

W1W_1 W2W_2 \ldots WNW_N

A1A_1 A2A_2 \ldots ANA_N

Output

Print the answer.

Sample Input 1

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

Sample Output 1

5

In the following explanation, let A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) represent the numbers of pieces on the vertices. Initially, A=(1,0,0,0,0,1)A = (1, 0, 0, 0, 0, 1).

Consider performing the operation as follows:

  • Remove one piece from vertex 11 and place one piece each on vertices 22 and 33. Now, A=(0,1,1,0,0,1)A = (0, 1, 1, 0, 0, 1).
  • Remove one piece from vertex 22. Now, A=(0,0,1,0,0,1)A = (0, 0, 1, 0, 0, 1).
  • Remove one piece from vertex 66. Now, A=(0,0,1,0,0,0)A = (0, 0, 1, 0, 0, 0).
  • Remove one piece from vertex 33 and place one piece on vertex 22. Now, A=(0,1,0,0,0,0)A = (0, 1, 0, 0, 0, 0).
  • Remove one piece from vertex 22. Now, A=(0,0,0,0,0,0)A = (0, 0, 0, 0, 0, 0).

In this procedure, the operation is performed five times, which is the maximum possible number of times.

Sample Input 2

2 1
1 2
1 2
0 0

Sample Output 2

0

In this sample input, there are no pieces on the graph from the beginning.

Sample Input 3

10 20
4 8
1 10
1 7
5 9
9 10
8 10
7 5
1 4
7 3
8 7
2 8
5 8
4 2
5 1
7 2
8 3
3 4
8 9
7 10
2 3
25 5 1 1 16 5 98 3 21 1
35 39 32 11 35 37 14 29 36 1

Sample Output 3

1380

update @ 2024/3/10 01:35:22