コード例 #1
0
ファイル: main.c プロジェクト: Pridexs/udesc
int main()
{
    int **matAdj;
    int nVertices, nArestas, noFonte, noDestino;
    int i, j;

    printf("> Trabalho Final\n");


    FILE *arquivo = fopen("grafo.txt", "r");
    matAdj = criaMatAdjacenteComPeso(&nVertices, &nArestas, &noFonte, &noDestino, arquivo);

    /*
    * Impressao Matriz
    */
    printf("Matriz:\n");
    for (i = 0; i < nVertices; i++)
    {
        for (j = 0; j < nVertices; j++)
        {
            printf("%4d", matAdj[i][j]);
        }
        printf("\n");
    }
    printf("\n");

    int fluxoMaximo = fordFulkerson(matAdj, nVertices, nArestas, noFonte, noDestino);
    printf("Fluxo Maximo: %d\n", fluxoMaximo);

    return 0;
}
コード例 #2
0
int main(){

  int runs;
  int cases = 0;
  int k,type;

  scanf("%d",&runs);

  while (runs--){
		cases++;
		scanf("%d %d",&n,&m);

		for (int i=0; i < n; ++i)
			 for (int j=0; j < m; ++j){
				  st[i][j] = 0;
			 }

		for (int i=0; i < n; ++i){
			 scanf("%d",&k);
			 for (int j=0; j < k; ++j){
				  scanf("%d",&type);
				  st[i][type-1]++;
			 }
		}
		/* formamos la red */
		N = 2*(m+1)*n;
		
		for (int i=0; i < N; ++i){
		  for (int j=0; j < N; ++j)
			 fnet[i][j] = cap[i][j] = 0;
		}
		
		for (int i=0; i < n; ++i){
			 for (int j=0; j < m; ++j){
				  if (st[i][j] > 1){
						add_edge(source(i),out(i,j),st[i][j]-1);
				  }
				  else if (st[i][j] == 0){
						add_edge(in(i,j),sink(i),1);
				  }
			 }
			 if (i != 0){		
				  add_edge(sink(i),source(i),INF);
			 }
		}
		for (int i=0; i < n; ++i)
		  for (int j=0; j < n; ++j)
				if (i!=j){
				  for (k=0; k < m; ++k)
					 add_edge(out(i,k),in(j,k),1);
				}
		int res = 0;
		for (int i=0; i < m; ++i)
			 if (st[0][i]) res++;
		printf("Case #%d: %d\n",cases,res + fordFulkerson(N,source(0),sink(0)));
  }

  return 0;
}
コード例 #3
0
ファイル: main.c プロジェクト: Powkachu/FordFulkerson
int main()
{
    printf("*** Algorithme de Ford-Fulkerson ***\n");
    printf("Ouverture du fichier contenant la matrice\n");

    graphe g = init_graphe();
    int flot = fordFulkerson(g);

    printf("Le flot maximum est : %d\n",flot);

    return 0;
}
コード例 #4
0
int main(){
    int f, vezes=0;
    setlocale(LC_ALL, "Portuguese");

    lerDados();

    f = fordFulkerson();
    imprimeFluxo();
    printf("%d %d\n", f, a);

    limparDados();
    return 0;
}
コード例 #5
0
int main() {
    int source = 109;
    int drain = 609;
    Graph graph = readGraph();
    int maxOutFlow = 0;
    int maxInFlow = 0;
    for (int u = 0; u < vertexCount; u++) {
	if(graph.getCost(source, u) > 0)
	    maxOutFlow += graph.getCost(source, u);
	if(graph.getCost(u, drain) > 0)
	    maxInFlow += graph.getCost(u, drain);
    }
    std::cout<<"Maximal flow: "<<fordFulkerson(graph, source, drain)<<std::endl;
    std::cout<<"Maximal out flow: "<<maxOutFlow<<std::endl;
    std::cout<<"Maximal in flow: "<<maxInFlow<<std::endl;
    return 0;
}
コード例 #6
0
int _tmain(int argc, _TCHAR* argv[])
{
	int numSaida = 0, numFim = 0;

	/*
	int m[TAMLIN][TAMCOL] = 
				  {-999,1,-999,3,-999,0,-999,
				   0,-999,2,4,5,0,-999,
				   -999,0,-999,-999,4,0,-999,
				   0,0,-999,-999,0,-999,9,
				   -999,0,0,1,-999,-999,6,
				   5,7,4,-999,-999,-999,-999,
				   -999,-999,-999,0,0,-999,-999};
				   */
	
	int m[TAMLIN][TAMCOL] = 
				  {-999,7,9,5,-999,-999,-999,
				   2,-999,2,-999,3,-999,-999,
				   1,1,-999,2,3,2,-999,
				   0,-999,3,-999,-999,4,-999,
				   -999,3,2,-999,-999,3,8,
				   -999,-999,2,2,2,-999,9,
				   -999,-999,-999,-999,4,1,-999};
	

	/*
	int m[TAMLIN][TAMCOL] = 
				  {-999,20,30,10,-999,
				   0,-999,40,-999,30,
				   0,0,-999,10,20,
				   0,-999,5,-999,20,
				   -999,0,0,0,-999};
	*/

	printf("Vertice Saida: ");
	scanf("%d", &numSaida);
	printf("\n\nVertice Chegada: ");
	scanf("%d", &numFim);

	fordFulkerson(m, numFim, numSaida);
	return 0;
}
コード例 #7
0
ファイル: asap2.c プロジェクト: joaogodinho/ISTTP1314ASAP2
int main()
{
    int i, j, k, nNodes, nEdges, initialNode, finalNode, nCritAreas, nCritPoints, criticPoint, *results, trash = 0;

    trash = scanf(" %d %d", &nNodes, &nEdges);

    graph.nNodes = nNodes;
    graph.neighbors = (int **) malloc(sizeof(int *) * nNodes);
    graph.nNeighbors = (int *) malloc(sizeof(int) * nNodes);
    graph.nodesInSearch = (int **) malloc(sizeof(int *) * nNodes);

    queueInit(nNodes);

    parent = (int *) malloc(sizeof(int) * nNodes);
    visited = (int *) malloc(sizeof(int) * nNodes);

    for(i=0; i<nNodes; i++) {
        graph.nNeighbors[i] = 0;
        graph.neighbors[i] = NULL;
        graph.nodesInSearch[i] = NULL;
        parent[i] = -1;
        visited[i] = -1;
    }

    for (i=0; i<nEdges; i++) {
        trash = scanf(" %d %d", &initialNode, &finalNode);

        graph.neighbors[initialNode] = realloc(graph.neighbors[initialNode],
            sizeof(int) * (graph.nNeighbors[initialNode] + 1));
        graph.neighbors[initialNode][graph.nNeighbors[initialNode]] = finalNode;

        graph.neighbors[finalNode] = realloc(graph.neighbors[finalNode],
            sizeof(int) * (graph.nNeighbors[finalNode] + 1));
        graph.neighbors[finalNode][graph.nNeighbors[finalNode]] = initialNode;

        graph.nodesInSearch[initialNode] = realloc(graph.nodesInSearch[initialNode],
                                                   sizeof(int) * (graph.nNeighbors[initialNode] + 1));
        graph.nodesInSearch[initialNode][graph.nNeighbors[initialNode]] = 0;


        graph.nodesInSearch[finalNode] = realloc(graph.nodesInSearch[finalNode],
                                                   sizeof(int) * (graph.nNeighbors[finalNode] + 1));
        graph.nodesInSearch[finalNode][graph.nNeighbors[finalNode]] = 0;

        graph.nNeighbors[finalNode]++;
        graph.nNeighbors[initialNode]++;
    }

    trash = scanf(" %d", &nCritAreas);

    results = (int *) malloc(sizeof(int) * nCritAreas);
    criticPoints = (int **) malloc (sizeof(int *) * nCritAreas);
    nCriticPoints = (int *) malloc(sizeof(int) * nCritAreas);

    for(i = 0; i < nCritAreas; i++) {
        trash = scanf(" %d", &nCritPoints);
        criticPoints[i] = (int *) malloc(sizeof(int) * nCritPoints);
        nCriticPoints[i] = nCritPoints;
        for(j = 0; j < nCritPoints; j++) {
            trash = scanf( " %d", &criticPoint);
            criticPoints[i][j] = criticPoint;
        }
    }

    for (k = 0; k < nCritAreas; k++) {
        results[k] = INT_MAX;
        for (i = 0; i < nCriticPoints[k]; i++) {
            for (j = i+1; j < nCriticPoints[k]; j++) {
                trash = 0;
                queueReset();
                trash = fordFulkerson(criticPoints[k][i], criticPoints[k][j]);
                results[k] = results[k] < trash ? results[k] : trash;
            }

        }
    }


    for (i = 0; i < nCritAreas; i++) {
        printf("%d\n", results[i]);
    }

    /* Frees */
    for (i=0; i<graph.nNodes; i++) {
        free(graph.neighbors[i]);
        free(graph.nodesInSearch[i]);
    }

    free(graph.neighbors);
    free(graph.nNeighbors);
    free(graph.nodesInSearch);
    free(parent);
    free(visited);
    queueFree();
    free(results);
    for (i = 0; i < nCritAreas; i++) { free(criticPoints[i]); }
    free(criticPoints);
    free(nCriticPoints);
    return 0;
}