O - I Wanna Go Home

查看题解 查看答案
题目描述
Time Limit: 1000 ms
Memory Limit: 256 mb

    The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.     "For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."     Would you please tell Mr. M at least how long will it take to reach his sweet home?

翻译:这个国家正面临着一场可怕的内战——该国的城市被分为两部分,分别支持不同的领导人。作为一名商人,M先生并不关心政治,但他实际上了解当前的严峻形势,而你的任务就是帮助他尽快回到家。M先生说:“为了安全起见,你的路线最多只能包含一条连接不同阵营城市的道路。”请问,你能告诉M先生至少需要多长时间才能回到他温馨的家吗?

输入输出格式
输入描述:
The input contains multiple test cases. The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country. The second line contains one integer M (0<=M<=10000), which is the number of roads. The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500]. Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i. To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2. Note that all roads are bidirectional and may have multiple edges. Input is ended with a case of N=0. 翻译:输入包含多个测试用例。 每个案例的第一行是一个整数N(2<=N<=600),表示该国城市的数量。 第二行包含一个整数M(0<=M<=10000),表示道路的数量。 接下来的M行是道路信息。每行包含三个整数A、B和T,表示A城市和B城市之间的道路需要花费时间T。T的范围为[1,500]。 接下来包含N个整数,这些整数要么是1,要么是2。第i个整数表示城市i的辅助领导者。 为了简化问题,我们假设M先生从城市1出发,他的目标是城市2。城市1始终支持领导者1,而城市2则与领导者2站在同一边。 请注意,所有道路都是双向的,且可能有多条边。 输入以N=0的情况结束。
输出描述:
For each test case, output one integer representing the minimum time to reach home. If it is impossible to reach home according to Mr. M's demands, output -1 instead. 翻译:对于每个测试用例,输出一个整数,表示到达家的最短时间。 如果根据M先生的要求无法回到家,则输出-1。
输入输出样例
输入样例#:
2
1
1 2 100
1 2
3
3
1 2 100
1 3 40
2 3 50
1 2 1
5
5
3 1 200
5 3 150
2 5 160
4 3 170
4 2 170
1 2 2 2 1
0
输出样例#:
复制
100
90
540

提交代码后在此处可查看状态