首页 > 代码库 > Dijkstra----------
Dijkstra----------
hnldyhy(303882171) 11:00:25
// Dijkstra 邻接矩阵的下标从1开始
#include <stdio.h>
#define N 201
#define INF 1000000000
int g[N][N],bz[N],dist[N],s,t,n;
void Dijkstra(int s)
{ int i,j,min,k,x;
for (i=1;i<=n;i++)
{ dist[i]=g[s][i]; bz[i]=0; }
bz[s]=1;
for (i=1;i<=n;i++)
{ printf("dist[]\n");
for (x=1;x<=n;x++)
printf("%d ",dist[x]); printf("\n");
printf("bz[]\n");
for (x=1;x<=n;x++)
printf("%d ",bz[x]); printf("\n");
min=INF;
for (k=1;k<=n;k++)
if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
bz[j]=1;
for (k=1;k<=n;k++)
if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
}
}
int main()
{ int i,j;
scanf("%d",&n);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&g[i][j]);
scanf("%d",&s);
Dijkstra(s);
for (i=1; i<=n; i++) printf("%d ",dist[i]);
printf("\n");
}
/*
5
0 10 200 11 200
10 0 13 18 200
200 13 0 12 15
11 18 12 0 16
200 200 15 16 0
1
*/
// Dijkstra 邻接矩阵的下标从1开始
#include <stdio.h>
#define N 201
#define INF 1000000000
int g[N][N],bz[N],dist[N],s,t,n;
void Dijkstra(int s)
{ int i,j,min,k,x;
for (i=1;i<=n;i++)
{ dist[i]=g[s][i]; bz[i]=0; }
bz[s]=1;
for (i=1;i<=n;i++)
{ printf("dist[]\n");
for (x=1;x<=n;x++)
printf("%d ",dist[x]); printf("\n");
printf("bz[]\n");
for (x=1;x<=n;x++)
printf("%d ",bz[x]); printf("\n");
min=INF;
for (k=1;k<=n;k++)
if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
bz[j]=1;
for (k=1;k<=n;k++)
if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
}
}
int main()
{ int i,j;
scanf("%d",&n);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&g[i][j]);
scanf("%d",&s);
Dijkstra(s);
for (i=1; i<=n; i++) printf("%d ",dist[i]);
printf("\n");
}
/*
5
0 10 200 11 200
10 0 13 18 200
200 13 0 12 15
11 18 12 0 16
200 200 15 16 0
1
*/
// Dijkstra 邻接矩阵的下标从1开始
#include <stdio.h>
#define N 201
#define INF 1000000000
int g[N][N],bz[N],dist[N],s,t,n;
void Dijkstra(int s)
{ int i,j,min,k,x;
for (i=1;i<=n;i++)
{ dist[i]=g[s][i]; bz[i]=0; }
bz[s]=1;
for (i=1;i<=n;i++)
{ printf("dist[]\n");
for (x=1;x<=n;x++)
printf("%d ",dist[x]); printf("\n");
printf("bz[]\n");
for (x=1;x<=n;x++)
printf("%d ",bz[x]); printf("\n");
min=INF;
for (k=1;k<=n;k++)
if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
bz[j]=1;
for (k=1;k<=n;k++)
if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
}
}
int main()
{ int i,j;
scanf("%d",&n);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&g[i][j]);
scanf("%d",&s);
Dijkstra(s);
for (i=1; i<=n; i++) printf("%d ",dist[i]);
printf("\n");
}
/*
5
0 10 200 11 200
10 0 13 18 200
200 13 0 12 15
11 18 12 0 16
200 200 15 16 0
1
*/
// Dijkstra 邻接矩阵的下标从1开始
#include <stdio.h>
#define N 201
#define INF 1000000000
int g[N][N],bz[N],dist[N],s,t,n;
void Dijkstra(int s)
{ int i,j,min,k,x;
for (i=1;i<=n;i++)
{ dist[i]=g[s][i]; bz[i]=0; }
bz[s]=1;
for (i=1;i<=n;i++)
{ printf("dist[]\n");
for (x=1;x<=n;x++)
printf("%d ",dist[x]); printf("\n");
printf("bz[]\n");
for (x=1;x<=n;x++)
printf("%d ",bz[x]); printf("\n");
min=INF;
for (k=1;k<=n;k++)
if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
bz[j]=1;
for (k=1;k<=n;k++)
if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
}
}
int main()
{ int i,j;
scanf("%d",&n);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&g[i][j]);
scanf("%d",&s);
Dijkstra(s);
for (i=1; i<=n; i++) printf("%d ",dist[i]);
printf("\n");
}
/*
5
0 10 200 11 200
10 0 13 18 200
200 13 0 12 15
11 18 12 0 16
200 200 15 16 0
1
*/
// Dijkstra 邻接矩阵的下标从1开始
#include <stdio.h>
#define N 201
#define INF 1000000000
int g[N][N],bz[N],dist[N],s,t,n;
void Dijkstra(int s)
{ int i,j,min,k,x;
for (i=1;i<=n;i++)
{ dist[i]=g[s][i]; bz[i]=0; }
bz[s]=1;
for (i=1;i<=n;i++)
{ printf("dist[]\n");
for (x=1;x<=n;x++)
printf("%d ",dist[x]); printf("\n");
printf("bz[]\n");
for (x=1;x<=n;x++)
printf("%d ",bz[x]); printf("\n");
min=INF;
for (k=1;k<=n;k++)
if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
bz[j]=1;
for (k=1;k<=n;k++)
if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
}
}
int main()
{ int i,j;
scanf("%d",&n);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&g[i][j]);
scanf("%d",&s);
Dijkstra(s);
for (i=1; i<=n; i++) printf("%d ",dist[i]);
printf("\n");
}
/*
5
0 10 200 11 200
10 0 13 18 200
200 13 0 12 15
11 18 12 0 16
200 200 15 16 0
1
*/
hnldyhy(303882171) 11:02:32
/*最小生成树,用Prim算法,邻接矩阵的下标从1开始*/
#include<stdio.h>
#define INF 200
int g[110][110],bz[110],low[110];
int min,ans,Q,n,a,b;
void prim()
{ int i,j,k,x;
for (i=1;i<=n;i++)
{ low[i]=g[1][i]; bz[i]=0; }
ans=0; bz[1]=1;
for (i=1;i<n;i++)
{ printf("low[]\n");
for (x=1;x<=n;x++)
printf("%d ",low[x]); printf("\n");
printf("bz[]\n");
for (x=1;x<=n;x++)
printf("%d ",bz[x]); printf("\n");
min=INF;
for (k=1;k<=n;k++)
if ( (low[k]<min)&& ( bz[k]==0) ) { min=low[k]; j=k; }
ans+=min;
printf("min=%d ans=%d\n",min, ans);
bz[j]=1;
for (k=1;k<=n;k++)
if ( (g[j][k]<low[k])&&(bz[k]==0)) low[k]=g[j][k];
}
}
int main()
{ int i,j;
while (scanf("%d",&n)!=EOF)
{
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&g[i][j]);
prim();
printf("ans=%d\n",ans);
}
}
/*
5
200 10 200 11 200
10 200 13 18 200
200 13 200 12 15
11 18 12 200 16
200 200 15 16 200
*/