POJ 3767

简介: I Wanna Go Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2488   Accepted: 1041 Description The country i...
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          

 

目录
相关文章
|
存储
POJ 2487 Stamps
POJ 2487 Stamps
102 0
|
测试技术
POJ 1001
此题用最朴素的思路实现即可,需模拟加法器,乘法器,最烦人的地方是特殊情形,如末位是小数点(12.^2=144,取小数点),整数末位是0(100^2=10000),0次幂,测试用例可能超出题目中说的范围,可能包含0次幂(100.0^0=0, 0.10^1=0.1)。
748 0
POJ 2487 Stamps
Description Background Everybody hates Raymond. He’s the largest stamp collector on planet earth and because of that he always makes fun of all the others at the stamp collector parties.
1061 0
|
算法 机器人 编译器
POJ-2632
#include int main() { int k,a,b,n,m,i,j,num,rep,rect[100][100],robot[100][3]; int flag; char c; for(scanf("%d...
919 0
|
消息中间件 人工智能 JavaScript
|
机器学习/深度学习
|
存储
大数加法-poj-1503
poj-1503-Integer Inquiry Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking vari
1117 0