Dijkstra ----------

Source: Internet
Author: User

 

 

 

Hnldyhy (303882171) 11:00:25
// The subscript of the Dijkstra adjacent matrix starts from 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
*/

 

 

 

 

 

 


// The subscript of the Dijkstra adjacent matrix starts from 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
*/

 

 

 

 

 

// The subscript of the Dijkstra adjacent matrix starts from 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
*/

 

 

 

 

 

 

 

 

// The subscript of the Dijkstra adjacent matrix starts from 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
*/

 

 

 

 

 

 

 

 

 

 


// The subscript of the Dijkstra adjacent matrix starts from 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
/* Minimum Spanning Tree, with the prim algorithm, the subscript of the adjacent matrix starts from 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
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.