usaco Postal Vans(dp)
是哈密頓回路,然后。。。就不知道怎么寫了 ,以前寫過類似的不過情況沒這么多也沒這么復(fù)
?usaco training 6.1.1 Postal Vans 題解
標(biāo)簽:?usaco training題解dp 2014-03-18 10:49?1511人閱讀?評(píng)論(0)?收藏?舉報(bào) ?分類:版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
目錄(?)[+]
轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/jiangshibiao/article/details/21446033
【原題】
Postal Vans
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
- #include<cstdio>??
- using?namespace?std;??
- int?n,i,f[1001],g[1001];??
- int?main()??
- {??
- ??scanf("%d",&n);??
- ??g[1]=2;g[2]=2;g[3]=8;??
- ??f[1]=0;f[2]=2;f[3]=4;??
- ??for?(i=4;i<=n;i++)??
- ??{??
- ????g[i]=f[i-1]*2+g[i-1]+g[i-2]-g[i-3];??
- ????f[i]=f[i-1]+g[i-1];??
- ??}??
- ??printf("%d",f[n]);??
- }??
【代碼2】高精
[cpp]?view plaincopy
- /*?
- PROG:vans?
- ID:juan1973?
- LANG:C++?
- */??
- #include<cstdio>??
- using?namespace?std;??
- struct?arr{int?n,p[1001];}g[1001],f[1001];??
- int?n,i;??
- arr?chen(arr?a)??
- {??
- ??for?(int?i=1;i<=a.n;i++)??
- ????a.p[i]*=2;??
- ??for?(int?i=1;i<=a.n;i++)??
- ????if?(a.p[i]>9)?{a.p[i+1]+=a.p[i]/10;a.p[i]%=10;}??
- ??if?(a.p[a.n+1]>0)?a.n++;??
- ??return?a;??
- }??
- arr?add(arr?a,arr?b)??
- {??
- ??a.n=a.n>b.n?a.n:b.n;??
- ??for?(int?i=1;i<=a.n;i++)??
- ????a.p[i]+=b.p[i];??
- ??for?(int?i=1;i<=a.n;i++)??
- ????if?(a.p[i]>9)?{a.p[i+1]+=a.p[i]/10;a.p[i]%=10;}??
- ??if?(a.p[a.n+1]>0)?a.n++;??
- ??return?a;??
- }??
- arr?Minus(arr?a,arr?b)??
- {??
- ??int?i=1,j,k;????
- ??while?(i<=b.n)????
- ??{????
- ????if?(a.p[i]>=b.p[i])a.p[i]=a.p[i]-b.p[i];????
- ????else????
- ????{????
- ??????j=i+1;????
- ??????while?(a.p[j]==0)?j++;????
- ??????a.p[j]--;????
- ??????for?(k=i+1;k<j;k++)?a.p[k]=9;????
- ??????a.p[i]=a.p[i]+10-b.p[i];????
- ????}????
- ????i++;????
- ??}????
- ??while?(a.p[a.n]==0&&a.n>1)a.n--;????
- ??return?a;????
- }????
- int?main()??
- {??
- ??freopen("vans.in","r",stdin);??
- ??freopen("vans.out","w",stdout);??
- ??scanf("%d",&n);??
- ??g[1].p[1]=2;g[2].p[1]=2;g[3].p[1]=8;??
- ??g[1].n=g[2].n=g[3].n=1;??
- ??f[1].p[1]=0;f[2].p[1]=2;f[3].p[1]=4;??
- ??f[1].n=f[2].n=f[3].n=1;??
- ??for?(i=4;i<=n;i++)??
- ??{??
- ????g[i]=Minus(add(chen(f[i-1]),add(g[i-1],g[i-2])),g[i-3]);??
- ????f[i]=add(f[i-1],g[i-1]);??
- ??}??
- ??for?(i=f[n].n;i>0;i--)??
- ????printf("%d",f[n].p[i]);??
- ??printf("\n");??
- }??
/*
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)容,希望文章能夠幫你解決所遇到的問題。