乘公交看奥运问题转乘公汽一次mathematica源码~

这段代码部分是我的功劳,大部分是饭盒同学的智慧嘎嘎~

cc[l_List, qd_, zd_] :=
  Module[{i, j, lx1 = {}, lx2 = {}, t4 = {}, t6 = {}},
               For[i = 1, i <= 1042, i++, t1 = {}; t2 = {};
                               For[j = 4, j <= Length[l[[i]]], j++,
     If[l[[i, j]] == qd, AppendTo[t1, i];
      For[k = j + 1, k <= Length[l[[i]]], k++, AppendTo[t1, l[[i, k]]]
                                                            ];
      AppendTo[lx1, t1]
                                           ];
     If[l[[i, j]] == zd, AppendTo[t2, i];
      For[k = 4, k <= j – 1, k++, AppendTo[t2, l[[i, k]]]
                                                             ];
      AppendTo[lx2, t2]
                                            ]
                                     ]
                          ];
              For[i = 1, i <= Length[lx1], i++,
    For[j = 1, j <= Length[lx2], j++, t3 = {};
     If[Intersection[lx1[[i]], lx2[[j]]] != {} &&
       lx1[[i, 1]] != lx2[[j, 1]], AppendTo[t3, lx1[[i, 1]]];  
      AppendTo[t3, Intersection[lx1[[i]], lx2[[j]]]];
      AppendTo[t3, lx2[[j, 1]]]; AppendTo[t4, t3]
                                              ]
                                   ]
                       ];
              For[i = 1, i <= Length[t4], i++,
    For[j = 1, j <= Length[t4[[i, 2]]], j++, t5 = {};
                                 x1 = Position[l[[t4[[i, 1]]]], qd];
     x2 = Position[l[[t4[[i, 1]]]], t4[[i, 2, j]]];
      x3 = Position[l[[t4[[i, 3]]]], t4[[i, 2, j]]];
     x4 = Position[l[[t4[[i, 3]]]], zd];
     AppendTo[t5, l[[t4[[i, 1]], 1]]];  
     AppendTo[t5, t4[[i, 2, j]]];
     AppendTo[t5, l[[t4[[i, 3]], 1]]];
     AppendTo[t5,
      x2[[1, 1]] – x1[[1 , 1]] + x4[[1, 1]] – x3[[1, 1]]]   ;
     If[l[[t4[[i, 3]], 2]] == “DY”, money1 = 1,
      Which[(x2[[1, 1]] – x1[[1 , 1]]) >=
         0 && (x2[[1, 1]] – x1[[1 , 1]]) <= 20,
       money1 =
        1, (x2[[1, 1]] – x1[[1 , 1]]) >=
         21 && (x2[[1, 1]] – x1[[1 , 1]]) <= 40,
       money1 = 2, (x2[[1, 1]] – x1[[1 , 1]]) > 40, money1 = 3]];
     If[l[[t4[[i, 3]], 2]] == “DY”, money2 = 1,
      Which[(x4[[1, 1]] – x3[[1 , 1]]) >=
         0 && (x4[[1, 1]] – x3[[1 , 1]]) <= 20,
       money2 =
        1, (x4[[1, 1]] – x3[[1 , 1]]) >=
         21 && (x4[[1, 1]] – x3[[1 , 1]]) <= 40,
       money2 = 2, (x4[[1, 1]] – x3[[1 , 1]]) > 40, money2 = 3]];
                AppendTo[t5, money1 + money2];
     AppendTo[
      t5, (x2[[1, 1]] – x1[[1 , 1]] + x4[[1, 1]] – x3[[1, 1]])*3 + 5];
     AppendTo[t6, t5]
                             ]
                     ];
         Print[t6]
              ];

记下今天mathematica中用的两个新函数

第一个  Flatten

用法:这个是help brows上的   

Flatten[list]
flattens out nested lists.
Flatten[list,n]
flattens to level n.
Flatten[list,n,h]
flattens subexpressions with head h.
Flatten[list,{{Subscript[s, 11],Subscript[s, 12],[Ellipsis]},{Subscript[s, 21],Subscript[s, 22],[Ellipsis]},[Ellipsis]}]
flattens list by combining all levels Subscript[s, ij] to make each level i in the result.

这个理解有点难度呵呵,其实也就是那个意思嘎嘎

看下里面的 Basic examples

一,Flatten out lists at all levels:

      In[1]:=Flatten[{{a, b}, {c, {d}, e}, {f, {g, h}}}]

      Out[1]:= {a, b, c, d, e, f, g, h}

二,Flatten only at level 1:

       In[1]:= Flatten[{{a, b}, {c, {d}, e}, {f, {g, h}}}, 1]

      Out[1]:={a, b, c, {d}, e, f, {g, h}}

好了例子就是这个了,不过注意是basic呵呵

第二个  Intersection

Intersection[Subscript[list, 1],Subscript[list, 2],[Ellipsis]]
gives a sorted list of the elements common to all the Subscript[list, i].

Basic examples

Find elements common to all the lists given:

In[1]:=Intersection[{1, 1, 2, 3}, {3, 1, 4}, {4, 1, 3, 3}]

Out[1]:={1, 3}

好了用法就是这样的~

主要是解决公交换乘问题换乘一次的方案实施~

好吧,洗洗睡了,上次的lingo分析还没彻底搞好,真是悲了个剧,算了睡觉,我还想再活五百年~

整理一下做完整的钢管订购与运输问题(题1,3)[前面的数据输入有误,多谢明明同学帮忙找错……]

首先铁路的数据

a = {{1, 29, 20}, {1, 30, 202}, {2, 30, 1200}, {3, 31, 690}, {4, 33,
690}, {5, 35, 462}, {6, 38, 70}, {7, 39, 30}, {23, 24, 450}, {24,
25, 80}, {24, 26, 1150}, {27, 28, 306}, {28, 29, 195}, {26, 30,
1100}, {30, 31, 720}, {31, 32, 520}, {32, 33, 170}, {33, 35,
88}, {33, 34, 160}, {34, 36, 70}, {34, 37, 320}, {37, 38,
160}, {38, 39, 290}}

公路数据(题1)

b = {{8, 9, 104}, {9, 10, 301}, {10, 11, 750}, {11, 12, 606}, {12, 13,
194}, {13, 14, 205}, {14, 15, 201}, {15, 16, 680}, {16, 17,
480}, {17, 18, 300}, {18, 19, 220}, {19, 20, 210}, {20, 21,
420}, {21, 22, 500}, {9, 23, 3}, {10, 25, 2}, {11, 26, 600}, {12,
27, 10}, {13, 28, 5}, {14, 29, 10}, {1, 14, 31}, {15, 30, 12}, {16,
31, 42}, {17, 32, 70}, {18, 35, 10}, {19, 36, 10}, {20, 37,
62}, {6, 21, 110}, {21, 38, 30}, {22, 39, 20}, {7, 22, 20}}

公路数据(题2)

b = {{8, 9, 104}, {9, 10, 301}, {10, 11, 750}, {11, 12, 606}, {12, 13,
194}, {13, 14, 205}, {14, 15, 201}, {15, 16, 680}, {16, 17,
480}, {17, 18, 300}, {18, 19, 220}, {19, 20, 210}, {20, 21,
420}, {21, 22, 500}, {9, 23, 3}, {10, 25, 2}, {11, 26, 600}, {12,
27, 10}, {13, 28, 5}, {14, 29, 10}, {1, 14, 31}, {15, 30, 12}, {16,
31, 42}, {17, 32, 70}, {18, 35, 10}, {19, 36, 10}, {20, 37,
62}, {6, 21, 110}, {21, 38, 30}, {22, 39, 20}, {7, 22, 20}, {34,
35, 130}, {35, 36, 190}, {36, 37, 260}, {6, 37, 100}}

Floyd算法

Floyd[n_, a_List] :=
Module[{i, j, k}, d = Table[Infinity, {i, n}, {j, n}];
r = Table[j, {i, n}, {j, n}];
For[i = 1, i <= n, i++, d[[i, i]] = 0];
For[i = 1, i <= Length[a], i++,
d[[a[[i, 1]], a[[i, 2]]]] = a[[i, 3]];
d[[a[[i, 2]], a[[i, 1]]]] = a[[i, 3]]];
For[k = 1, k <= n, k++,
For[i = 1, i <= n, i++,
For[j = 1, j <= n, j++,
If[d[[i, k]] + d[[k, j]] < d[[i, j]],
d[[i, j]] = d[[i, k]] + d[[k, j]];
r[[i, j]] = r[[i, k]]]]]]];
Floyd[39, a];
MatrixForm[d]
MatrixForm[r] Continue reading “整理一下做完整的钢管订购与运输问题(题1,3)[前面的数据输入有误,多谢明明同学帮忙找错……]”

建模之钢管订购问题~

很郁闷,2点了,还没解决我白天留下的问题,暂且记下重要数据和代码

建模钢管

(*以下是铁路的数据*)
a = {{1, 29, 20}, {1, 30, 202}, {2, 30, 1200}, {3, 31, 690}, {4, 33,
690}, {5, 35, 462}, {6, 38, 70}, {7, 39, 30}, {23, 24, 450}, {24,
25, 80}, {24, 26, 1150}, {27, 28, 306}, {28, 29, 195}, {26, 30,
1100}, {30, 31, 720}, {31, 32, 520}, {32, 33, 170}, {33, 35,
88}, {33, 34, 160}, {34, 36, 70}, {34, 37, 320}, {37, 38,
160}, {38, 39, 290}}

(*Floyd算法*) Continue reading “建模之钢管订购问题~”

Hello world!

Welcome to 阿拉丁’s NOTE . This is your first footprint here. welcome!I’m looking forward to you coming again……