topcoder SRM 492 div2 level3
2012-05-25 17:47
316 查看
A traveling salesman wants to survey N cities for business opportunities. The cities are numbered 0 throughN-1, and he wishes to visit each of them at least once. He starts at city 0 and can end at any city. There are several bidirectional roads, each connecting two different cities and costing some amount of money to traverse. The traveling salesman also has a time machine. He can use the time machine to go back in time, without affecting which cities he is considered to have already visited. For example, suppose he has visited cities A, B, C, D, and E, in that order, and is currently in city E. He can use the time machine to go back to city A, B, C, or D. Suppose he chooses to go back to city C. At that point, he can then go back further to city A or B, but he cannot use the time machine to go forward to city D or E. Note that going back in time will not change the fact that he is considered to have already visited cities A, B, C, D, and E. You are given the roads in the String[] roads. Concatenate the elements ofroads, in order, to get a single space-separated list of roads. Each road will be formatted "a,b,travelcost" (quotes for clarity), which means that the bidirectional road connects city a and city b, and costs travelcost to traverse. He can use the time machine any number of times, and it costs nothing to use it. Return the minimum cost required to visit all the cities. Return -1 if it is impossible to visit all the cities. | ||||||||||||
Definition | ||||||||||||
| ||||||||||||
Constraints | ||||||||||||
- | N will be between 2 and 1000, inclusive. | |||||||||||
- | roads will contain between 1 and 50 elements, inclusive. | |||||||||||
- | Each element of roads will contain between 1 and 50 characters, inclusive. | |||||||||||
- | Each character in roads will be '0'-'9', ',' or ' '. | |||||||||||
- | roads will be formatted as described in the problem statement without leading or trailing spaces. | |||||||||||
- | All integers in the concatenation of all the elements of roads in the order they are given will have no extra leading zeroes. | |||||||||||
- | In each road, a and b as described in the problem statement will be different and will each be between 0 andN-1, inclusive. | |||||||||||
- | In each road, travelcost as described in the problem statement will be between 1 and 10,000,000, inclusive. | |||||||||||
- | For each two different cities, there will be at most one road connecting them. | |||||||||||
Examples | ||||||||||||
0) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
|
仔细一想,其实就是最小生成树
因为每个点都遍历一次,且每条边只遍历一次。
prim或kruskal即可。
相关文章推荐
- TopCoder SRM 569 DIV2 Level3: MegaFactorialDiv2
- topcoder SRM 500 div2 level3
- topcoder SRM 623 DIV2 CatAndRat
- TopCoder 300 points 26-SRM 156 DIV 2 78.06/300 26.02%
- TopCoder SRM 543 DIV2
- TopCoder SRM 654 Div2 Problem 500 - OneEntrance (思维)
- topcoder srm 325 div1
- topcoder SRM 593 DIV2 WolfDelaymaster
- Topcoder SRM 709 DIV 2 250pt Robofactory solution
- [TopCoder] SRM 578 DIV 2, Goose In Zoo, Solution
- TopCoder SRM 595 Div2 第3题
- TopCoder SRM 660 Div2 Problem 500 - PrivateD2party (贪心)
- TopCoder SRM 144 DIV2(550-point)
- TopCoder SRM 144 DIV 2
- TopCoder SRM 663 Div2 Problem 500 - ABBA (思维)
- topcoder srm 455 div1
- topcoder srm 451 div2 level 3 PizzaDelivery
- topcoder srm 663 div1 -23
- topcoder srm 686 div1 -3
- topcoder SRM 639 div2