题目链接
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
题目大意:
N个城市分属于两个敌对集团,现在要从城市1到城市2(城市1与城市2分别属于集团1与集团2),所选择的路径中只能有一条道路跨越不同的集团,求最短路径。
解题思路:
由于所选择的路径中只能有一条道路跨越不同的集团且城市1属于集团1而城市2属于集团2,那么路径中肯定有一条跨集团道路,且一定是从集团1跨越到集团2。
这样的话,在建图的过程中,对于同一集团中的城市,它们之间的道路是双向的;对于不同集团中的城市,它们之间的道路是单向的,且只能从集团1中的城市走到集团2中的城市。
AC代码:
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 601
#define INF 0x3f3f3f3f
int map[maxn][maxn];
void init(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
map[i][j] = INF;}map[i][i] = 0;}
}
int type[maxn];
int n, m;
int d[maxn]; bool mark[maxn];
void dijkstra(int s) {
for (int i = 1; i <= n; i++) {
d[i] = INF; mark[i] = false;}int newnode = s; mark[newnode] = true;d[newnode] = 0;for (int i = 1; i < n; i++) {
for (int j = 1; j <= n; j++) {
if (mark[j] || map[newnode][j] == INF)continue;d[j] = min(d[j], d[newnode] + map[newnode][j]);}int tmp = INF;for (int k = 1; k <= n; k++) {
if (mark[k] || d[k] == INF)continue;if (d[k] < tmp) {
newnode = k; tmp = d[k];}}if (tmp == INF)return;mark[newnode] = true;}
}int main() {
while (cin >> n&&n) {
init(n);//初始化cin >> m;while (m--) {
int a, b, cost;cin >> a >> b >> cost;if (a > b) {
//方便后面重新建图int tmp = b; b = a; a = tmp;}map[a][b] = cost;}for (int i = 1; i <= n; i++)cin >> type[i];for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (type[i] == type[j]) map[j][i] = map[i][j];//同集团则双向else if (type[i] == 2) {
//不同集团则只能从集团1到集团2map[j][i] = map[i][j];map[i][j] = INF;//删去从集团2到集团1的道路}}}dijkstra(1);if (d[2] == INF)cout << -1 << endl;else cout << d[2] << endl;}return 0;
}
当然,也可以在dijkstra算法进行中对从集团2走到集团1的这一种情况进行剪枝:
AC代码:
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 601
#define INF 0x3f3f3f3f
int map[maxn][maxn];
void init(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
map[i][j] = INF;}map[i][i] = 0;}
}
int type[maxn];
int n, m;
int d[maxn]; bool mark[maxn];
void dijkstra(int s) {
for (int i = 1; i <= n; i++) {
d[i] = INF; mark[i] = false;}int newnode = s; mark[newnode] = true;d[newnode] = 0;for (int i = 1; i < n; i++) {
for (int j = 1; j <= n; j++) {
//对从集团2走到集团1的这种情况进行剪枝if (mark[j] || map[newnode][j] == INF || (type[newnode]==2&&type[j]==1))continue;d[j] = min(d[j], d[newnode] + map[newnode][j]);}int tmp = INF;for (int k = 1; k <= n; k++) {
if (mark[k] || d[k] == INF)continue;if (d[k] < tmp) {
newnode = k; tmp = d[k];}}if (tmp == INF)return;mark[newnode] = true;}
}int main() {
while (cin >> n&&n) {
init(n);//初始化cin >> m;while (m--) {
int a, b, cost;cin >> a >> b >> cost;map[a][b] = map[b][a] = cost;}for (int i = 1; i <= n; i++)cin >> type[i];dijkstra(1);if (d[2] == INF)cout << -1 << endl;else cout << d[2] << endl;}return 0;
}