NOIP复赛复习6程序对拍与图论模板Word文档格式.docx

上传人:b****6 文档编号:22134746 上传时间:2023-02-02 格式:DOCX 页数:16 大小:17.22KB
下载 相关 举报
NOIP复赛复习6程序对拍与图论模板Word文档格式.docx_第1页
第1页 / 共16页
NOIP复赛复习6程序对拍与图论模板Word文档格式.docx_第2页
第2页 / 共16页
NOIP复赛复习6程序对拍与图论模板Word文档格式.docx_第3页
第3页 / 共16页
NOIP复赛复习6程序对拍与图论模板Word文档格式.docx_第4页
第4页 / 共16页
NOIP复赛复习6程序对拍与图论模板Word文档格式.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

NOIP复赛复习6程序对拍与图论模板Word文档格式.docx

《NOIP复赛复习6程序对拍与图论模板Word文档格式.docx》由会员分享,可在线阅读,更多相关《NOIP复赛复习6程序对拍与图论模板Word文档格式.docx(16页珍藏版)》请在冰豆网上搜索。

NOIP复赛复习6程序对拍与图论模板Word文档格式.docx

Q.empty())

{

intfront=Q.front();

cout<

<

front<

"

"

;

Q.pop();

for(inti=1;

i<

=N;

i++)

if(!

visited[i]&

&

maze[front-1][i-1]==1)

visited[i]=1;

Q.push(i);

}

}

intmain()

if(visited[i]==1)

continue;

BFS(i);

return0;

2、图的遍历-DFS

stack>

{0,0,1,0,1},

{0,0,1,0,0},

{1,1,0,0,1},

{0,0,1,0,0}

voidDFS(intstart)

stack<

s;

s.push(start);

boolis_push=false;

s.empty())

is_push=false;

intv=s.top();

 

if(maze[v-1][i-1]==1&

!

visited[i])

s.push(i);

is_push=true;

break;

is_push)

cout<

v<

s.pop();

DFS(i);

3、最小生成树-Kruskal

cstdio>

using 

namespace 

std;

const 

int 

MAXN 

30;

pa[MAXN];

rank[MAXN];

n,sum;

struct 

node{ 

x,y;

w;

}edge[MAXN*MAXN];

bool 

cmp(node 

p,node 

q){ 

return 

p.w<

q.w;

void 

make_set(int 

x) 

pa[x] 

x;

rank[x] 

0;

find_set(int 

if(x 

!

pa[x]) 

find_set(pa[x]);

pa[x];

union_set(int 

x, 

y,int 

w) 

find_set(x);

find_set(y);

== 

y)return 

if(rank[x] 

>

rank[y])

pa[y] 

else 

y;

rank[y]) 

rank[y]++;

sum+=w;

main() 

// 

freopen("

input.txt"

"

r"

stdin);

while(cin>

n){ 

if(!

n) 

break;

char 

ch;

m,k=0;

for 

(int 

1;

i++) 

cin 

ch 

m;

j++) 

edge[k].w;

edge[k].x 

i;

edge[k].y 

'

A'

k++;

sort(edge,edge+k,cmp);

for(int 

i=0;

i<

MAXN;

make_set(i);

sum=0;

k;

union_set(edge[i].x,edge[i].y,edge[i].w);

cout<

sum<

endl;

4、最小生成树-Prim

maxn=30;

INF=1000000;

graph[maxn][maxn];

lowcost[maxn],closet[maxn];

visited[maxn];

n;

createGraph(){ 

memset(graph,0,sizeof(graph));

memset(lowcost,0,sizeof(lowcost));

memset(closet,0,sizeof(closet));

memset(visited,0,sizeof(visited));

a;

j=0;

j<

j++){ 

scanf("

%d"

&

a);

if(a==0) 

graph[i][j]=graph[j][i]=INF;

graph[i][j]=graph[j][i]=a;

prim(){ 

visited[0]=1;

i++){ 

lowcost[i]=graph[i][0];

closet[i]=0;

i=1;

minn=lowcost[0],k=0;

visited[j] 

lowcost[j]<

minn){ 

minn=lowcost[j];

k=j;

sum+=minn;

visited[k]=1;

t=0;

t<

t++){ 

visited[t] 

lowcost[t]>

graph[t][k]){ 

lowcost[t]=graph[t][k];

closet[t]=k;

printf("

%d\n"

sum);

while(~scanf("

n)){ 

createGraph();

prim();

5、最短路径-SPFA

cstdlib>

constintmaxn=100005;

structdqs

intf,t,c;

}hh[maxn];

inttot=0,first[maxn],next[maxn],d[maxn];

boolused[maxn];

voidbuild(intf,intt,intc)

hh[++tot]=(dqs){f,t,c};

next[tot]=first[f];

first[f]=tot;

queue<

q;

intn,m,s,e;

voidspfa(ints)

d[s]=0;

q.push(s);

used[s]=1;

while(!

q.empty())

intx=q.front();

q.pop();

used[x]=0;

for(inti=first[x];

i=next[i])

intu=hh[i].t;

if(d[u]>

d[x]+hh[i].c)

d[u]=d[x]+hh[i].c;

if(!

used[u])

q.push(u);

used[u]=1;

%d%d%d%d"

n,&

m,&

s,&

e);

for(inti=1;

=n;

i++)

d[i]=1e9;

=m;

%d%d%d"

f,&

t,&

c);

build(f,t,c);

build(t,f,c);

spfa(s);

printf("

d[e]);

6、最短路径-Dijkstra

voidDijkstra()

while(true)

intx=-1;

used[i])

if(x==-1||d[i]<

d[x])

x=i;

if(x==-1)break;

used[x]=1;

......

7、最短路径-Floyd

constintmaxn=1005;

intd[maxn][maxn];

voidfloyd()

for(intk=1;

k<

k++)

for(intj=1;

j++)

if(i!

=j&

i!

=k&

k!

=j)

d[i][j]=min(d[i][j],d[i][k]+d[k][j]);

d[i][j]=1e9;

d[f][t]=c;

d[t][f]=c;

floyd();

d[s][e]);

8、最近公共祖先(LCA)-倍增

algorithm>

cstring>

constintmaxn=250010;

}hh[maxn<

1];

inttot=0,fa[maxn][31],next[maxn],first[maxn],f[maxn],d[maxn];

voidbuild(intff,inttt,intcc)

hh[++tot]=(dqs){ff,tt,cc};

next[tot]=first[ff];

first[ff]=tot;

intdeep[maxn];

voiddfs(intx,intsd)

deep[x]=sd;

intu;

u=hh[i].t;

deep[u]&

u)

f[u]=x;

dfs(u,sd+1);

intlca(intx,inty)

if(deep[x]<

deep[y])

swap(x,y);

intdeepcha=deep[x]-deep[y];

for(inti=0;

=30;

if(1<

i&

deepcha)

x=fa[x][i];

for(inti=30;

i>

=0;

i--)

if(fa[x][i]!

=fa[y][i])

y=fa[y][i];

if(x!

=y)

returnf[x];

returnx;

intn;

scanf("

n);

intu,v,c;

u,&

v,&

build(u,v,c);

build(v,u,c);

dfs(0,0);

fa[i][0]=f[i];

=20;

fa[i][j]=fa[fa[i][j-1]][j-1];

intm;

m);

%d%d"

v);

intxx=lca(u,v);

d[u]+d[v]-2*d[xx]);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 成考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1