欢迎来到天天文库
浏览记录
ID:14852894
大小:32.05 KB
页数:49页
时间:2018-07-30
《算法大全(c,c )(algorithm daquan (c, c ))》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
算法大全(c,c++)(AlgorithmDaquan(C,c++))SolvingtheshortestpathofsinglesourcepointbyA.labelingmethod:VarA:array[1..Maxn,1..Maxn]ofinteger;B:array[1..Maxn]ofinteger;{b[i]referstotheshortestpathfromvertexitosourcepoint}Mark:array[1..Maxn]ofboolean;Procedurebhf;VarBest,best_j:integer;BeginFillchar(mark,sizeof(mark),false);Mark[1]:=true;b[1]:=0;{1assourcepoint}RepeatBest:=0;Fori:=1tondo Ifmark[i]then{everypointthathascalculatedtheshortestpath}Forj:=1tondoIf(notmark[j])and(a[i,j]>0)thenIf(best=0)or(b[i]+a[i,j]0thenbeginB[best_j]:=best;mark[best_j]:=true;End;Untilbest=0;End;{bhf}B.Floyedalgorithmsolvestheshortestpathbetweenallvertexpairs:Procedurefloyed;BeginForI:=1tondo Forj:=1tondoIfa[I,j]>0thenp[I,j]:=Ielsep[I,j]:=0;{p[I,j]representstheprecursornodeofJontheshortestpathfromItoj}Fork:=1tondo{enumerationintermediatenode}Fori:=1tondoForj:=1tondoIfa[i,k]+a[j,k]0thenpre[i]:=v0elsepre[i]:=0;End;Mark[v0]:=true;Repeat{everycycleaddsanodeclosesttothe1setandadjuststheparametersofothernodes}Min:=maxint;u:=0;{urecordsthenearestnodefromthe1set}Fori:=1tondoIf(notmark[i])and(d[i]0thenbeginMark[u]:=true;Fori:=1tondoIf(notmark[i])and(a[u,i]+d[u],thenEe[I]=Ve[j];TheneareststarttimeofD.edgeactivityisEl[I],andiftheedgeIisrepresentedby,thenEl[I]=Vl[k]-w[j,k];IfEe[j]=El[j],activeJisthekeyactivity,Thepathcomposedofkeyactivitiesisthecriticalpath.Solvingmethod:A.startsfromthesourcepointtopsort,determineswhetherthereisaloopandcalculatesVe;B.startsatthesink,topsort,andVl;C.calculatesEeandEl; 6.topologicalsortFindthepointofpenetration0,deletealltheedgesconnectedtoit,andrepeattheprocessrepeatedly.Examplesfindasequence,wherethesumofanycontinuousPtermispositive,thesumofanyQtermisnegative,andifnot,theoutputNO.7.loopproblemEulerloop(DFS)Definition:acircuitthatpassesonlyonceoneachedgeofagraph.(sufficientandnecessaryconditions:graphswithandwithoutsingularities)HamiltoncircuitDefinition:acircuitthatpassesonlyoncepervertexofagraph.OnestrokeNecessaryandsufficientconditions:thenumberofsingularpointsandthenumberofsingularpointsare0or2.9.judgingwhetherthereisanegativeloopBellman-fordalgorithminthegraph X[I],y[I],t[I]denotethestartingpoint,endpointandweightoftheIedgerespectively.AtotalofNnodesandmedges.ProcedureBellman-FordBeginForI:=0ton-1dod[I]:=+infinitive;D[0]:=0;ForI:=1ton-1doForj:=1tomdo{enumerationeachside}Ifd[x[j]]+t[j]=bestthenexit;{s[n]istheweightand}ofthefirstnitemsIfk<=nthenbeginIfv>w[k]thensearch(k+1,v-w[k]);Search(k+1,V);End;End;LDPF[I,j]selectsseveralitemsinthefirstIitemstomakethevolumeexactlyJ,whichisBooleantype.Implementation:transformingoptimizationproblemsintodeterministicproblemsF[I,j]=f[I-1,j-w[i]](w[I]<=j<=v)boundary:f[0,0]:=true. ForI:=1tondoForj:=w[I]toVdoF[I,j]:=f[I-1,j-w[I]];Optimization:thecurrentstateisonlyrelatedtothepreviousstagestate,andcanbereducedtoonedimension.F[0]:=true;ForI:=1tondobeginF1:=f;Forj:=w[I]toVdoIff[j-w[I]]thenf1[j]:=true;F:=f1;End;B.seeksthemaximumvaluethatcanbeputin.F[I,j]forcapacityI,takethemaximumvalueobtainedbeforetheJbackpack.F[i,j]=max{f[I-w[J],j-1]+P[J],f[I,j-1]}C.asksthenumberofexactlyfilledcases.DP: Procedureupdate;VarJ,k:integer;BeginC:=a;Forj:=0tondoIfa[j]>0thenIfj+now<=nthenInc(c[j+now],a[j]);A:=c;End;2.reusableBackpackAasksforthemostweightyoucanputin.F[I,J]selectsanumberofitemsinthefirstIitemstomakethevolumeexactlyJ,whichisBooleantype.ThestatetransitionequationisF[I,j]=f[I-1,J-w[I]*k](k=1..Jdivw[I]) B.seeksthemaximumvaluethatcanbeputin.USACO1.2ScoreInflationForacompetition,thetotaltimeisTfixed,thereareanumberofoptionaltopics,eachsubjectcanenterthenumberofunlimited,eachThereisaTiproblem(thetimeneededtosolvetheproblem)andaSi(thefractionoftheanswertothisquestion)WhenthetotaltimeofsolvingthesequestionsislessthanT,thetotalscoreisthelargest,andthemaximumscoreisobtained.Easytothinkof:F[i,j]=max{f[i-k*w[j],j-1]+k*p[j]}(0<=k<=Idivw[j])Amongthem,f[i,j],whenthecapacityisI,themaximumofthejknapsackcanreach.*implementation:BeginFillChar(F,SizeOf(f),0);Fori:=1ToMDo Forj:=1ToNDoIfi-problem[j].time>=0ThenBeginT:=problem[j].point+f[i-problem[j].time];Ift>f[i]Thenf[i]:=t;End;Writeln(f[M]);End.C.asksthenumberofexactlyfilledcases.Ahoi2001Problem2Thenumberofexpressionsforthesumofprimenumberswithessentiallydifferentnaturalnumbersn.First,generatethepermutationofthecoefficientsofeachprimenumber,andtestthemonebyone,whichisthemethod.Proceduretry(dep:integer);VarI,j:integer;Begin Cal;{thisprocesscalculatestheresultofthecurrentcoefficient,andnowistheresult}Ifnow>nthenexit;{pruning}Ifdep=l+1thenbegin{generatesallcoefficients}Cal;Ifnow=nthenInc(TOT);Exit;End;Fori:=0tondivpr[dep]dobeginXs[dep]:=i;Try(dep+1);Xs[dep]:=0;End;End;Thinkingtwo,recursivesearchefficiencyishigherProceduretry(DEP,rest:integer); VarI,J,x:integer;BeginIf(rest<=0)or(dep=l+1)thenbeginIfrest=0thenInc(TOT);Exit;End;Fori:=0torestdivpr[dep]doTry(dep+1,rest-pr[dep]*i);End;{main:try(1,n);}Ideathree:dynamicprogrammingcanbeusedtosolveUSACO1.2moneysystemVitems,BackpackCapacityofN,thetotalnumberofmethods.Transferequation:Procedureupdate; VarJ,k:integer;BeginC:=a;Forj:=0tondoIfa[j]>0thenFork:=1tondivnowdoIfj+now*k<=nthenInc(c[j+now*k],a[j]);A:=c;End;{main}BeginRead(now);{readtheweightofthefirstobject}I:=0;{a[i]isthetotalnumberofIwhentheknapsackcapacityis}Whilei<=ndobeginA[i]:=1;Inc(I,now);end;{definingtheweightofthefirstitem,theweightoftheintegermultiplesis1,astheinitial value}.AFori:=2toVdoBeginRead(now);Update;{dynamicupdate}End;Writeln(a[n]);Four,sortingalgorithmA.quicksort:Procedureqsort(L,r:integer);VarI,J,mid:integer;BeginI:=l;j:=r;mid:=a[(l+r)div2];{thenumberofthecurrentsequenceinthemiddlepositionisdefinedasthemiddlenumber}RepeatWhilea[i]middoDec(J);{intherighthalfofthesearchforasmallernumberthanthemiddle}Ifi<=jthenbegin{ifyoufindasetofnumbersthatareinconsistentwiththesortingtarget,exchangethem}Swap(a[i],a[j]);Inc(I);Dec(J);{}continuetofind}End;Untili>j;Ifla[j]thenswap(a[i],a[j]);End;D.bubblesortProcedurebubble_sort;VarI,J,k:integer;BeginFori:=1ton-1doForj:=ndowntoi+1doIfa[j]r)or(a[i]<=a[j])){satisfyingtherequirementofthecurrentelementoftheleftsequenceThenbeginTmp[t]:=a[i];Inc(I);EndElsebeginTmp[t]:=a[j];Inc(J);End;Inc(t);End;Fori:=ptoRdoa[i]:=tmp[i];End;{merge}Proceduremerge_sort(VaRa:listtype;P,r:integer);{mergesorta[p..R]}Varq:integer;Begin Ifp<>rthenbeginQ:=(p+r-1)div2;Merge_sort(a,P,Q);Merge_sort(a,q+1,R);Merge(a,P,Q,R);End;End;{main}BeginMerge_sort(a,1,n);End.G.radixsortIdea:sortingeachofthemfromlowtohighFive.HighprecisioncalculationDefinitionofhighprecisionnumbers:Type Hp=array[1..Maxlen]ofinteger;1.highprecisionadditionProcedureplus(a,b:hp;VARc:hp);VarI,len:integer;BeginFillchar(C,sizeof(c),0);Ifa[0]>b[0]thenlen:=a[0]elselen:=b[0];Fori:=1tolendobeginInc(c[i],a[i]+b[i]);Ifc[i]>10thenbeginDec(c[i],10);Inc(c[i+1]);end;{carry}End;Ifc[len+1]>0thenInc(len);C[0]:=len;End;{plus}2.highprecisionsubtraction Proceduresubstract(a,b:hp;VARc:hp);VarI,len:integer;BeginFillchar(C,sizeof(c),0);Ifa[0]>b[0]thenlen:=a[0]elselen:=b[0];对于i==1到莱恩开始股份有限公司(c,I,];如果cii<0然后开始(c,i,10);DEC(c[i+1]);结束;当(莱恩1)和(C=0)做DEC(镜头);c[0]==莱恩;结束;3。高精度乘以低精度程序多(一:惠普;B:LongInt;VaRC:HP);var,i,整数;开始fillchar(C,sizeof(C),0); =a[0];对于i==1到莱恩开始公司(C,I,];公司(C+1),(A(*)B)div10);C=i:=C[i]mod10;结束;公司(莱恩);而(C[镜头]>=10)开始处理最高位的进位}{c[镜头+1]:=C[莱恩]div10;c=10;公司(莱恩);结束;while(len>1)and(c[len]=0)dodec(len);{若不需进位则调整len}c[0]==莱恩;结束;{乘} 4。高精度乘以高精度程序high_multiply(A,B:惠普;varC:惠普}var,i,j,莱恩:整数;开始fillchar(C,sizeof(C),0);对于i:=1到a[0]对于j==1到B[0]开始公司(C[我+J-1],一个[我]*B[J]);公司(C[我],[我]C+J-1DIV10);C[我]:=C+J-1签证J-1][我+模10;结束;1=0[0]+;当(莱恩1)和(C=0)做DEC(镜头);c[0]==莱恩;end;5。高精度除以低精度 程序将(一:惠普;B:LongInt;HP;var变量D:LongInt);{var,i,整数;开始fillchar(C,sizeof(C),0);=:[0];d=0;我:=Len到1开始d=d*10+A[i];c;D:;结束;当(透镜1)和(c=0)时,DEC(透镜);c[0]==莱恩;结束;6。高精度除以高精度 程序high_devide(A,B:惠普;varC,D:HP);VaRI,莱恩:整数;开始fillchar(C,sizeof(C),0);fillchar(D,sizeof(D),0);=:[0];d[0]=1;我:=Len到1开始乘法(d,10,d);D[1]:=[i];while(compare(d,b)>=0)do{即d>=b}开始减法(D,B,D);股份有限公司(丙);end;结束; 而(Len>1)和(参数[Len]=0)做DEC(LEN);c.len:=Len;end;六、树的遍历1.已知前序中序求后序过程解决(前置,中间:字符串);varI:整数;开始如果(预=“”或(“=”)退出;I:=POS(预[1],中);solve(copy(pre,2,i),copy(mid,1,i-1));解决(复制(预,i+1,长度(预)I),复制(中间,我+1,长度(中)I));职位:=岗位+预[1];{加上根,递归结束后后即为后序遍历}结束;2.已知中序后序求前序 过程解决(中间,邮政:字符串);varI:整数;开始如果(中=“”或(POST=“”)然后退出;i:=(POST(长度(POST)],中间);预:=预+岗位(岗位)[长度];{加上根,递归结束后预即为前序遍历}解决(复印件(中、1、I-1),复制(后,1,:));解决(复制(中,我+1,长度(中)I),复制(POST,i,长度(POST)I));end;3。已知前序后序求中序的一种函数ok(S1,S2,字符串):布尔函数;varI,L:整数;P:布尔;开始ok=真的; L==长度(S1);对于i:=1到l开始=错误的;对于j=1到L做如果S1i=,则为;如果不是p,则开始ok;=false;退出;结束;end;end;过程解决(前,后,字符串);varI:整数;开始如果(前=“”或(POST=“”)然后退出;I=0;重复公司(一);直到OK(副本,前2,i),副本(POST,1,i)); 解决(复印件,预,2,一),副本(邮寄,1,I);midstr:=midstr+预[1];解决(复印件(前,我2,长度(预)-I-1),复制(后,我1,长度(后)-I-1));结束;七进制转换1。任意正整数进制间的互化除N取余2.实数任意正整数进制间的互化乘n取整3.负数进制:设计一个程序,读入一个十进制数的基数和一个负进制数的基数,并将此十进制数转换为此负进制下的数:-R∈{-2,-3,-4,....-20}八全排列与组合的生成1.排列的生成:(1..n) 过程求解(DEP:整数);VaRI:整数;开始如果DEP=n+1然后开始Writeln(S);退出;结束;对于i:=1到n做如果不使用,我就开始S=S+CHR(我+ORD(“0”));用[我]:=真;解决(DEP+1);拷贝=(s,1,长度(s)-1);使用[i==false;结束;结束;2.组合的生成(1..n中选取k个数的所有方案)过程解决(DEP,前:整数);VaRI:整数; 开始ifdep=k+1thenbeginwriteln(s);exit;end;对于i:=1到n做如果(不使用[i])和(i预),然后开始s:=s+chr(i+ord(''0''));used[i]:=true;解决(DEP+1,I);拷贝=(s,1,长度(s)-1);使用[i==false;结束;结束;九。查找算法1。折半查找折半查找(K值)功能:整数;varlow,hig,mid:integer;开始low:=1;hig:=n; 中:=(低+高)div2;而(一个[中]。关键<>K)和(低<=HIG)开始ifa[mid].key>kthenhig:=mid-1否则低:=中+1;中:=(低+高)div2;结束;iflow>higthenmid:=0;折半查找:=中;结束;2。树形查找二叉排序树:每个结点的值都大于其左子树任一结点的值而小于其右子树任一结点的值。查找functiontreesrh(k:keytype):pointer;指针;开始 q=根;当(q<0)和(q。如果k。右:右;treesrh:=Q;end;十、贪心*会议问题(1)N个活动每个活动有一个开始时间和一个结束时间,任一时刻仅一项活动进行,求满足活动数最多的情况。解:按每项活动的结束时间进行排序,排在前面的优先满足。(2)会议室空闲时间最少。(3)每个客户有一个愿付的租金,求最大利润。(4)共R间会议室,第我个客户需使用我间会议室,费用相同,求最大利润。十一、回溯法框架 1。n皇后问题过程尝试(i:字节);字节;开始如果i=n1,则开始打印;退出;结束;对于j=1到n做如果一个[我],[我]和B+C[J-1]开始x;一个[J]:=false;B[J]:+我=false;C[J-1]:=false;尝试(i+1);一个[J]:=true;B[我]:=+真实;C[J-1]:=真;end;end;2。河内塔汉诺塔h(n)=2×h(n-1)+1 h(1)=1初始所有铜片都在一柱上procedurehanoi(n,a,b,c:byte);{将第n块铜片从a柱通过b柱移到c柱上}beginifn=0thenexit;hanoi(n-1,a,c,b);{将上面的n-1块从a柱通过c柱移到b柱上}write(n,'movedfrom'a,'two',(c);hanoi(n-1,b,a,c);{将b上的n-1块从b柱通过a柱移到c柱上than;初始铜片分布在3个柱上,给定目标柱goalh[1..n]..3,0存放三个柱的状态,now与nowp存最大的不到位的铜片的柱号和编号,h[i,0]存第in个柱上的个数.proceduremove(k,goal:integer);{将最大不到位的k移到目标柱goal上} beginifk=0thenexit;fori:=1to3)doj:=1twohe[0]doifh[i,j]=cthenbeginnow:=i;nowp:=j;than;{找到k的位置}ifnow<>goalthenbegin若未移到目标}{move(k-1.6-nowgoal);{剩下的先移到没用的柱上}writeln(kmovedfromnowtwogoal);h[h[goal,goal,0]+1]:=h[now,nowp];h[now,nowp]:=0;inc.(h[goal],0);dec(h[now,0]);move(c-1goal);{剩下的移到目标上}than;十二、dfs框架noip2001数的划分procedurework(dep,pre,s:longint);{入口为work(1,1,n)} {dep为当前试放的第dep个数,pre为前一次试放的数,s为当前剩余可分的总数}wasj:longint;beginifdep=nthenbeginifs>=pretheninc(r);exit;than;j:=pretwosdiv2dowork(dep+1,j,sj);than;类似:proceduretry(dep:integer);wasi:integer;beginifdep=kthenbeginiftot>=a[dep-1]theninc.(sum);exit;than; fori:=a[dep-1]twototdiv2dobegina[dep]:=i;dec(tot,i);try(dep+1);inc.(tot,i);than;thantry};{十三、bfs框架ioi94房间问题head:=1;tail:=0;whiletail=1),and(inthe<=l.len)thenwhilej
此文档下载收益归作者所有
举报原因
联系方式
详细说明
内容无法转码请点击此处