Anda di halaman 1dari 2

#include <stdio.

h>
#include <iostream>
using namespace std;
#include<conio.h>
#define GRAPHSIZE 2048
#define INFINITY GRAPHSIZE*GRAPHSIZE
#define MAX(a, b) ((a > b) ? (a) : (b))
int e; /* The number of nonzero edges in the graph */
int n; /* The number of nodes in the graph */
int distt[10][5]; /* dist[i][j] is the distance between node i and j; or 0 if th
ere is no direct connection */
int dist[5][5];
long w[10];
long d[20]; /* d[i] is the length of the shortest path between the source (s) an
d node i */
void printD() {
int i;
for (i = 1; i <= n; ++i)
printf("%10d", i);
printf("\n");
for (i = 1; i <= n; ++i) {
printf("%10ld", d[i]);
}
printf("\n");
}
void dijkstra(int s) {
int i, k, mini;
int visited[10];
n=5;
for (i = 1; i <= n; ++i) {
d[i] = 100;
visited[i] = 0; /* the i-th element has not yet been visited */
}
d[s] = 0;
for (k = 1; k <=n; ++k) {
mini = k;
for (i = 1; i <= n; ++i){
if (!visited[i] &&( (d[i] < d[mini])))
mini=i;
visited[mini] = 1;
for (i = 1; i <= n; ++i){
if (dist[i-1][mini-1]!=0)
if (d[mini] + dist[i-1][mini-1] < d[i])
d[i] = d[mini] + dist[i-1][mini-1];
}
}
}
}
int main(int argc, char *argv[])
{
int i, j;
int u=0, v=0;
int w[10]={1,2,3,4,5,6,7,2,9,10};
int e;
int distt[10][5]={{0,-1,1,0,0},{0,0,0,-1,1}, {0,-1,0,1,0},{0,0,1,0,-1},{
-1,0,0,1,0},{0,0,-1,0,1},{1,0,0,0,-1},{0,1,0,-1,0},{0,0,1,-1,0},{-1,1,0,0,0}};
for (i = 0; i < 5; ++i)
for (j = 0; j < 5; ++j){
dist[i][j] = 0;
// cout<<dist[i][j]<<endl;;
}
for (i = 0; i < 10; ++i){
for (j = 0; j < 5; ++j){
if (distt[i][j]==1){
v=(j+1);
//cout<<u<<endl;
}
if (distt[i][j]==-1){
u=(j+1);
//cout<<v<<endl;
}
if (u!=0 && v!=0){
//cout<<v<<u<<endl;
dist[v-1][u-1]=w[i];
cout<<u<<v<<dist[v-1][u-1]<<endl;
u=0, v=0;
}
}
}
n = 5;
dijkstra(1);
printD();
getch();
// return 0;
}

Anda mungkin juga menyukai