POJ3767 I Wanna Go Home dijkstra
2014-05-27 20:16
162 查看
题目链接:http://vjudge.net/contest/view.action?cid=47262#problem/A
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
Sample Output
理解要点:
1、永远不会走从领导2到领导1的路
2、先根据题意建立双向路,然后把存在领导2到领导1的路去除,让其变成领导1到领导2的单项路,之后就是dijkstra了
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
理解要点:
1、永远不会走从领导2到领导1的路
2、先根据题意建立双向路,然后把存在领导2到领导1的路去除,让其变成领导1到领导2的单项路,之后就是dijkstra了
#include <iostream> #include <cstdio> #include <cstring> #define inf 0x3fffffff using namespace std; int map[605][605]; int leader[605]; int dijks(int n) { int i,j,k,min; int dis[605]; int vis[605]; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++){ dis[i]=map[1][i];//一到n的距离; } for(i=1;i<=n;i++){ min=inf; k=1; for(j=1;j<=n;j++){ if(!vis[j]&&min>dis[j]){ min=dis[j]; k=j; } } vis[k]=1; if(k==2) break; for( j=1;j<=n;j++){ if(!vis[j]&&dis[k]+map[k][j]<dis[j]) dis[j]=dis[k]+map[k][j]; } } if(dis[2]==inf) return -1; else return dis[2]; } int main() { int n,i,j,a,b,t,m; while(~scanf("%d",&n),n) { for(i=1;i<=n;++i) for(j=1;j<i;++j) map[i][j]=map[j][i]=inf; scanf("%d",&m); while(m--) { scanf("%d%d%d",&a,&b,&t); map[a][b]=map[b][a]=t; } for(i=1;i<=n;++i) scanf("%d",&leader[i]); for(i=1;i<=n;++i) for(j=1;j<=n;++j) if(leader[i]!=leader[j])//将领导者为从2~1的路变成不能走的路; { if(leader[i]==1) map[j][i]=inf; else map[i][j]=inf; } printf("%d\n",dijks(n)); } return 0; }
相关文章推荐
- POJ 3767 I Wanna Go Home【最短路,Dijkstra+spfa,题意是关键呀】
- POJ 3767 I Wanna Go Home(Dijkstra)
- POJ3767----I Wanna Go Home
- poj3767 I Wanna Go Home
- POJ-3767 I Wanna Go Home -----有限制的dijkstra
- 题目1162:I Wanna Go Home:标准单源最短路
- 题目1162:I Wanna Go Home
- POJ 3767 I Wanna Go Home
- POJ 3767 I Wanna Go Home (dijkstra算法)
- poj 3767 I Wanna Go Home
- poj_3767——I Wanna Go Home
- 题目1162:I Wanna Go Home
- POJ 3767 I Wanna Go Home
- 九度OJ-1162:I Wanna Go Home
- POJ 3767 I Wanna Go Home
- 题目1162:I Wanna Go Home
- 九度 OJ 1162 I Wanna Go Home
- 九度 题目1162:I Wanna Go Home
- I wanna go home
- POJ - 3767 - I Wanna Go Home