日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

usaco Postal Vans(dp)

發(fā)布時(shí)間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 usaco Postal Vans(dp) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

是哈密頓回路,然后。。。就不知道怎么寫了 ,以前寫過類似的不過情況沒這么多也沒這么復(fù)

?

usaco training 6.1.1 Postal Vans 題解

標(biāo)簽:?usaco training題解dp ?1511人閱讀?評(píng)論(0)?收藏?舉報(bào) ?分類:

目錄(?)[+]

轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/jiangshibiao/article/details/21446033

【原題】

Postal Vans

ACM South Pacific Region -- 2003

Tiring of their idyllic fields, the cows have moved to a new suburb. The suburb is a rectangular grid of streets with a post office at its Northwest corner. It has four avenues running East-West and N (1 <= N <= 1000) streets running North-South.

For example, the following diagram shows such a suburb with N=5 streets, with the avenues depicted as horizontal lines, and the post office as a dark blob at the top-left corner:

Each day the postal van leaves the post office, drives around the suburb and returns to the post office, passing exactly once through every intersection (including those on borders or corners). The executives from the post company want to know how many distinct routes can be established for the postal van (of course, the route direction is significant in this count).

For example, the following diagrams show two such routes for the above suburb:

As another example, the following diagrams show all the four possible routes for a suburb with N=3 streets:

Write a program that will determine the number of such distinct routes given the number of streets.

PROGRAM NAME: vans

INPUT FORMAT

  • Line 1: A single integer, N

SAMPLE INPUT (file vans.in)

4

OUTPUT FORMAT

  • Line 1: A single integer that tells how many possible distinct routes corresponding to the number of streets given in the input.

SAMPLE OUTPUT (file vans.out)

12


【譯題】

描述

郊區(qū)呈矩形,有四條東西方向的街道和N(1<=N<=1000)條南北方向的街道。在郊區(qū)的西北角有一個(gè)郵局。

如N=5時(shí),郊區(qū)如下圖所示,圓點(diǎn)表示郵局,直線表示街道。

每天郵政卡車從郵局出發(fā),每個(gè)十字路口(包括邊界和四角)經(jīng)過且只經(jīng)過一次。現(xiàn)在郵局希望知道郵政貨車行駛的路線有幾種。 例如,下面兩幅圖表示的是滿足上圖的兩條路線

另一個(gè)例子,下面四幅圖表示了當(dāng)N=3時(shí)的全部四種情況

[編輯]格式

PROGRAM NAME: vans

INPUT FORMAT:

(file vans.in)

INPUT FORMAT 一行:一個(gè)數(shù)值N

OUTPUT FORMAT:

(file vans.out) 一行: 到INPUT中給出的街道的路徑總數(shù)

[編輯]SAMPLE INPUT

 4

[編輯]SAMPLE OUTPUT

 12

wcada


【前言】參考了pty大神的詳細(xì)推導(dǎo),于是把題解原封不動(dòng)地寫在這里。

【題解】

----------------------------------------------原文始-----------------------------------------------------

預(yù)備知識(shí):

哈密度路:由一個(gè)點(diǎn)出發(fā)到另外一個(gè)點(diǎn)結(jié)束,要求經(jīng)過圖中所有的點(diǎn)的一條路(不能重復(fù)經(jīng)過點(diǎn))。

哈密頓回路:從一個(gè)點(diǎn)出發(fā)再回到此點(diǎn),經(jīng)過圖中所有點(diǎn)的一條路(不能重復(fù)經(jīng)過點(diǎn))。

?

問題顯然的解法:對(duì)于一個(gè)n*4的圖求哈密頓回路的個(gè)數(shù)。用陳丹琦的方法。

但是由于寬度只有4,所以有另外一種遞推的方法,達(dá)到優(yōu)化的目的:

設(shè)f[i]為前i列中,第i列的第一個(gè)格子到第二個(gè)格子的哈密度路的條數(shù)。(顯然,f[n]就為答案)


1 ? ? ? ? 2 ? ? ? ? ?3?? 。。i-1?? ??i

設(shè)g[i]為前i列中,第i列的第一個(gè)格子到第四個(gè)格子的哈密頓路的條數(shù)。


1 ? ? ? ? ? 2 ? ? ? ? 3?? 。。i-1???? i

很顯然的:f[i]=f[i-1]+g[i-1]

證明第i列一二號(hào)格子的只能由兩種方式得到:

1、?=f[i-1]? 2、=g[i-1]

下面介紹g[i]的遞推方法:

分四種情況進(jìn)行討論:g[i]=g1[i]+g2[i]+g3[i]+g4[i]

1、?=g1[i]=f[i-1]?? 2、?=g2[i]=f[i-1]

3、?=g3[i]=g[i-2]?? 4、?g4[i]

又分三種情況討論

?=f[i-2]? ?=f[i-2]

有沒有發(fā)現(xiàn)這種情況就是第i-1列的g4[i-1]

所以g4[i]=g4[i-1]+f[i-2]*2=g[i-1]-g1[i-1]-g2[i-1]-g3[i-1]+f[i-2]*2=g[i-1]-g[i-3]

所以g[i]=g1[i]+g2[i]+g3[i]+g4[i]=f[i-1]*2+g[i-1]+g[i-2]-g[i-3](比較優(yōu)美吧。呵呵)

兩式聯(lián)立:f[i]=f[i-1]+g[i-1]

----------------------------------------------原文完-----------------------------------------------------

【代碼1】非高精

[cpp]?view plaincopy
  1. #include<cstdio>??
  2. using?namespace?std;??
  3. int?n,i,f[1001],g[1001];??
  4. int?main()??
  5. {??
  6. ??scanf("%d",&n);??
  7. ??g[1]=2;g[2]=2;g[3]=8;??
  8. ??f[1]=0;f[2]=2;f[3]=4;??
  9. ??for?(i=4;i<=n;i++)??
  10. ??{??
  11. ????g[i]=f[i-1]*2+g[i-1]+g[i-2]-g[i-3];??
  12. ????f[i]=f[i-1]+g[i-1];??
  13. ??}??
  14. ??printf("%d",f[n]);??
  15. }??

【代碼2】高精

[cpp]?view plaincopy
  1. /*?
  2. PROG:vans?
  3. ID:juan1973?
  4. LANG:C++?
  5. */??
  6. #include<cstdio>??
  7. using?namespace?std;??
  8. struct?arr{int?n,p[1001];}g[1001],f[1001];??
  9. int?n,i;??
  10. arr?chen(arr?a)??
  11. {??
  12. ??for?(int?i=1;i<=a.n;i++)??
  13. ????a.p[i]*=2;??
  14. ??for?(int?i=1;i<=a.n;i++)??
  15. ????if?(a.p[i]>9)?{a.p[i+1]+=a.p[i]/10;a.p[i]%=10;}??
  16. ??if?(a.p[a.n+1]>0)?a.n++;??
  17. ??return?a;??
  18. }??
  19. arr?add(arr?a,arr?b)??
  20. {??
  21. ??a.n=a.n>b.n?a.n:b.n;??
  22. ??for?(int?i=1;i<=a.n;i++)??
  23. ????a.p[i]+=b.p[i];??
  24. ??for?(int?i=1;i<=a.n;i++)??
  25. ????if?(a.p[i]>9)?{a.p[i+1]+=a.p[i]/10;a.p[i]%=10;}??
  26. ??if?(a.p[a.n+1]>0)?a.n++;??
  27. ??return?a;??
  28. }??
  29. arr?Minus(arr?a,arr?b)??
  30. {??
  31. ??int?i=1,j,k;????
  32. ??while?(i<=b.n)????
  33. ??{????
  34. ????if?(a.p[i]>=b.p[i])a.p[i]=a.p[i]-b.p[i];????
  35. ????else????
  36. ????{????
  37. ??????j=i+1;????
  38. ??????while?(a.p[j]==0)?j++;????
  39. ??????a.p[j]--;????
  40. ??????for?(k=i+1;k<j;k++)?a.p[k]=9;????
  41. ??????a.p[i]=a.p[i]+10-b.p[i];????
  42. ????}????
  43. ????i++;????
  44. ??}????
  45. ??while?(a.p[a.n]==0&&a.n>1)a.n--;????
  46. ??return?a;????
  47. }????
  48. int?main()??
  49. {??
  50. ??freopen("vans.in","r",stdin);??
  51. ??freopen("vans.out","w",stdout);??
  52. ??scanf("%d",&n);??
  53. ??g[1].p[1]=2;g[2].p[1]=2;g[3].p[1]=8;??
  54. ??g[1].n=g[2].n=g[3].n=1;??
  55. ??f[1].p[1]=0;f[2].p[1]=2;f[3].p[1]=4;??
  56. ??f[1].n=f[2].n=f[3].n=1;??
  57. ??for?(i=4;i<=n;i++)??
  58. ??{??
  59. ????g[i]=Minus(add(chen(f[i-1]),add(g[i-1],g[i-2])),g[i-3]);??
  60. ????f[i]=add(f[i-1],g[i-1]);??
  61. ??}??
  62. ??for?(i=f[n].n;i>0;i--)??
  63. ????printf("%d",f[n].p[i]);??
  64. ??printf("\n");??
  65. }??




/*
ID:jinbo wu
TASK: vans
LANG:C++
*/
#include<bits/stdc++.h>
using namespace std;
struct node
{int a[1005];
};
node f[1010],g[1010];
node mult(node t)
{node ans;int flag=0;for(int i=1;i<=1000;i++){int tmp=t.a[i]*2;if(flag==1){tmp+=1;flag=0;}if(tmp>=10){flag=1;tmp-=10;}ans.a[i]=tmp;}return ans;
}
node add(node t1,node t2)
{node ans;int flag=0;for(int i=1;i<=1000;i++){int tmp=t1.a[i]+t2.a[i];if(flag==1){tmp+=1;flag=0;}if(tmp>=10){flag=1;tmp-=10;}ans.a[i]=tmp;}return ans;
}
node minu(node t1,node t2)
{node ans;int flag=0;for(int i=1;i<=1000;i++){int tmp=t1.a[i]-t2.a[i];if(flag==1){tmp-=1;flag=0;}if(tmp<0){flag=1;tmp+=10;}ans.a[i]=tmp;}return ans;
}
int main()
{freopen("vans.in","r",stdin);freopen("vans.out","w",stdout);int n;cin>>n;g[1].a[1]=2;g[2].a[1]=2;g[3].a[1]=8;f[1].a[1]=0;f[2].a[1]=2;f[3].a[1]=4;for (int i=4;i<=n;i++){g[i]=minu(add(g[i-1],add(g[i-2],mult(f[i-1]))),g[i-3]);f[i]=add(f[i-1],g[i-1]);}int flag=0;for(int i=1000;i>=1;i--){if(f[n].a[i]||flag){cout<<f[n].a[i];flag=1;}}if(flag==0)cout<<0;cout<<endl;
}



總結(jié)

以上是生活随笔為你收集整理的usaco Postal Vans(dp)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。