pku2355 Railway tickets

來源:互聯網
上載者:User

有n個火車站,從一個網站到另一個網站的費用C與它們的距離L有關,有3種距離,對應有3種費用。現在求從一個網站S到另一個網站E所需的最小費用。

很簡單的DP,難度等價於數字三角形,

注意:輸入中起點不一定大於終點

View Code

 1 program pku2355(input,output); 2 var 3     f    :array[0..10010] of longint; 4     d    :array[0..10010] of longint; 5     l,c    :array[1..3] of longint; 6     x,y    :longint; 7     n    :longint; 8 procedure swap(var aa,bb:longint); 9 var10     tt:longint;11 begin12     tt:=aa;13     aa:=bb;14     bb:=tt;15 end;16 procedure init;17 var18     i:longint;19 begin20     for i:=1 to 3 do21         read(l[i]);22     for i:=1 to 3 do23         read(c[i]);24     readln;25     readln(n);26     readln(x,y);27     if x>y then28         swap(x,y);29     d[1]:=0;30     for i:=2 to n do31         readln(d[i]);32 end;33 procedure main;34 var35     i,j:longint;36 begin37     fillchar(f,sizeof(f),63);38     f[x]:=0;39     for i:=x+1 to y do40     begin41         for j:=i-1 downto x do42         begin43             if d[i]-d[j]>l[3] then44                 break;45             if (d[i]-d[j]<=l[1])and(f[j]+c[1]<f[i]) then46                 f[i]:=f[j]+c[1];47             if (d[i]-d[j]<=l[2])and(f[j]+c[2]<f[i]) then48                 f[i]:=f[j]+c[2];49             if (d[i]-d[j]<=l[3])and(f[j]+c[3]<f[i]) then50                 f[i]:=f[j]+c[3];51         end;52     end;53     writeln(f[y]);54 end;55 begin56     init;57     main;58 end.

本來這道題O(n^2)演算法過不了,但是適當break也水過了,poj馬上100道了,等完成最後一道在、再來寫線段樹最佳化的程式吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.