當前位置:首頁 » 編程語言 » c語言圖論演算法

c語言圖論演算法

發布時間: 2023-01-30 21:01:12

1. c語言編寫路線

#include <stdio.h>
#include <malloc.h>
#include<stdlib.h>
#define MAX 100
#define MAXNUM 10000000
int previous[MAX-1];// 求路徑需要
int pp[MAX-1];// 記錄最短路徑
typedef struct graphnode
{
int vexnum; //頂點
int arcnum; //弧
int gra[MAX][MAX]; //鄰接矩陣表示0或1
}Graph;
int dist[MAX]; // 最短距離
int arc[MAX][MAX]; // 權

int main()
{
void Dijkstra(Graph *g,int v);
int i,j,n,m;
int v; //源點
Graph *G;
G=(Graph *)malloc(sizeof(Graph));
printf("vexnum:\n");
scanf("%d",&G->vexnum);
printf("arcnum:\n");
scanf("%d",&G->arcnum);
printf("graph:\n");
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
{
scanf("%d",&G->gra[i][j]);
}
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
{
if(G->gra[i][j]==1)
{
printf("請輸入%d到%d的權值:",i,j);
scanf("%d",&arc[i][j]);//若有弧 則輸入i到j直接的權
}
else
arc[i][j]=MAXNUM;
}
printf("請輸入源點v的值:");
scanf("%d",&v);
Dijkstra(G,v);
printf("請輸入源點所要到達的點:\n");
scanf("%d",&n);
pp[0]=0;
i=1;
m=n;// 記錄n的值
while(n!=0)// 求0到其他點路徑
{
pp[i]=previous[n];
i++;
n=previous[n];

}
printf("Path:0 -> ");
for(j=G->vexnum-1;j>=0;j--)
if(pp[j]!=0)
printf(" %d -> ",pp[j]);
printf("%d\n",m);
return 0;

}
void Dijkstra(Graph *G,int v)
{
int previous[MAX-1];
int newdist;
bool sign[MAX];
if(v<0||v>MAX-1)
{
printf("該源點不存在!\n");
return;
}

for(int i=0;i<G->vexnum;i++) //初始化
{
dist[i]=arc[v][i];
sign[i]=false;
if(dist[i]==MAXNUM)
previous[i]=0;
else
previous[i]=v;
}

dist[v]=0;
sign[v]=true;

for(i=0;i<G->vexnum;i++) // i<n-1 待定
{
float temp=MAXNUM;
int u=v; //u 中間變數
for(int j=0;j<G->vexnum;j++)
if((!sign[j])&&(dist[j]<temp))
{
u=j;
temp=dist[j];
}
sign[u]=true;
for(j=0;j<G->vexnum;j++)
if((!sign[j])&&(arc[u][j]<MAXNUM))
{
newdist=dist[u]+arc[u][j];
if(newdist<dist[j])
{
dist[j]=newdist;
previous[j]=u;
}

}
}
for(i=0;i<G->vexnum;i++)
if(dist[i]!=MAXNUM)
printf("從%d到%d的最短路徑是 %d\n",v,i,dist[i]);
else
printf("從%d到%d無最短路徑\n",v,i);
printf("\n");

}
這是Dijkstra演算法求單源最短路徑演算法 上程序中 假定頂點從0開始,搜索整個圖,然後求出0到其他各點的最短距離,存放在dist數組中,main函數後面幾行是求0到其他各點的路徑 基本上能滿足你的要求了

2. C語言中的演算法,都涉及到哪些數學知識

正規知識系統是把凸輪包含在離散數學里的,一般是離散數學的最後一章。
演算法的設計還依賴一門重要的數學課:線性代數,主要是關於矩陣和方程組的運算方法。

當然,高等數學也很重要,因為高等數學的指導思想是以直代曲,是一種逼近思想,而計算機的邏輯原理恰恰也是 虛擬現實,就是以盡量高的精度逼近自然界中的准確值。

3. 求一個廣度優先演算法的實例及其C語言程序(L-dequeue)

#include <stdio.h>
#define max 100
typedef struct anode
{
int adjvex; //邊的終點位置
struct anode *nextarc;
}arcnode;

typedef struct node
{
int data;
arcnode *firstout;
}vnode;

typedef struct
{
vnode adjlist[max];
int n;
int e;
}Agraph;

static int visit[max];
//深度遍歷
void DFS(Agraph &G,int v) //v為初始頂點編號
{
int k;
arcnode *p;
for(k=0;k<G.n;k++)
visit[k]=0;
printf("%d ",v);
p=G.adjlist[v].firstout;
while(p)
{
if(!visit[p->adjvex])
DFS(G,p->adjvex);
p=p->nextarc;
}
}

void BFS(Agraph &G,int v)
{
arcnode *p;
int q[max];
int front=0;
int rear=0;
int w,i;
for(i=0;i<G.n;i++)
visit[i]=0;
printf("%d ",v);
visit[v]=1;
rear=(rear+1)%max;
q[rear]=v;
while(front!=rear)
{
front=(front+1)%max;
w=q[front];
p=G.adjlist[w].firstout;
while(p)
{
if(!visit[p->adjvex])
{
printf("%d ",p->adjvex);
visit[p->adjvex]=1;
rear=(rear+1)%max;
q[rear]=p->adjvex;
}
p=p->nextarc;
}
printf("\n");
}
}

//層序遍歷二叉樹
struct btnode
{
int data;
btnode *lchild,*rchild;
};

void level(struct btnode *bt)
{
if(!bt)
return;
btnode *q[max];
int front,rear;
front=0;
rear=0;
printf("%d ",bt->data);
rear=(rear+1)%max;
q[rear]=bt;
while(front!=rear)
{
front=(front+1)%max;
bt=q[front];
if(bt->lchild)
{
printf("%d ",bt->lchild->data);
rear=(rear+1)%max;
q[rear]=bt->lchild;
}
if(bt->rchild)
{
printf("%d ",bt->rchild->data);
rear=(rear+1)%max;
q[rear]=bt->rchild;
}

}
}

void DFS1(Agraph &G,int v)
{
arcnode *p;
printf("%d ",v);
visit[v]=1;
p=G.adjlist[v].firstout;
while(p)
{
if(!visit[p->adjvex])
{
DFS1(G,p->adjvex);
}
p=p->nextarc;
}
}

void level1(struct btnode *bt)
{
if(!bt)
return;
printf("%d ",bt->data);
struct btnode *q[max];
int front=0;
int rear=0;
rear=(rear+1)%max;
q[rear]=bt;
while(front!=rear)
{
front=(front+1)%max;
bt=q[front];
if(bt->lchild)
{
printf("%d ",bt->lchild->data);
rear=(rear+1)%max;
q[rear]=bt->lchild;

}
if(bt->rchild)
{
printf("%d ",bt->rchild->data);
rear=(rear+1)%max;
q[rear]=bt->rchild;
}
}
}

void BFS1(Agraph &G,int v)
{
int q[max];
int front=0;
int rear=0;
int i;
for(i=0;i<G.n;i++)
visit[i]=0;
printf("%d ",v);
visit[v]=1;
rear=(rear+1)%max;
q[rear]=v;
arcnode *p;

while(front!=rear)
{
front=(front+1)%max;
i=q[front];
p=G.adjlist[i].firstout;
while(p)
{
if(!visit[p->adjvex])
{
printf("%d ",p->adjvex);
visit[p->adjvex]=1;
rear=(rear+1)%max;
q[rear]=p->adjvex;
}
p=p->nextarc;

}
}
}

4. c語言演算法

離散數學離散數學作為計算機學科的基礎是競賽中涉及最多的數學分支,重中之重又在於圖論和組合數學,尤其是圖論。圖論之所以運用最多是因為它的變化最多,而且可以輕易地結合基本數據結構和許多演算法的基本思想,較多用到的知識包括連通性判斷、DFS和BFS,關節點和關鍵路徑、歐拉迴路、最小生成樹、最短路徑、二部圖匹配和網路流等等。雖然這部分的比重很大,但是往往也是競賽中的難題所在,如果有初學者對於這部分的某些具體內容暫時感到力不從心,也不必著急,可以慢慢積累。組合數學競賽中設計的組合計數問題大都需要用組合數學來解決,組合數學中的知識相比於圖論要簡單一些,很多知識對於小學上過奧校的同學來說已經十分熟悉,但是也有一些部分需要先對代數結構中的群論有初步了解才能進行學習。組合數學在競賽中很少以難題的形式出現,但是如果積累不夠,任何一道這方面的題目卻都有可能成為難題。數論以素數判斷和同餘為模型構造出來的題目往往需要較多的數論知識來解決,這部分在競賽中的比重並不大,但只要來上一道,也足以使知識不足的人冥思苦想上一陣時間。素數判斷和同餘最常見的是在以密碼學為背景的題目中出現,在運用密碼學常識確定大概的過程之後,核心演算法往往要涉及數論的內容。計算幾何計算幾何相比於其它部分來說是比較獨立的,就是說它和其它的知識點很少有過多的結合,較常用到的部分包括—線段相交的判斷、多邊形面積的計算、內點外點的判斷、凸包等等。計算幾何的題目難度不會很大,但也永遠不會成為最弱的題。線性代數對線性代數的應用都是圍繞矩陣展開的,一些表面上是模擬的題目往往可以藉助於矩陣來找到更好的演算法。 ~

5. C語言演算法求解:對任意給定的網路(頂點數和邊數自定),設計演算法生成它的最小生成樹。

上一個圖和代碼:

圖1為要創建的圖,圖2為對應的最小生成樹

代碼為:

//圖論之最小生成樹:prim演算法實現

#include"stdio.h"

#include"malloc.h"

//聲明

voidoutput(structadjacentlisthead*alh,intmapsize);

structadjacentlistson//鄰接表項結構體

{

intsonelement;

intquanvalue;

structadjacentlistson*next;

};

structadjacentlisthead//鄰接表頭結構體

{

charflag;

intelement;

intcurqvalue;

structadjacentlisthead*previous;

structadjacentlistson*startson;

};

structadjacentlisthead*mapinitialnize(intmapsize)//初始化圖函數

{

structadjacentlisthead*alh=NULL;

structadjacentlistson*newnode=NULL;

inti,x,y,z;

alh=malloc(sizeof(structadjacentlisthead)*mapsize);

if(alh==NULL)

returnNULL;

for(i=0;i<mapsize;i++)

{

alh[i].flag=0;

alh[i].element=i+1;

alh[i].curqvalue=0;

alh[i].previous=NULL;

alh[i].startson=NULL;

}

scanf("%d%d%d",&x,&y,&z);

while(x&&y)//直到輸入的x,y中至少有一個0為止

{

newnode=malloc(sizeof(structadjacentlistson));

newnode->sonelement=y;

newnode->quanvalue=z;

newnode->next=alh[x-1].startson;

alh[x-1].startson=newnode;

scanf("%d%d%d",&x,&y,&z);

}

returnalh;

}

intfindminnode(structadjacentlisthead*alh,intmapsize)//找到最小權值對應的結點

{

inti,minvalue=~(1<<(sizeof(int)*8-1)),minplace=0;

for(i=0;i<mapsize;i++)

{

if(alh[i].flag==0&&alh[i].curqvalue!=0)

{

if(alh[i].curqvalue<minvalue)

{

minvalue=alh[i].curqvalue;

minplace=i+1;//

}

}

}

returnminplace;

}

voidfindthemintree(structadjacentlisthead*alh,intstartplace,intmapsize)//找到最小生成樹

{

structadjacentlistson*alstmp=NULL;

intcurplace=startplace;

while(curplace)

{

alstmp=alh[curplace-1].startson;

alh[curplace-1].flag=1;//正在訪問

while(alstmp)

{

if(alh[alstmp->sonelement-1].flag==0)

{

if(alh[alstmp->sonelement-1].curqvalue==0||(alh[alstmp->sonelement-1].curqvalue>alstmp->quanvalue))//比較方法與有權圖有一點不同

{

alh[alstmp->sonelement-1].curqvalue=alstmp->quanvalue;

alh[alstmp->sonelement-1].previous=&alh[curplace-1];

}

}

alstmp=alstmp->next;

}

curplace=findminnode(alh,mapsize);//通過函數找到最小的一個結點

}

}

voidoutput(structadjacentlisthead*alh,intmapsize)

{

inti;

for(i=0;i<mapsize;i++)

{

printf("%d點的權值為:%d ",i+1,alh[i].curqvalue);

}

printf("................................................... ");

}

intmain()

{

structadjacentlisthead*alh=NULL;

intmapsize=7,startplace=1;

alh=mapinitialnize(mapsize);

findthemintree(alh,startplace,mapsize);

output(alh,mapsize);

}

輸入數據對應第一圖:

122

134

141

212

243

2510

314

342

365

411

423

432

457

468

474

5210

547

576

635

648

671

744

756

761

000

6. 求解:圖論中常見的最短路徑演算法有幾種都是什麼

主要是有三種、、

第一種是最直接的貪心dijkstra演算法、、可以利用堆數據結構進行優化、、缺點就是不能求有負權的最短路與判斷負環、、

第二種是bellman-ford演算法、、根據鬆弛操作的性質是可以來判斷負環的、、時間復雜度是O(nm)的、、

第三種是SPFA演算法、、把他單獨拿出來作為一種演算法並不是非常好的、、他的實質應該是上面的bellman-ford演算法的隊列優化時間復雜度更低、O(KE)、K的值約等於2、、

7. 本科獨立用C語言完成沒有優化的C語言編譯器屬於什麼水平

我覺得水平還是很高的,但意義恐怕不大。編譯器技術是非常成熟的領域,而且由於應用場景的限
制實時,復雜的演算法已經自動出局了,你可選的東西是有限的。編譯器可能有很多實現的形
式,虛擬機/解釋器/靜態編譯器 等,也有成熟的開源實現。作為本科生,而非專門研究該分支的學生,應該合理分配自己學習的時間,如果做這個編譯器就干
掉了大半年,那計網和OS這些課程該咋辦? 

我知道很多人會認為沒有做編譯器優化特指中段優化,不考慮機器碼上的優化比較劃水。但編
譯器優化是一個很復雜的東西:首先它和你用的IR表示有關而且是強烈耦合,SSA IR基本還
好,有開源代碼和文獻記載,你想要的都能在網上挖到但這怎麼體現你的水平是吧。你
要考慮編譯器的性能,盡管編譯器的後端優化基本上可以納入到某種PEabstract interpretation的
范疇中。

要不然你可以通過編寫插件的方式白嫖例如visual studio code這類軟
件的強大編輯功能,如果你寫的不是c compiler,你也可以盡量把語法設計得很像c,這樣你又能進一步
白嫖其強大的intellisense code,當然仍然有不少人或者應該說團隊達到了這一步,到這里,應該卷死
了99.99%的同行應該毫無問題。

8. C語言演算法有哪些 並舉例和分析

演算法大全(C,C++)
一、 數論演算法

1.求兩數的最大公約數
function gcd(a,b:integer):integer;
begin
if b=0 then gcd:=a
else gcd:=gcd (b,a mod b);
end ;

2.求兩數的最小公倍數
function lcm(a,b:integer):integer;
begin
if a<b then swap(a,b);
lcm:=a;
while lcm mod b>0 do inc(lcm,a);
end;

3.素數的求法
A.小范圍內判斷一個數是否為質數:
function prime (n: integer): Boolean;
var I: integer;
begin
for I:=2 to trunc(sqrt(n)) do
if n mod I=0 then begin
prime:=false; exit;
end;
prime:=true;
end;

B.判斷longint范圍內的數是否為素數(包含求50000以內的素數表):
procere getprime;
var
i,j:longint;
p:array[1..50000] of boolean;
begin
fillchar(p,sizeof(p),true);
p[1]:=false;
i:=2;
while i<50000 do begin
if p[i] then begin
j:=i*2;
while j<50000 do begin
p[j]:=false;
inc(j,i);
end;
end;
inc(i);
end;
l:=0;
for i:=1 to 50000 do
if p[i] then begin
inc(l);pr[l]:=i;
end;
end;{getprime}

function prime(x:longint):integer;
var i:integer;
begin
prime:=false;
for i:=1 to l do
if pr[i]>=x then break
else if x mod pr[i]=0 then exit;
prime:=true;
end;{prime}

二、圖論演算法

1.最小生成樹

A.Prim演算法:

procere prim(v0:integer);
var
lowcost,closest:array[1..maxn] of integer;
i,j,k,min:integer;
begin
for i:=1 to n do begin
lowcost[i]:=cost[v0,i];
closest[i]:=v0;
end;
for i:=1 to n-1 do begin
{尋找離生成樹最近的未加入頂點k}
min:=maxlongint;
for j:=1 to n do
if (lowcost[j]<min) and (lowcost[j]<>0) then begin
min:=lowcost[j];
k:=j;
end;
lowcost[k]:=0; {將頂點k加入生成樹}
{生成樹中增加一條新的邊k到closest[k]}
{修正各點的lowcost和closest值}
for j:=1 to n do
if cost[k,j]<lwocost[j] then begin
lowcost[j]:=cost[k,j];
closest[j]:=k;
end;
end;
end;{prim}

B.Kruskal演算法:(貪心)

按權值遞增順序刪去圖中的邊,若不形成迴路則將此邊加入最小生成樹。
function find(v:integer):integer; {返回頂點v所在的集合}
var i:integer;
begin
i:=1;
while (i<=n) and (not v in vset[i]) do inc(i);
if i<=n then find:=i else find:=0;
end;

procere kruskal;
var
tot,i,j:integer;
begin
for i:=1 to n do vset[i]:=[i];{初始化定義n個集合,第I個集合包含一個元素I}
p:=n-1; q:=1; tot:=0; {p為尚待加入的邊數,q為邊集指針}
sort;
{對所有邊按權值遞增排序,存於e[I]中,e[I].v1與e[I].v2為邊I所連接的兩個頂點的序號,e[I].len為第I條邊的長度}
while p>0 do begin
i:=find(e[q].v1);j:=find(e[q].v2);
if i<>j then begin
inc(tot,e[q].len);
vset[i]:=vset[i]+vset[j];vset[j]:=[];
dec(p);
end;
inc(q);
end;
writeln(tot);
end;

2.最短路徑

A.標號法求解單源點最短路徑:
var
a:array[1..maxn,1..maxn] of integer;
b:array[1..maxn] of integer; {b[i]指頂點i到源點的最短路徑}
mark:array[1..maxn] of boolean;

procere bhf;
var
best,best_j:integer;
begin
fillchar(mark,sizeof(mark),false);
mark[1]:=true; b[1]:=0;{1為源點}
repeat
best:=0;
for i:=1 to n do
If mark[i] then {對每一個已計算出最短路徑的點}
for j:=1 to n do
if (not mark[j]) and (a[i,j]>0) then
if (best=0) or (b[i]+a[i,j]<best) then begin
best:=b[i]+a[i,j]; best_j:=j;
end;
if best>0 then begin
b[best_j]:=best;mark[best_j]:=true;
end;
until best=0;
end;{bhf}

B.Floyed演算法求解所有頂點對之間的最短路徑:
procere floyed;
begin
for I:=1 to n do
for j:=1 to n do
if a[I,j]>0 then p[I,j]:=I else p[I,j]:=0; {p[I,j]表示I到j的最短路徑上j的前驅結點}
for k:=1 to n do {枚舉中間結點}
for i:=1 to n do
for j:=1 to n do
if a[i,k]+a[j,k]<a[i,j] then begin
a[i,j]:=a[i,k]+a[k,j];
p[I,j]:=p[k,j];
end;
end;

C. Dijkstra 演算法:

var
a:array[1..maxn,1..maxn] of integer;
b,pre:array[1..maxn] of integer; {pre[i]指最短路徑上I的前驅結點}
mark:array[1..maxn] of boolean;
procere dijkstra(v0:integer);
begin
fillchar(mark,sizeof(mark),false);
for i:=1 to n do begin
d[i]:=a[v0,i];
if d[i]<>0 then pre[i]:=v0 else pre[i]:=0;
end;
mark[v0]:=true;
repeat {每循環一次加入一個離1集合最近的結點並調整其他結點的參數}
min:=maxint; u:=0; {u記錄離1集合最近的結點}
for i:=1 to n do
if (not mark[i]) and (d[i]<min) then begin
u:=i; min:=d[i];
end;
if u<>0 then begin
mark[u]:=true;
for i:=1 to n do
if (not mark[i]) and (a[u,i]+d[u]<d[i]) then begin
d[i]:=a[u,i]+d[u];
pre[i]:=u;
end;
end;
until u=0;
end;

3.計算圖的傳遞閉包

Procere Longlink;
Var
T:array[1..maxn,1..maxn] of boolean;
Begin
Fillchar(t,sizeof(t),false);
For k:=1 to n do
For I:=1 to n do
For j:=1 to n do T[I,j]:=t[I,j] or (t[I,k] and t[k,j]);
End;

4.無向圖的連通分量

A.深度優先
procere dfs ( now,color: integer);
begin
for i:=1 to n do
if a[now,i] and c[i]=0 then begin {對結點I染色}
c[i]:=color;
dfs(I,color);
end;
end;

B 寬度優先(種子染色法)

5.關鍵路徑

幾個定義: 頂點1為源點,n為匯點。
a. 頂點事件最早發生時間Ve[j], Ve [j] = max{ Ve [j] + w[I,j] },其中Ve (1) = 0;
b. 頂點事件最晚發生時間 Vl[j], Vl [j] = min{ Vl[j] – w[I,j] },其中 Vl(n) = Ve(n);
c. 邊活動最早開始時間 Ee[I], 若邊I由<j,k>表示,則Ee[I] = Ve[j];
d. 邊活動最晚開始時間 El[I], 若邊I由<j,k>表示,則El[I] = Vl[k] – w[j,k];
若 Ee[j] = El[j] ,則活動j為關鍵活動,由關鍵活動組成的路徑為關鍵路徑。
求解方法:
a. 從源點起topsort,判斷是否有迴路並計算Ve;
b. 從匯點起topsort,求Vl;
c. 算Ee 和 El;

6.拓撲排序

找入度為0的點,刪去與其相連的所有邊,不斷重復這一過程。
例 尋找一數列,其中任意連續p項之和為正,任意q 項之和為負,若不存在則輸出NO.

7.迴路問題

Euler迴路(DFS)
定義:經過圖的每條邊僅一次的迴路。(充要條件:圖連同且無奇點)

Hamilton迴路
定義:經過圖的每個頂點僅一次的迴路。

一筆畫
充要條件:圖連通且奇點個數為0個或2個。

9.判斷圖中是否有負權迴路 Bellman-ford 演算法

x[I],y[I],t[I]分別表示第I條邊的起點,終點和權。共n個結點和m條邊。
procere bellman-ford
begin
for I:=0 to n-1 do d[I]:=+infinitive;
d[0]:=0;
for I:=1 to n-1 do
for j:=1 to m do {枚舉每一條邊}
if d[x[j]]+t[j]<d[y[j]] then d[y[j]]:=d[x[j]]+t[j];
for I:=1 to m do
if d[x[j]]+t[j]<d[y[j]] then return false else return true;
end;

10.第n最短路徑問題

*第二最短路徑:每舉最短路徑上的每條邊,每次刪除一條,然後求新圖的最短路徑,取這些路徑中最短的一條即為第二最短路徑。
*同理,第n最短路徑可在求解第n-1最短路徑的基礎上求解。

三、背包問題

*部分背包問題可有貪心法求解:計算Pi/Wi
數據結構:
w[i]:第i個背包的重量;
p[i]:第i個背包的價值;

1.0-1背包: 每個背包只能使用一次或有限次(可轉化為一次):

A.求最多可放入的重量。
NOIP2001 裝箱問題
有一個箱子容量為v(正整數,o≤v≤20000),同時有n個物品(o≤n≤30),每個物品有一個體積 (正整數)。要求從 n 個物品中,任取若千個裝入箱內,使箱子的剩餘空間為最小。
l 搜索方法
procere search(k,v:integer); {搜索第k個物品,剩餘空間為v}
var i,j:integer;
begin
if v<best then best:=v;
if v-(s[n]-s[k-1])>=best then exit; {s[n]為前n個物品的重量和}
if k<=n then begin
if v>w[k] then search(k+1,v-w[k]);
search(k+1,v);
end;
end;

l DP
F[I,j]為前i個物品中選擇若干個放入使其體積正好為j的標志,為布爾型。
實現:將最優化問題轉化為判定性問題
f [I, j] = f [ i-1, j-w[i] ] (w[I]<=j<=v) 邊界:f[0,0]:=true.
For I:=1 to n do
For j:=w[I] to v do F[I,j]:=f[I-1,j-w[I]];
優化:當前狀態只與前一階段狀態有關,可降至一維。
F[0]:=true;
For I:=1 to n do begin
F1:=f;
For j:=w[I] to v do
If f[j-w[I]] then f1[j]:=true;
F:=f1;
End;

B.求可以放入的最大價值。
F[I,j] 為容量為I時取前j個背包所能獲得的最大價值。
F [i,j] = max { f [ i – w [ j ], j-1] + p [ j ], f[ i,j-1] }

C.求恰好裝滿的情況數。
DP:
Procere update;
var j,k:integer;
begin
c:=a;
for j:=0 to n do
if a[j]>0 then
if j+now<=n then inc(c[j+now],a[j]);
a:=c;
end;

2.可重復背包

A求最多可放入的重量。
F[I,j]為前i個物品中選擇若干個放入使其體積正好為j的標志,為布爾型。
狀態轉移方程為
f[I,j] = f [ I-1, j – w[I]*k ] (k=1.. j div w[I])

B.求可以放入的最大價值。
USACO 1.2 Score Inflation
進行一次競賽,總時間T固定,有若干種可選擇的題目,每種題目可選入的數量不限,每種題目有一個ti(解答此題所需的時間)和一個si(解答此題所得的分數),現要選擇若干題目,使解這些題的總時間在T以內的前提下,所得的總分最大,求最大的得分。
*易想到:
f[i,j] = max { f [i- k*w[j], j-1] + k*p[j] } (0<=k<= i div w[j])
其中f[i,j]表示容量為i時取前j種背包所能達到的最大值。
*實現:
Begin
FillChar(f,SizeOf(f),0);
For i:=1 To M Do
For j:=1 To N Do
If i-problem[j].time>=0 Then
Begin
t:=problem[j].point+f[i-problem[j].time];
If t>f[i] Then f[i]:=t;
End;
Writeln(f[M]);
End.

C.求恰好裝滿的情況數。
Ahoi2001 Problem2
求自然數n本質不同的質數和的表達式的數目。
思路一,生成每個質數的系數的排列,在一一測試,這是通法。
procere try(dep:integer);
var i,j:integer;
begin
cal; {此過程計算當前系數的計算結果,now為結果}
if now>n then exit; {剪枝}
if dep=l+1 then begin {生成所有系數}
cal;
if now=n then inc(tot);
exit;
end;
for i:=0 to n div pr[dep] do begin
xs[dep]:=i;
try(dep+1);
xs[dep]:=0;
end;
end;

思路二,遞歸搜索效率較高
procere try(dep,rest:integer);
var i,j,x:integer;
begin
if (rest<=0) or (dep=l+1) then begin
if rest=0 then inc(tot);
exit;
end;
for i:=0 to rest div pr[dep] do
try(dep+1,rest-pr[dep]*i);
end;
{main: try(1,n); }

思路三:可使用動態規劃求解
USACO1.2 money system
V個物品,背包容量為n,求放法總數。
轉移方程:

Procere update;
var j,k:integer;
begin
c:=a;
for j:=0 to n do
if a[j]>0 then
for k:=1 to n div now do
if j+now*k<=n then inc(c[j+now*k],a[j]);
a:=c;
end;
{main}
begin
read(now); {讀入第一個物品的重量}
i:=0; {a[i]為背包容量為i時的放法總數}
while i<=n do begin
a[i]:=1; inc(i,now); end; {定義第一個物品重的整數倍的重量a值為1,作為初值}
for i:=2 to v do
begin
read(now);
update; {動態更新}
end;
writeln(a[n]);

四、排序演算法

A.快速排序:

procere qsort(l,r:integer);
var i,j,mid:integer;
begin
i:=l;j:=r; mid:=a[(l+r) div 2]; {將當前序列在中間位置的數定義為中間數}
repeat
while a[i]<mid do inc(i); {在左半部分尋找比中間數大的數}
while a[j]>mid do dec(j);{在右半部分尋找比中間數小的數}
if i<=j then begin {若找到一組與排序目標不一致的數對則交換它們}
swap(a[i],a[j]);
inc(i);dec(j); {繼續找}
end;
until i>j;
if l<j then qsort(l,j); {若未到兩個數的邊界,則遞歸搜索左右區間}
if i<r then qsort(i,r);
end;{sort}

B.插入排序:

思路:當前a[1]..a[i-1]已排好序了,現要插入a[i]使a[1]..a[i]有序。
procere insert_sort;
var i,j:integer;
begin
for i:=2 to n do begin
a[0]:=a[i];
j:=i-1;
while a[0]<a[j] do begin
a[j+1]:=a[j];
j:=j-1;
end;
a[j+1]:=a[0];
end;
end;{inset_sort}

C.選擇排序:
procere sort;
var i,j,k:integer;
begin
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then swap(a[i],a[j]);
end;

D. 冒泡排序
procere bubble_sort;
var i,j,k:integer;
begin
for i:=1 to n-1 do
for j:=n downto i+1 do
if a[j]<a[j-1] then swap( a[j],a[j-1]); {每次比較相鄰元素的關系}
end;

E.堆排序:
procere sift(i,m:integer);{調整以i為根的子樹成為堆,m為結點總數}
var k:integer;
begin
a[0]:=a[i]; k:=2*i;{在完全二叉樹中結點i的左孩子為2*i,右孩子為2*i+1}
while k<=m do begin
if (k<m) and (a[k]<a[k+1]) then inc(k);{找出a[k]與a[k+1]中較大值}
if a[0]<a[k] then begin a[i]:=a[k];i:=k;k:=2*i; end
else k:=m+1;
end;
a[i]:=a[0]; {將根放在合適的位置}
end;

procere heapsort;
var
j:integer;
begin
for j:=n div 2 downto 1 do sift(j,n);
for j:=n downto 2 do begin
swap(a[1],a[j]);
sift(1,j-1);
end;

熱點內容
21款發現5哪個配置透明引擎蓋 發布:2024-04-23 21:20:18 瀏覽:871
adb拷文件夾 發布:2024-04-23 21:12:25 瀏覽:8
如何破解wmv加密視頻 發布:2024-04-23 21:11:44 瀏覽:556
安卓拍抖音怎麼把聲音錄進去 發布:2024-04-23 20:58:51 瀏覽:846
安卓fifa15怎麼恢復數據 發布:2024-04-23 20:58:42 瀏覽:242
卸載oracle資料庫 發布:2024-04-23 20:36:26 瀏覽:521
發帖站源碼 發布:2024-04-23 20:22:45 瀏覽:682
小豚攝像頭存儲卡滿了怎麼辦 發布:2024-04-23 20:04:41 瀏覽:232
我的世界伺服器管理應該做什麼 發布:2024-04-23 19:37:19 瀏覽:146
伺服器是如何處理多個用戶的請求 發布:2024-04-23 19:35:26 瀏覽:957