luogu#P5234. [JSOI2012] 越狱老虎桥

[JSOI2012] 越狱老虎桥

Background

This is beautiful Nanjing. This is the graceful Jinxiang River. This is the cozy Laohuqiao.

If we say the beauty of the Jinxiang River lies in its pleasant scenery, it is better to say it lies in the relaxed, old-classical-alley style of life in Nanjing. If we say the Jinxiang River is charming because of its simple folk customs, it is better to say it is the secrets buried by history that attract people’s curiosity.

Perhaps many people still remember Laohuqiao Prison, the largest prison in Jiangnan during the Beiyang period. Over nearly a century, through the rise and fall of several eras such as the Late Qing, Beiyang, the Republic of China, and New China, its name changed again and again, showing all its vicissitudes.

People today may find it hard to believe how many thrilling events once took place right here.

Problem Description

It was the winter of 19481948. A small team from an underground organization in Nanjing decided to raid Laohuqiao Prison to rescue several hundred trapped people. At that time, Laohuqiao Prison was surrounded by NN layers of power grids, numbered from inside to outside as 1,2,,N1, 2, \dots, N. The 11st layer of the power grid was connected to high-voltage electricity. There were MM high-voltage wires connecting all the grids. The ii-th wire connected the aia_i-th and bib_i-th layers, and to destroy the ii-th wire, at least TiT_i agents were needed. Facing so many layers of grids, the raiding team was troubled. They must destroy at least one layer of the power grid, otherwise the raid cannot succeed.

However, a cunning spy learned about this. To sabotage the raid plan, the enemy secretly added one more high-voltage wire without letting the team members discover it.

To ensure success, no matter which two layers this newly added secret high-voltage wire connects, the team must destroy exactly one high-voltage wire so that at least one layer of the power grid becomes unpowered. Note that for the newly added wire, we do not know how many agents are needed to destroy it successfully. The question is: what is the minimum number of agents the team must have?

The decisive battle is tonight!

Input Format

The first line contains 22 integers, NN and MM, representing the number of grid layers and the number of high-voltage wires.

The next MM lines each contain 33 integers: aia_i, bib_i, and TiT_i.

Output Format

Output only one line containing one integer, which is the minimum number of agents needed.

If the plan is bound to fail, output 1-1.

3 2
1 2 1
2 3 2
-1
4 3
1 2 1
1 3 2
1 4 3
3

Hint

For 30%30\% of the testdata, N200N \leq 200, M250M \leq 250.

For 70%70\% of the testdata, N50000N \leq 50000, M100000M \leq 100000.

For 100%100\% of the testdata, N500000N \leq 500000, M1000000M \leq 1000000, T100000T \leq 100000.

For the second sample, the newly added high-voltage wire can only appear between 22 and 33, between 22 and 44, or between 33 and 44.

If it appears between 22 and 33, then the only wire that can be destroyed is the one between 11 and 44. If it appears between 22 and 44, then the only wire that can be destroyed is the one between 11 and 33. If it appears between 33 and 44, then the only wire that can be destroyed is the one between 11 and 22.

Therefore, at least 33 agents are needed to handle all possible cases.

Translated by ChatGPT 5