CF 586B 起点到终点的最短路和次短路之和

起点是右下角  终点是左上角
每次数据都是两行的点  输入n 表示有n列
接下来来的2行是 列与列之间的距离
最后一行是  行之间的距离
枚举就行
 
Sample test(s)
input
4
1 2 3
3 2 1
3 2 2 3
output
12
input
3
1 2
3 3
2 1 3
output
11
input
2
1
1
1 1
output
4

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <string>
 6 # include <cmath>
 7 # include <queue>
 8 # define LL long long
 9 using namespace std ;
10 
11 const int INF = 0x3f3f3f3f ;
12 
13 int a[3][60] ;
14 int b[60] ;
15 
16 int main()
17 {
18     int n;
19     while(scanf("%d",&n) != EOF)
20     {
21        int i , j ;
22        int sum = 0 ;
23        for (i = 0 ; i < 2  ; i++)
24           for (j = 0 ; j < n-1 ; j++)
25            scanf("%d" , &a[i][j]) ;
26        for (i = 0 ; i < n  ; i++)
27           scanf("%d" , &b[i]) ;
28        int ans[3] = {INF , INF , INF} ;
29        for (i = 0 ; i < n-1  ; i++)
30        {
31            sum += a[1][i] ;
32        }
33        sum += b[0] ;
34        ans[2] = sum ;
35        sort(ans , ans+3) ;
36        for (i = 0 ; i < n-1  ; i++)
37        {
38            sum = sum - a[1][i] + a[0][i] ;
39            sum = sum - b[i] + b[i+1] ;
40            ans[2] = sum ;
41            sort(ans , ans+3) ;
42        }
43        printf("%d\n" , ans[0] + ans[1]) ;
44 
45 
46 
47     }
48     return 0;
49 }
View Code

 

posted @ 2015-10-13 23:05  __Meng  阅读(223)  评论(0编辑  收藏  举报