poj-I Wanna Go Home

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 AB and T, which means the road between city A and city B will cost time TT 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

解题思路:

dijsktra求最短路,但是保存边的权值时有技巧,只保留从1阵营到2阵营的边,将2阵营到1阵营的边设为无穷大。这样做的原因是1点一定在1阵营,2点一定在2阵营,从1点到2点一定会经过且只能经过从1阵营到2阵营的边。

注意:

测试样例中可能任意两个顶点之间不止一条边,这时需要选择一条最短的边来保存。

还有就是顶点是从1开始的,这点在存储边的时候要注意。

最后不能用memset赋特别大的值(该值最好在0x00到0xff),否则后返回随机数。原因见:点击打开链接

# include<stdio.h>
# include<memory.h>
# define INF 123123123
int min(int a,int b)
{
	return a<b?a:b;
}
int w[700][700];
bool vis[700];
int mark[700];
int dis[700];
int main()
{
   int n,i,j;
   while(scanf("%d",&n)!=EOF)
   {
     if(n==0) break;
    // memset(w,INF,sizeof(w));//此时不能用memset
	 for(i=0;i<700;i++){
		 for(j=i;j<700;j++){
			 w[i][j]=w[j][i]=INF;
		 }
	 }
	 for(i=0;i<700;i++)
	 {
	    mark[i]=0;
		vis[i]=false;
		dis[i]=INF;
	 }
	 dis[1]=0;

	 int m;
	 scanf("%d",&m);
	 while(m--)
	 {
	    int a,b,c;
		scanf("%d%d%d",&a,&b,&c);//从1开始
		if(c<w[a][b])//可能有多条边
		  w[a][b]=w[b][a]=c;
	 }

	 for(i=1;i<=n;i++)
	 {
		scanf("%d",&mark[i]);
	 }

	 for(i=1;i<=n;i++)
	 {
		 for(j=1;j<=n;j++)
		 {
		     if(mark[i]==1&&mark[j]==2)
			 {
			    w[j][i]=INF;
			 }
		 }
	 }
     
	 for(i=1;i<=n;i++)
	 {
		int minv=INF,index=0;
	    for(j=1;j<=n;j++)
		{
		  if(vis[j]==false&&dis[j]<minv)
		  {
		     minv=dis[j];
			 index=j;
		  }
		}
		vis[index]=true;

		for(j=1;j<=n;j++)
		{
		  dis[j]=min(dis[j],dis[index]+w[index][j]);
		}
	 }
     
	 if(dis[2]!=INF)
	    printf("%d\n",dis[2]);
	 else
		 printf("-1\n");
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/zhuixun_/article/details/80143235