Under construction

Still being built. Things may change

Crazy Circuits

Time limit1sMemory limit128 MB

Summary
Given a directed acyclic circuit with current demands on edges, find the minimum supply current at the + terminal so every component gets its required current, or report impossible.
Level

Medium7 of 10

Topics
Graph, Dynamic programming, Greedy, Topological sort
Solved
No attempts yet

Problem

You have just built a circuit board for your new robot, and now you need to power it. Your robot circuit consists of a number of electrical components, each of which requires a certain amount of current to operate. Every component has a + lead and a - lead, which are connected to each other at junctions on the circuit board. Current flows through a component from + to - (but note that a component does not "use up" the current: everything that comes in through the + end goes out through the - end).

The junctions on the board are labeled 1,…,N1, \dots, N, plus two special junctions labeled + and - where the power supply terminals are connected. The + terminal connects only + leads, and the - terminal connects only - leads. At every junction, all the current that enters from the - leads of the connected components leaves through the connected + leads, and you are able to control how much current flows to each connected + lead (the methods for doing so are beyond the scope of this problem). Moreover, you have assembled the circuit so that there are no feedback loops (components chained in a way that would let current flow around a loop).

Figure 1: Two examples of valid circuit diagrams. In (a), every component can be powered along a directed path from the positive terminal to the negative terminal. In (b), components 4 and 6 cannot be powered, because there is no directed path from junction 4 to the negative terminal.

To save power, and also to keep your circuit from overheating, you would like to use as little current as possible to make your robot work. What is the smallest amount of current you need to send through the + terminal (all of which you may imagine necessarily leaving through the - terminal) so that every component on your robot receives its required supply of current?

Input

The input contains multiple test cases. Each test case begins with a single line containing two integers NN (0≤N≤500 \le N \le 50), the number of junctions not counting the positive and negative terminals, and MM (1≤M≤2001 \le M \le 200), the number of components in the circuit diagram. Each of the next MM lines describes one component. The ii-th component description contains three fields: pip_i, the positive junction the component is connected to; nin_i, the negative junction the component is connected to; and an integer IiI_i (1≤Ii≤1001 \le I_i \le 100), the minimum amount of current component ii needs to function. Each of pip_i and nin_i is given either as the character + (the positive terminal), the character - (the negative terminal), or an integer between 11 and NN (one of the numbered junctions). No two components share the same positive junction and the same negative junction at once. The end of input is marked by an invalid test case with N=M=0N = M = 0, which should not be processed.

Output

For each test case, print a single integer: the minimum amount of current that must be supplied at the positive terminal so that every component is powered. If there is no way to direct a sufficient amount of current to every component simultaneously, print impossible instead.

Examples3

  1. Example 1

    Input
    6 10
    + 1 1
    1 2 1
    1 3 2
    2 4 5
    + - 1
    4 3 2
    3 5 5
    4 6 2
    5 - 1
    6 5 3
    4 6
    + 1 8
    1 2 4
    1 3 5
    2 4 6
    3 - 1
    3 4 3
    0 0
    
    Expected output
    9
    impossible
    
  2. Example 2

    Input
    0 1
    + - 5
    0 0
    
    Expected output
    5
    
  3. Example 3

    Input
    1 2
    + 1 3
    1 - 2
    0 0
    
    Expected output
    3