Description
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?
Input
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 there is at most 1 road between two cities.
Input is ended with a case of N=0.
Output
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.
Sample Input
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
Sample Output
100 90 540
#include <iostream>
#include <CSTDLIB>
using namespace std;
#define MAXNUM 800
#define INF 999999999
int map[MAXNUM][MAXNUM];
int team[MAXNUM];
int result[MAXNUM];
bool flag[MAXNUM];
void shortest_way(int start, int end) // Dijkstra算法
{
int max_val = INT_MAX;
int next_one = 0, add_val;
int ic, is, it;
for (ic = start; ic <= end; ic++)
result[ic] = map[1][ic];
result[start] = 0;
flag[start] = true;
for (ic = start; ic <= end; ++ic)
{
max_val = INT_MAX;
for (is = start; is <= end; ++is)
{
if (!flag[is] && result[is] < max_val)
{
max_val = result[is];
add_val = result[is];
next_one = is;
}
}
// if (next_one==2) break;
flag[next_one] = true;
for (it = start; it <= end; ++it)
{
if (!flag[it] && (map[next_one][it] + add_val) < result[it])
{
result[it] = map[next_one][it] + add_val;
}
}
}
if (result[2] < INF)
cout << result[2] << endl;
else
cout << -1 << endl;
}
int main()
{
int N, ic, is, roads;
int i, j, val;
while (cin >> N)
{
if (!N) break;
for (ic = 1; ic <= N; ++ic)
for (is = 1; is <= N; ++is)
map[ic][is] = INF;
memset(team, 0, sizeof(team));
memset(flag, false, sizeof(flag));
cin >> roads;
for (ic = 1; ic <= roads; ic++)
{
cin >> i >> j >> val;
map[i][j] = map[j][i] = val;
}
for (ic = 1; ic <= N; ic++)
{
cin >> team[ic];
}
for (i = 1; i <= N; ++i) // 不同support leader的city的不能往返
for (j = 1; j <= N; ++j)
{
if (team[i] != team[j])
{
if (team[i] == 2 && team[j] == 1)
map[i][j] = INF;
else if (team[i] == 1 && team[j] == 2)
map[j][i] = INF;
}
}
shortest_way(1, N);
}
return 0;
}