- 总时间限制:
- 1000ms 内存限制:
- 65536kB
- 描述
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?
输入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 integersA, B and T, which means the road between city A and cityB 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 cityi.
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.
输出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.
样例输入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
题解:就是找到回家的最短路,从城市1->城市2,但是有个条件,就是每个点有值1或2,为了安全,值只能变一次,也就是说从支持1的城市到支持2的城市后,不能再返回支持1的城市,也就是说从值为1->值为2这是个单向的,那么只要在基本赋值完成后,弄成单向图就行啦,我把从值为2的城市到值为1的城市的边置为INF,就不能走这条路,剩余的就是单源最短路径Dijksra了
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x9f9f9f;
int n,m;
int maze[700][700];
int sup[10010];
int vis[700];
int dis[700];
void dijksra(int start)
{
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
dis[1] = 0;
for(int i = 2; i <= n; i++)
dis[i] = maze[1][i];
vis[start] = 1;
for(int i = 1; i <= n; i++){
int tmp = INF;
int k;
for(int j = 1; j <= n; j++){
if (vis[j] == 0 && dis[j] < tmp){
tmp = dis[j];
k = j;
}
}
vis[k] = 1;
for(int j = 1; j <= n; j++){
if (vis[j] == 0 && dis[j] > dis[k]+maze[k][j])
dis[j] = dis[k]+maze[k][j];
}
}
}
int main()
{
while(~scanf("%d",&n)){
if (n == 0)
break;
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
if (i == j)
maze[i][j] = 0;
else maze[i][j] = INF;
memset(sup,0,sizeof(sup));
cin >> m;
for(int i = 0; i < m; i++){
int s,e,v;
cin >> s >> e >> v;
maze[s][e] = maze[e][s] = v;
}
for(int i = 1; i <= n; i++)
cin >> sup[i];
for(int i = 1; i <= m; i++){
for(int j = 1; j <= m; j++){
if (i == j)
continue;
else{
if (sup[i] == 1 && sup[j] == 2)
maze[j][i] = INF;
else if (sup[i] == 2 && sup[j] == 1)
maze[i][j] = INF;
}
}
}
dijksra(1);
if (dis[2] == INF)
printf("-1\n");
else
printf("%d\n",dis[2]);
}
return 0;
}