int main()
{
	Graph g1 = test1();

	std::vector<long long> result;
	bool cycle = bellmanFord(g1, 5, result);

	std::cout << "Test 1: " << std:: endl
			  << "Result for vertice 5 as source:" << std::endl;
	if(not cycle)
	{
		for(auto distance: result)
			std::cout << distance << ' ';
	}
	else
		std::cout << "Negative cycle is present in the graph";
	std::cout << std::endl << std::endl;

	std::cout << "Test 2: " << std:: endl
			  << "Result for vertice 1 as source:" << std::endl;


	g1 = test2();
	cycle = bellmanFord(g1, 1, result);
	if(not cycle)
	{
		for(auto distance: result)
			std::cout << distance << ' ';
	}
	else
		std::cout << "Negative cycle is present in the graph";
	std::cout << std::endl << std::endl;

	return 0;
}
Ejemplo n.º 2
0
int main(){
	int farmNum,roadNum,partyLocation;
	scanf("%d%d%d",&farmNum,&roadNum,&partyLocation);
	partyLocation--;
	//Edge roads[ROAD_NUM_MAX];
	Edge *roads=new Edge[roadNum];
	for(int roadID=0;roadID<roadNum;roadID++){
		scanf("%d%d%d",&roads[roadID].from,&roads[roadID].to,&roads[roadID].weight);
		roads[roadID].from--;
		roads[roadID].to--;
	}
	//int timeBack[FARM_NUM_MAX];
	int *timeBack=new int[farmNum];
	bellmanFord(farmNum,timeBack,roadNum,roads,partyLocation);
	for(int roadID=0;roadID<roadNum;roadID++)
		std::swap(roads[roadID].from,roads[roadID].to);
	//int timeForward[FARM_NUM_MAX];
	int *timeForward=new int[farmNum];
	bellmanFord(farmNum,timeForward,roadNum,roads,partyLocation);
	int timeMax=0;
	for(int farmID=0;farmID<farmNum;farmID++)
		if(timeMax<timeForward[farmID]+timeBack[farmID])
			timeMax=timeForward[farmID]+timeBack[farmID];
	printf("%d\n",timeMax);
	delete[] roads;
	delete[] timeBack;
	delete[] timeForward;
	return 0;
}
Ejemplo n.º 3
0
int main()
{
    FILE * f = fopen("matrix.txt","r");
    readFromAdjMatrix(f);
    printAdjMatrix();
    bfs(0);
    dfs(0);
    dfsRecurs(0);
    prim(0);
    kruskal();
    dijkstra(0);
    bellmanFord(2);
    return 0;
}
Ejemplo n.º 4
0
BellmanFordResult* reconsider_graph(Graph *graph){
    BellmanFordResult *bellman_ford_result;
    Graph graph_reconsidered;
    graph_reconsidered.numbers_nodes = graph->numbers_nodes + 1;
    allocate_memory(&graph_reconsidered);
    create_graph_reconsidered(&graph_reconsidered, graph);
    bellman_ford_result = bellmanFord(&graph_reconsidered, graph_reconsidered.numbers_nodes-1);
    if(bellman_ford_result->exist_negative_cycle == false){
        for(int i = 0; i < graph->numbers_nodes; i++)
            for(int j = 0; j < graph->numbers_nodes; j++)
                if(exist_arc(graph, i, j))
                    insert_arc(graph, i, j, get_cost_edge(graph, i, j) + bellman_ford_result->distance[i] - bellman_ford_result->distance[j]);
    };
    return bellman_ford_result;
};
Ejemplo n.º 5
0
int main() {
	int test_case;
	scanf("%d", &test_case);
	while (test_case--) {
		edge_number = 0;
		scanf("%d%d", &point_number, &cnt_edge);
		for (int i = 1; i <= cnt_edge; i++) {
			int start, end, value;
			scanf("%d%d%d", &start, &end, &value);
			addEdge(start, end, value);
		}
		int flag = bellmanFord();
		if (!flag) {
			printf("possible\n");
		}
		else {
			printf("not possible\n");
		}
	}
	return 0;
}