void test() { vii* adj = new vii[5]; adj[0].push_back(ii(1, 1)); adj[0].push_back(ii(2, 10)); adj[1].push_back(ii(3, 2)); adj[2].push_back(ii(3, -10)); adj[3].push_back(ii(4, 3)); bool neg_cycle; int* dist = bellman_ford(5, 0, adj, neg_cycle); assert_false(neg_cycle); assert_equal(0, dist[0]); assert_equal(1, dist[1]); assert_equal(10, dist[2]); assert_equal(0, dist[3]); assert_equal(3, dist[4]); delete[] adj; adj = new vii[5]; adj[0].push_back(ii(1, 1)); adj[0].push_back(ii(2, 0)); adj[1].push_back(ii(3, 2)); adj[2].push_back(ii(3, -10)); adj[3].push_back(ii(4, 3)); adj[3].push_back(ii(0, 0)); dist = bellman_ford(5, 0, adj, neg_cycle); assert_true(neg_cycle); delete[] adj; adj = new vii[2]; adj[0].push_back(ii(1, -1)); adj[1].push_back(ii(0, -1)); dist = bellman_ford(2, 0, adj, neg_cycle); assert_true(neg_cycle); delete[] adj; adj = new vii[2]; adj[0].push_back(ii(1, -1)); adj[1].push_back(ii(0, -1)); dist = bellman_ford(2, 1, adj, neg_cycle); assert_true(neg_cycle); }
int main() { ALGraph *graph = malloc(sizeof(ALGraph)); createALGraph(graph); printGraph(graph); if(bellman_ford(graph, 0)>0) { printshortestPath(graph, 0); } else { printf("there is a negative circle\n"); } /* test = -1; if(test) { printf("true\n"); } else { printf("false\n"); } */ return 0; }
int main(int argc, char *argv[]) { int i, j; int w; FILE *fin = fopen("dist.txt", "r"); fscanf(fin, "%d", &n); e = 0; for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) { fscanf(fin, "%d", &w); if (w != 0) { edges[e].u = i; edges[e].v = j; edges[e].w = w; ++e; } } fclose(fin); /* printDist(); */ bellman_ford(0); printDist(); return 0; }
int main() { int i, res; init(); res = bellman_ford(); printf("%s\n", ans[res]); return 0; }
int main() { int ans, i; init(); bellman_ford(); ans = oo; for(i = 2;i <= n; i++) if(ans > d[i] + price[i]) ans = d[i] + price[i]; printf("%d\n", ans); return 0; }
int main(int argc, char* argv[]) { while(init()) { if (bellman_ford(0)) { printf("successful conspiracy\n"); } else { printf("lamentable kingdom\n"); } } return 0; }
void original_bellman_ford(std::shared_ptr<graph_generator> graph) { zbp::distance_matrix G = graph->original_graph(); std::vector<double> d; bellman_ford(G, 0, graph->size(), d); #ifdef _DEBUG if (d.empty()) std::cout << "Wykryto ujemny cykl" << std::endl; else { for (int i = 0; i < d.size(); ++i) std::cout << 0 << " --> " << i << " : " << d[i] << std::endl; } #endif }
int main() { scanf("%d %d", &V, &E); for(int i = 0; i < E; i++) { int from, to, cost; scanf("%d %d %d", &from, &to, &cost); // undirected graph EDGES[2 * i] = Edge(from, to, cost); EDGES[2 * i + 1] = Edge(to, from, cost); } bellman_ford(0); for(int i = 0; i < V; i++) { printf("vertex:%d, distance:%d\n", i, DISTANCE[i]); } }
int main() { FILE * f = fopen("matrix.txt","r"); readFromAdjMatrix(f); printAdjMatrix(); bfs(0); dfs(0); dfsRecurs(0); prim(0); kruskal(); dijkstra(0); bellman_ford(0); return 0; }
int main() { int ans, i; init(); memset(flag, 0, sizeof(flag)); flag[1] = 1; dfs(1, level[1]); bellman_ford(); ans = oo; for(i = 2;i <= n; i++) if(ans > d[i] + price[i]) ans = d[i] + price[i]; if(ans == oo) ans = price[1]; printf("%d\n", ans); return 0; }
int main(){ list<vertex*> V; vector<edge> E; populate_adj_list_w_edges(V, E); vertex* s = V.front(); bellman_ford(V, s); printf("Distance from %d:\n", s->index); print_adj_list(V); }
int main() { int i; scanf("%d",&n); for (i=0;i<n;i++) { scanf("%d%d%d",&f[i].st,&f[i].ed,&f[i].val); f[i].ed++; if (f[i].st<min) min=f[i].st; if (f[i].ed>max) max=f[i].ed; } printf("%d\n",bellman_ford(min)); return 0; }
int main(){ int tc, n, m, t, i, j, a, b; scanf("%d", &tc); while(tc--){ scanf("%d %d", &n, &m); memset(g, 0, sizeof(g)); memset(bitmask, 0, sizeof(bitmask)); for(i=0;i<m;i++){ scanf("%d %d %d", &a, &b, &t); g[a][b] = t; bitmask[a][b]=1; } if (bellman_ford(n, m)) printf("possible\n"); else printf("not possible\n"); } }
int main() { int i, j, a, b, c, n, m; while (scanf("%d%d", &n, &m) && n != 0) { j = 0; for (i = 0; i < m; ++i) { scanf("%d%d%d", &a, &b, &c); edge[j++] = Edge(a, b, c); edge[j++] = Edge(b, a, c); } if (bellman_ford(n, j, 1) == 1) printf("%d\n", dist[n]); } return 0; }
void deserialize_pkt(void * packet){ uint16_t sender_id; sender_id=process_pkt(packet); if(servers[sender_id-1].is_alive==1 && servers[sender_id-1].is_neighbor==1){ // accept packet only if its from an active and neighnor server printf("RECEIVED A MESSAGE FROM SERVER %d\n",sender_id); bellman_ford(); num_of_pkts_received++; reset_skip_flag(sender_id); } else{ //discard packet printf("PACKET FROM SERVER %d DISCARDED\n",sender_id); } }
int mincost_maxflow() { int max = 0, i, j, t; while(bellman_ford()) { i = n; while(pre[i] != -1) { t = pre[i]; e[t].f--; e[e[t].op].f++; e[e[t].op].c = -e[t].c; i = e[t].u; } max += -d[n]; } return max; }
int min_cost_flow() { int min = 0, i, j, t; while(bellman_ford()) { min += d[n]; i = n; while(pre[i] != -1) { t = pre[i]; e[t].f--; e[e[t].op].f++; e[e[t].op].c = -e[t].c; i = e[t].u; } } return min; }
int main() { int n, ml, md; int i; int a, b, c; int m = 1; scanf("%d %d %d", &n, &ml, &md); for (i = 0; i < ml; i++) { scanf("%d %d %d", &a, &b, &c); cons[m].a = a; cons[m].b = b; cons[m].c = c; m++; } for (i = 0; i < md; i++) { scanf("%d %d %d", &a, &b, &c); cons[m].a = b; cons[m].b = a; cons[m].c = -c; m++; } if (bellman_ford(cons, n, m - 1) == 0) { if (d[n] == INF) { printf("-2\n"); } else { printf("%d\n", d[n]); } } else { printf("-1\n"); } return 0; }
int reweighttingEdge(struct Matrix_Graph *mat_graph, \ struct Point_Graph *poi_graph) { int i, j; int newNode, noNegativeCircult; struct Edge *edge; newNode = 0; poi_graph->node[newNode].start = NULL; poi_graph->node[newNode].from = newNode; mat_graph->adjcent_matrix[newNode][newNode] = 0; for (i = 1; i <= poi_graph->node_num; i++) { edge = alloc(struct Edge, 1); edge->to = i; edge->weight = 0; edge->next = poi_graph->node[newNode].start; poi_graph->node[newNode].start = edge; mat_graph->adjcent_matrix[newNode][i] = INT_MAX; } for (j = 0; j <= mat_graph->node_num; j++) mat_graph->shortest_matrix[0][j] = mat_graph->adjcent_matrix[0][j]; if (!(noNegativeCircult = bellman_ford(mat_graph, poi_graph, newNode))) return noNegativeCircult; for (i = 1; i <= poi_graph->node_num; i++) { edge = poi_graph->node[i].start; while (edge) { edge->newWeight = edge->weight + \ mat_graph->shortest_matrix[newNode][i] \ - mat_graph->shortest_matrix[newNode][edge->to]; edge = edge->next; } } return noNegativeCircult; }
int main(int argc, char const *argv[]) { int n ; printf("enter number of vertices\n"); scanf("%d",&n); int a[n+1][n+1], w[n+1][n+1], d[n+1]; int path[n+1]; int i,j; create_matrix(n, a, w); printf("Adj matrix :\n"); for ( i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) { printf("%d ", a[i][j]); } printf("\n"); } printf("Weight matrix :\n"); for ( i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) { printf("%d ", w[i][j]); } printf("\n"); } printf("Enter source\n"); int source; scanf("%d",&source); bellman_ford(n, a, w, d, path, source); printf("Distances :\n"); print_result(n, d, path); return 0; }
int main(){ while (scanf("%d%d", &nrow, &ncol) && nrow){ int nstone, nhole, x, y; memset(holes, -1, sizeof(holes)); memset(stones, 0, sizeof(stones)); scanf("%d", &nstone); for (int i = 0; i < nstone; i++){ scanf("%d%d", &x, &y); stones[x][y] = true; } scanf("%d", &nhole); for (int i = 0; i < nhole; i++){ scanf("%d%d", &x, &y); holes[x][y].read(); } int ans = bellman_ford(0, nrow * ncol - 1); if (ans == -INF) printf("Never\n"); else if (ans == INF) printf("Impossible\n"); else printf("%d\n", ans); } return 0; }
void bellman() { int i, j; int nArestas; int nVertices; int **matAdj; int *distancia; /* d[i] é a distância mínima do nó escolhido ao nó i */ // Alocacao para a matAdj[tamMax][tamMax] matAdj = (int**)calloc(tamMax, sizeof(int*)); for (i = 0; i < tamMax; i++) { for (j = 0; j < tamMax; j++) { matAdj[i] = (int*)calloc(tamMax, sizeof(int)); } } // Inicia a matriz com INF for (i = 0; i < tamMax; i++) { for (j = 0; j < tamMax; j++) { if (i != j) matAdj[i][j] = INF; else matAdj[i][j] = 0; } } // Fim da alocao para a matriz FILE *fp = fopen("grafo.txt", "r"); if (fp == NULL) { printf("ERRO AO ABRIR GRAFO!\n"); exit(2); } //Carrega a matriz adjacente criaMatAdjacenteComPeso(matAdj, &nVertices, &nArestas, fp); //Alocacao da matriz que guarda as distancias distancia = (int*)malloc(sizeof(int) * nVertices); //alocacao do vetor de arestas arestas = (Aresta*)malloc(sizeof(Aresta)*2*nArestas); nArestas = 0; precedentes = (Lista*)malloc(sizeof(Lista)*nVertices); /*IMPRESSAO DA MATRIZ for(i=0;i<nVertices;i++){ for(j=0;j<nVertices;j++){ printf("%d ", matAdj[i][j]); } printf("\n"); } */ for (i = 0; i < nVertices; i++) { inicializa_lista(&precedentes[i], sizeof(int)); for (j = 0; j < nVertices; j++) { if (matAdj[i][j] != INF) { arestas[nArestas].origem = i; arestas[nArestas].destino = j; arestas[nArestas].peso = matAdj[i][j]; nArestas++; } } } fclose(fp); /* print origem -> destino com peso for(i=0;i<nArestas;i++){ printf("%d -%d-> %d\n", arestas[i].origem,arestas[i].peso,arestas[i].destino); } */ /* printDist(); */ int chosenOne; printf("Executar Bellman-Ford a partir do vertice: \n"); scanf("%d", &chosenOne); bellman_ford(chosenOne, nVertices, distancia, nArestas);/*chama funcao bellman_ford com parametros*/ printDistancias(chosenOne,nVertices, distancia);//imprime as distancias e precedentes }
int main() { graph_matrix g1; g1.g_cnt = 4; g1.g_m[0][1] = INF, g1.g_m[0][2] = 1, g1.g_m[0][3] = -5; g1.g_m[1][0] = INF, g1.g_m[1][2] = INF, g1.g_m[1][3] = 4; g1.g_m[2][0] = 1, g1.g_m[2][1] = INF, g1.g_m[2][3] = 2; g1.g_m[3][0] = -5, g1.g_m[3][1] = 4, g1.g_m[3][2] = 2; //这里的e1和g1描绘同一个图g1,8条边重复表示无向图,g1中存在负权值环 edge_list e1(8); e1.e_l[0].e_beg = 0, e1.e_l[0].e_end = 2, e1.e_l[0].e_value = 1; e1.e_l[1].e_beg = 2, e1.e_l[1].e_end = 0, e1.e_l[1].e_value = 1; e1.e_l[2].e_beg = 2, e1.e_l[2].e_end = 3, e1.e_l[2].e_value = 2; e1.e_l[3].e_beg = 3, e1.e_l[3].e_end = 2, e1.e_l[3].e_value = 2; e1.e_l[4].e_beg = 0, e1.e_l[4].e_end = 3, e1.e_l[4].e_value = -5; e1.e_l[5].e_beg = 3, e1.e_l[5].e_end = 0, e1.e_l[5].e_value = -5; e1.e_l[6].e_beg = 1, e1.e_l[6].e_end = 3, e1.e_l[6].e_value = 4; e1.e_l[7].e_beg = 3, e1.e_l[7].e_end = 1, e1.e_l[7].e_value = 4; //g2中不存在负权环,g2和e2描绘同一个图g2 graph_matrix g2; g2.g_cnt = 8; for(int i = 0; i < 8; ++ i) for(int j = 0; j < 8; ++ j) g2.g_m[i][j] = (i == j)? 0 : INF; g2.g_m[0][1] = 3; g2.g_m[0][2] = 9; g2.g_m[0][3] = 12; g2.g_m[1][0] = 3; g2.g_m[1][2] = 5; g2.g_m[1][5] = 9; g2.g_m[1][7] = 30; g2.g_m[2][0] = 9; g2.g_m[2][1] = 5; g2.g_m[2][7] = 26; g2.g_m[3][0] = 12; g2.g_m[3][4] = 3; g2.g_m[3][5] = 7; g2.g_m[3][6] = 20; g2.g_m[4][3] = 3; g2.g_m[4][5] = 11; g2.g_m[4][7] = 20; g2.g_m[5][1] = 9; g2.g_m[5][3] = 7; g2.g_m[5][4] = 11; g2.g_m[5][6] = 16; g2.g_m[6][3] = 20; g2.g_m[6][5] = 16; g2.g_m[6][7] = 9; g2.g_m[7][2] = 26; g2.g_m[7][1] = 30; g2.g_m[7][4] = 20; g2.g_m[7][6] = 9; edge_list e2(28); e2.e_l[0].e_beg = 0, e2.e_l[0].e_end = 1, e2.e_l[0].e_value = 3; e2.e_l[1].e_beg = 1, e2.e_l[1].e_end = 0, e2.e_l[1].e_value = 3; e2.e_l[2].e_beg = 0, e2.e_l[2].e_end = 2, e2.e_l[2].e_value = 9; e2.e_l[3].e_beg = 2, e2.e_l[3].e_end = 0, e2.e_l[3].e_value = 9; e2.e_l[4].e_beg = 1, e2.e_l[4].e_end = 2, e2.e_l[4].e_value = 5; e2.e_l[5].e_beg = 2, e2.e_l[5].e_end = 1, e2.e_l[5].e_value = 5; e2.e_l[6].e_beg = 0, e2.e_l[6].e_end = 3, e2.e_l[6].e_value = 12; e2.e_l[7].e_beg = 3, e2.e_l[7].e_end = 0, e2.e_l[7].e_value = 12; e2.e_l[8].e_beg = 1, e2.e_l[8].e_end = 5, e2.e_l[8].e_value = 9; e2.e_l[9].e_beg = 5, e2.e_l[9].e_end = 1, e2.e_l[9].e_value = 9; e2.e_l[10].e_beg = 1, e2.e_l[10].e_end = 7, e2.e_l[10].e_value = 30; e2.e_l[11].e_beg = 7, e2.e_l[11].e_end = 1, e2.e_l[11].e_value = 30; e2.e_l[12].e_beg = 3, e2.e_l[12].e_end = 4, e2.e_l[12].e_value = 3; e2.e_l[13].e_beg = 4, e2.e_l[13].e_end = 3, e2.e_l[13].e_value = 3; e2.e_l[14].e_beg = 3, e2.e_l[14].e_end = 5, e2.e_l[14].e_value = 7; e2.e_l[15].e_beg = 5, e2.e_l[15].e_end = 3, e2.e_l[15].e_value = 7; e2.e_l[16].e_beg = 3, e2.e_l[16].e_end = 6, e2.e_l[16].e_value = 20; e2.e_l[17].e_beg = 6, e2.e_l[17].e_end = 3, e2.e_l[17].e_value = 20; e2.e_l[18].e_beg = 2, e2.e_l[18].e_end = 7, e2.e_l[18].e_value = 26; e2.e_l[19].e_beg = 7, e2.e_l[19].e_end = 2, e2.e_l[19].e_value = 26; e2.e_l[20].e_beg = 4, e2.e_l[20].e_end = 5, e2.e_l[20].e_value = 11; e2.e_l[21].e_beg = 5, e2.e_l[21].e_end = 4, e2.e_l[21].e_value = 11; e2.e_l[22].e_beg = 5, e2.e_l[22].e_end = 6, e2.e_l[22].e_value = 16; e2.e_l[23].e_beg = 6, e2.e_l[23].e_end = 5, e2.e_l[23].e_value = 16; e2.e_l[24].e_beg = 6, e2.e_l[24].e_end = 7, e2.e_l[24].e_value = 9; e2.e_l[25].e_beg = 7, e2.e_l[25].e_end = 6, e2.e_l[25].e_value = 9; e2.e_l[26].e_beg = 4, e2.e_l[26].e_end = 7, e2.e_l[26].e_value = 20; e2.e_l[27].e_beg = 7, e2.e_l[27].e_end = 4, e2.e_l[27].e_value = 20; int dist[MAX], path[MAX]; cout << "bellman ford on g1:" << endl; if(bellman_ford(g1, e1, 0, dist, path)) //1 print_dp(dist, path, g1.g_cnt); else cout << "g1 doesn't exist shortest path" << endl; cout << "bellman ford on g2:" << endl; if(bellman_ford(g2, e2, 0, dist, path)) //1 print_dp(dist, path, g2.g_cnt); else cout << "g2 doestn't exist shortest path" << endl; cout << "spfa on g1:" << endl; if(spfa(g1, 0, dist, path)) //2 print_dp(dist, path, g1.g_cnt); else cout << "g1 doestn't exist shortest path" << endl; cout << "spfa on g2:" << endl; if(spfa(g2, 0, dist, path)) //2 print_dp(dist, path, g2.g_cnt); else cout << "g2 doesn't exist shortest path" << endl; cout << "dijkstra on g2:"; dijkstra(g2, 0, dist, path); //3 print_dp(dist, path, g2.g_cnt); cout << "floyd warshall on g2:" << endl; floyd(g2); //4 for(int i = 0; i < g2.g_cnt; ++ i){ for(int j = 0; j < g2.g_cnt; ++ j) cout << g2.g_m[i][j] << ' '; cout << endl; } cout << endl << "difference constraints: "; int **ss = new int*[8]; for(int i = 0; i < 8; ++ i) ss[i] = new int[5]; ss[0][0] = 1, ss[0][1] = -1, ss[0][2] = 0, ss[0][3] = 0, ss[0][4] = 0; ss[1][0] = 1, ss[1][1] = 0, ss[1][2] = 0, ss[1][3] = 0, ss[1][4] = -1; ss[2][0] = 0, ss[2][1] = 1, ss[2][2] = 0, ss[2][3] = 0, ss[2][4] = -1; ss[3][0] = -1, ss[3][1] = 0, ss[3][2] = 1, ss[3][3] = 0, ss[3][4] = 0; ss[4][0] = -1, ss[4][1] = 0, ss[4][2] = 0, ss[4][3] = 1, ss[4][4] = 0; ss[5][0] = 0, ss[5][1] = 0, ss[5][2] = -1, ss[5][3] = 1, ss[5][4] = 0; ss[6][0] = 0, ss[6][1] = 0, ss[6][2] = -1, ss[6][3] = 0, ss[6][4] = 1; ss[7][0] = 0, ss[7][1] = 0, ss[7][2] = 0, ss[7][3] = -1, ss[7][4] = 1; int b[8] = {0, -1, 1, 5, 4, -1, -3, -3}; int *dis = new int[MAX]; if(difference_constraints(ss, 8, 5, b, dis)){ //5 for(int i = 1; i <= 5; ++ i) cout << dis[i] << ' '; cout << endl; } else cout << "no ans" << endl; return(0); }
int main() { while(init()) bellman_ford(); return 0; }
int main(void) { int first[11], node[26], next[26], pi[11]; int w[26], d[11]; int s; int i; int ok; /* The graph contains the following directed edges with weights: (1, 2), weight 6 (1, 4), weight 7 (2, 3), weight 5 (2, 4), weight 8 (2, 5), weight -4 (3, 2), weight -2 (4, 3), weight -3 (4, 5), weight 9 (5, 1), weight 2 (5, 3), weight 7 */ first[1] = 1; first[2] = 3; first[3] = 6; first[4] = 7; first[5] = 9; first[6] = 12; first[7] = 14; first[8] = 18; first[9] = 20; first[10] = 24; node[1] = 2; node[2] = 4; node[3] = 3; node[4] = 4; node[5] = 5; node[6] = 2; node[7] = 3; node[8] = 5; node[9] = 1; node[10] = 3; node[11] = 6; node[12] = 7; node[13] = 8; node[14] = 9; node[15] = 10; node[16] = 1; node[17] = 2; node[18] = 9; node[19] = 4; node[20] = 10; node[21] = 2; node[22] = 10; node[23] = 5; node[24] = 4; node[25] = 3; w[1] = 6; w[2] = 7; w[3] = 5; w[4] = 8; w[5] = -4; w[6] = -2; w[7] = -3; w[8] = 9; w[9] = 2; w[10] = 7; w[11] = 4; w[12] = 2; w[13] = 9; w[14] = 1; w[15] = 5; w[16] = 3; w[17] = 4; w[18] = 9; w[19] = 6; w[20] = 12; w[21] = 1; w[22] = 9; w[23] = 2; w[24] = 1; w[25] = 4; next[1] = 2; next[2] = 0; next[3] = 4; next[4] = 5; next[5] = 0; next[6] = 0; next[7] = 8; next[8] = 0; next[9] = 10; next[10] = 11; next[11] = 0; next[12] = 13; next[13] = 0; next[14] = 15; next[15] = 16; next[16] = 17; next[17] = 0; next[18] = 19; next[19] = 0; next[20] = 21; next[21] = 22; next[22] = 23; next[23] = 0; next[24] = 25; next[25] = 0; //printf("Enter source node: "); //scanf("%d", &s); s = 1; ok = bellman_ford(first, node, next, w, d, pi, s, 10); //printf("bellman_ford returns "); //printf("%d\n\n", ok); int ret=0; for (i = 1; i <= 5; ++i) { //printf("%d: %f %d\n", i, d[i], pi[i]); ret+=d[i]+pi[i]; } return ret; }
void main(void) { int first[6], node[11], next[11], pi[6]; double w[11], d[6]; int s; int i; int ok; /* The graph contains the following directed edges with weights: (1, 2), weight 6 (1, 4), weight 7 (2, 3), weight 5 (2, 4), weight 8 (2, 5), weight -4 (3, 2), weight -2 (4, 3), weight -3 (4, 5), weight 9 (5, 1), weight 2 (5, 3), weight 7 */ first[1] = 1; first[2] = 3; first[3] = 6; first[4] = 7; first[5] = 9; node[1] = 2; node[2] = 4; node[3] = 3; node[4] = 4; node[5] = 5; node[6] = 2; node[7] = 3; node[8] = 5; node[9] = 1; node[10] = 3; w[1] = 6.0; w[2] = 7.0; w[3] = 5.0; w[4] = 8.0; /* w[5] = -4.0; w[6] = -2.0; w[7] = -3.0; */ w[5] = 4.0; w[6] = 2.0; w[7] = 3.0; w[8] = 9.0; w[9] = 2.0; w[10] = 7.0; next[1] = 2; next[2] = 0; next[3] = 4; next[4] = 5; next[5] = 0; next[6] = 0; next[7] = 8; next[8] = 0; next[9] = 10; next[10] = 0; print "Enter source node: "; read s; ok = bellman_ford(first, node, next, w, d, pi, s, 5); print "bellman_ford returns "; print ok; print "\n\n"; for (i = 1; i <= 5; ++i) { print i; print ": "; print d[i]; print " "; print pi[i]; print "\n"; } }
int main(void) { int first[6], node[11], next[11], pi[6]; double w[11], d[6]; int s; int i; int ok; /* The graph contains the following directed edges with weights: (1, 2), weight 6 (1, 4), weight 7 (2, 3), weight 5 (2, 4), weight 8 (2, 5), weight -4 (3, 2), weight -2 (4, 3), weight -3 (4, 5), weight 9 (5, 1), weight 2 (5, 3), weight 7 */ first[1] = 1;// on met tous les sommet // first[2] = 3; first[3] = 6; first[4] = 7; first[5] = 9; next[1] = 2; // on met a zero le dernier arc de chaque sommet next[2] = 0; next[3] = 4; next[4] = 5; next[5] = 0; next[6] = 0; next[7] = 8; next[8] = 0; next[9] = 10; next[10] = 0; node[1] = 2;// on met les arc dans l'ordre // node[2] = 4; node[3] = 3; node[4] = 4; node[5] = 5; node[6] = 2; node[7] = 3; node[8] = 5; node[9] = 1; node[10] = 3; w[1] = 6.0;// on met tous les poids dans l'ordre w[2] = 7.0; w[3] = 5.0; w[4] = 8.0; w[5] = -4.0; w[6] = -2.0; w[7] = -3.0; w[8] = 9.0; w[9] = 2.0; w[10] = 7.0; printf("Enter source node: "); scanf("%d", &s); ok = bellman_ford(first, node, next, w, d, pi, s, 5); printf("bellman_ford returns "); printf("%d\n\n", ok); for (i = 1; i <= 5; ++i) { printf("%d: %f %d\n", i, d[i], pi[i]); } return 0; }