Diamond Dealer

Interview

Time limit1sMemory limit128 MB

Summary
For each simple polygon given clockwise, count concave vertices (dents) and edges not touching any dent, then print max(-a*p + b*q, 0).
Level

Medium4 of 10

Topics
Geometry, Implementation, Math, Brute force
Solved
No attempts yet

Problem

Mr. Chou is a diamond dealer in a two-dimensional world. To be a successful businessman, he must know the exact value of the (two-dimensional) diamonds he trades. Mr. Chou is tired of computing these values by hand, so you must write a program that does it for him.

The value of a diamond is determined by the smoothness of its surface. Smoothness depends on the number of faces on the surface: the more faces, the smoother the surface. If the surface has dents (shown in red in the figure), the value of the diamond decreases.

A diamond is given as a simple polygon defined by its vertices. Each concave vertex of the polygon (a vertex whose interior angle is greater than 180 degrees) is a dent, and each edge of the polygon is a face. A face is considered to be in a dent if at least one of its two endpoints is a concave vertex.

Let aa be the number of dents and let bb be the number of faces that are not in any dent. The value vv of the diamond is

v=−a⋅p+b⋅q,v = -a \cdot p + b \cdot q,

where pp is the penalty for a dent and qq is the value of a face. If vv is negative, the diamond is worthless and its value is 00.

Input

The first line contains the number of test cases TT.

Each test case consists of:

  • One line with three integers pp, qq, and mm: the penalty for a dent (0≤p≤1000 \le p \le 100), the value of a face (0≤q≤1000 \le q \le 100), and the number of vertices describing the diamond (3≤m≤303 \le m \le 30).
  • mm lines, each with two integers xix_i and yiy_i (−1000≤xi,yi≤1000-1000 \le x_i, y_i \le 1000), giving the vertices of the polygon in clockwise order: (x0,y0)−(x1,y1)−⋯−(xm−1,ym−1)−(x0,y0)(x_0, y_0) - (x_1, y_1) - \cdots - (x_{m-1}, y_{m-1}) - (x_0, y_0).

No three vertices of a single diamond are collinear.

Output

For each test case, print a single line containing one integer: the value of the diamond.

Examples2

  1. Example 1

    Input
    1
    10 5 7
    0 10
    8 4
    10 -7
    6 -9
    -5 -4
    -5 7
    -2 6
    
    Expected output
    15
    
  2. Example 2

    Input
    1
    7 4 3
    0 0
    0 10
    10 0
    
    Expected output
    12