POJ 3767

I Wanna Go Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2488   Accepted: 1041

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 #include <stdio.h>
 3 #include <string.h>
 4 #define Max 0x7ffffff
 5 int map[605][605];
 6 int lead[605];
 7 bool vis[605];
 8 int ans[605];
 9 int city,rode;
10 void djk(int v)
11 {
12     memset(ans,0,sizeof(ans));
13     memset(vis,0,sizeof(vis));
14     int i,j,k,t,min = 0x7ffffff;
15     int next;
16     for(i=1;i<=city;i++)
17     {
18         ans[i] = map[v][i];
19         //printf("%d\n",ans[i]);
20     }
21     vis[v] = 1;
22     for(i=1;i<=city-1;i++)
23     {
24         min = 0x7ffffff;
25         for(j=1;j<=city;j++)
26         {
27             if(!vis[j]&&min>ans[j])
28             {
29                 next = j;
30                 min = ans[j];
31             }
32         }
33         vis[next] = 1;
34         for(k=1;k<=city;k++)
35         if(!vis[k]&&(ans[k]>(min + map[next][k])))
36         ////特别注意:不能定义为整形最大值 ,少了一个f就ac啦,因为 map[next][k]可能是整形最大值 ,再加上min就会溢出 
37             ans[k] = min + map[next][k];
38     }
39 }    
40 int main()
41 {
42     int i,j,k,t;
43     int from,to,cost;
44     while(scanf("%d%d",&city,&rode),city)
45     {
46         memset(lead,0,sizeof(lead));
47         for(i=1;i<=city;i++)
48             for(j=1;j<=city;j++)
49                 map[i][j] = map[j][i] = Max;
50         for(i=1;i<=rode;i++)
51         {
52             scanf("%d%d%d",&from,&to,&cost);
53             if(cost<map[from][to])//防止输入多次,但是花费不同的情况 
54                 map[from][to] = map[to][from] = cost;
55         } 
56         for(i=1;i<=city;i++)
57             scanf("%d",lead+i);
58         for(i=1;i<=city;i++)
59             for(j=1;j<=city;j++)
60                 if(lead[i]==2&&lead[j]==1)
61                     map[i][j] = Max;
62         djk(1);
63         if(ans[2]<Max&&ans[2]>=0)
64             printf("%d\n",ans[2]);
65         else
66             printf("-1\n");
67     }
68     return 0;
69 }      
70             
71          

 

posted @ 2012-08-21 18:16  加拿大小哥哥  阅读(214)  评论(0编辑  收藏  举报