Gold Bandits
Time limit5sMemory limit256 MB
The bandits rob the richest set of villages along a shortest road from home to the castle that still leaves a return route avoiding robbed villages.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
A valley in a distant land holds many villages. A cruel king rules them all and demands that every village pay him tribute in gold each year. When the king demands it, a village must carry the gold to his castle as quickly as it can.
One village in the kingdom is a village of bandits. The bandits saved no gold for the king, because they spent every bit of gold they had. They are bandits, though. On the way to the castle they can rob any village they pass through and hand that gold to the king as their own. For each village on the way they choose whether to rob it.
The bandits pass through as few villages as possible on the way to the castle, so their route to the castle uses the fewest roads. They have one more concern. After delivering the gold they must be able to get home, and they consider it unsafe to return through a village they robbed. They do not care how long the way home is, and they may travel the same road more than once.
Find the largest total amount of gold the bandits can rob on the way to the king's castle while still being able to get home safely.
Input
The input holds several test cases. Each test case begins with a line holding two integers and (, ), where is the number of villages and is the number of roads. The villages are numbered through . Village is the bandits' home and village holds the king's castle.
The next line holds space separated integers (), the amount of gold in villages in that order. The bandits' home and the king's castle are left out of this list and hold no gold.
Each of the next lines holds two integers and (), meaning that a road connects village and village . Every road is two way. The pairs are all different. Every village is reachable from every other village, directly or through other villages.
The input ends with a line holding two zeros.
Output
For each test case, print one integer on its own line: the largest total amount of gold the bandits can rob and still get home safely. Print no blank lines between the answers.