Exemplo n.º 1
0
int main()
{
    int i,j;
    Graph G;
    Input(&G);
    printf("\n建立的无向带权图是:\n");
    printf(" ");
    for(i=0;i<G.nodeN;i++)
        printf(" %c",G.data[i]);
    printf("\n");
    for(i=0;i<G.nodeN;i++)
    {
        printf("%c ",G.data[i]);
        for(j=0;j<G.nodeN;j++)
        {
            printf("%d ",G.weight[i][j] >= 0 ? G.weight[i][j]:0);
        }
        printf("\n");
    }
    s = 0;//初始化总权重
    printf("\n最小生成树的边为:\n");
    Prim(&G);
    printf("总权重为:%d\n",s);
    return 0;
}
int main()
{
    while(scanf("%d",&n),n)
    {
        for(int i=0;i<n;i++)
            scanf("%d%d%d",&x[i],&y[i],&z[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                cost[i][j]=cost[j][i]=abs(z[i]-z[j]);
                dis[i][j]=dis[j][i]=Distance(i,j);
            }
        }
        double l=0,r=1e8,mid;
        while(r-l>1e-6)
        {
            mid=(r+l)/2;
            if(Prim(mid))
                l=mid;
            else r=mid;
        }
        printf("%.3lf\n",l);
    }
    return 0;
} 
Exemplo n.º 3
0
// Prim's MCST algorithm: use priority queue
// Version for Adjancency Matrix representation
int main(int argc, char** argv) {
  Graph* G;
  FILE *fid;

  if (argc != 2) {
    cout << "Usage: grprimm2 <file>\n";
    exit(-1);
  }

  if ((fid = fopen(argv[1], "rt")) == NULL) {
    cout << "Unable to open file |" << argv[1] << "|\n";
    exit(-1);
  }

  G = createGraph<Graphm>(fid);
  if (G == NULL) {
    cout << "Unable to create graph\n";
    exit(-1);
  }

  int D[G->n()];
  for (int i=0; i<G->n(); i++)     // Initialize
    D[i] = INFINITY;
  D[0] = 0;

  Prim(G, D, 0);
  for(int k=0; k<G->n(); k++)
    cout << D[k] << " ";
  cout << endl;
  return 0;
}
Exemplo n.º 4
0
int main(int argc, char* argv[])
{
	while ( init () ) {
		printf ( "%.3lf\n" , Prim () );
	}
	return 0;
}
Exemplo n.º 5
0
int main() {
  MatrixGraph graph;
  int i, j, s, t;
  char select;

  do {
    printf("输入生成图的类型(0:无向图,1:有向图):");
    scanf("%d", &graph.graphType);

    printf("输入图的顶点数量和边数量(n,m):");
    scanf("%d,%d", &graph.vertexNum,&graph.edgeNum);

    for(i=0;i<graph.vertexNum;i++)
      for(j=0;j<graph.vertexNum;j++)
        graph.edges[i][j] = MAXVALUE;

    Create(&graph);
    printf("邻接矩阵数据如下:\n");
    OutPut(&graph);

    Prim(graph);

    printf("图遍历完毕,是否要继续?Y/N: \n");
    getchar();
    scanf("%c", &select);
  }while(select !='N' && select != 'n');

  return 0;
}
Exemplo n.º 6
0
int main(){
  scanf("%d%d", &n, &m);
  int u, v, w;
  memset(G, INF, sizeof(G));
  //for(int i = 1; i <= n; ++i) G[i][i] = 0;
  for(int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &u, &v, &w);
    G[u][v] = G[v][u] = w;
  }
  printf("%d\n", Prim(1));
  while(1);
  return 0;
}
Exemplo n.º 7
0
int main(){
  int a,b,c,i,flag;
  while(~scanf("%d",&n)&&n){
    memset(vis,0,sizeof vis);
    memset(adj,-1,sizeof adj);
    memset(d,0x3F,sizeof d);
    for(i=1;i<=n*(n-1)/2;i++){
      scanf("%d%d%d%d",&a,&b,&c,&flag);
      adj[a][b]=c,adj[b][a]=c;
      if(flag)adj[a][b]=0,adj[b][a]=0;
    }
    Prim();
  }
}
Exemplo n.º 8
0
int main(){

    double tempo = crono();
    int n, m, u, v, c, count=1;
    FILE *entrada = fopen("entrada.txt", "r");

    while(!feof(entrada)){

        fscanf(entrada, "%d %d", &n, &m);
        setMatriz(n);

        while(m--){
            fscanf(entrada, "%d %d %d", &u, &v, &c);
            mat[u][v] = c;
            mat[v][u] = c;
        }

        int pr = Prim(n, 1);
        int y;
        for(y=1; y<=n; y++){
            if(chave[y] == 999999){
                setMatriz(n);
                Prim(n, y);
            }
        }

        printf("Instancia %d\n%d\n\n", count, pr);
        count++;

    }

    tempo = crono() - tempo;
    printf("\n\nTEMPO DE EXECUCAO: %.25lf", tempo);

    return 0;
}
int main()
{
	FILE * in = fopen("input.txt", "r");

	int numVerticies;
	fscanf(in,"%d", &numVerticies);

	Graph G = newGraph(numVerticies);
	buildGraph(in,G);
	Prim(G, 1);
	fclose(in);

	printf("\n\n\t***** COJ : 1480 - Dota Warlock Power *****\n\n");
	system("PAUSE");
	return 0;

}
Exemplo n.º 10
0
void main()
{
	int mat[100][100];
	int n,len,i;
	int a,b,cost,s;
	int inf=100<<16+100;
	while (scanf("%d",&n),n)
	{
		memset(mat,100,sizeof(mat));
		len=n*(n-1)/2;
		for (i=0;i<len;i++)
		{
			scanf("%d%d%d%d",&a,&b,&cost,&s);
			if (s==1)cost=0;
			mat[a-1][b-1]=mat[b-1][a-1]=cost;
		}
		printf("%d\n",Prim(mat,n));
	}
}
int main()
{
    int ncase;
    scanf("%d",&ncase);
    while(ncase--)
    {
        scanf("%d%d",&s,&p);
        for(int i=0;i<p;i++)
            scanf("%lf%lf",&nod[i].x,&nod[i].y);
        for (int i = 0; i < p; i++)
            for (int j = i + 1; j < p; j++)
                dis[i][j] = dis[j][i] = Distance(i,j);
        top=0;
        Prim(0);
        std::sort(edg,edg+top);
        printf("%.2lf\n",edg[p-s-1].d);
    }
    return 0;
}
Exemplo n.º 12
0
int main()
{
	int n,i,j,k,q;
	scanf("%d",&n);
	for (i=0;i<n;i++)
		for (j=0;j<n;j++)
			scanf("%d",&GE[i][j]);
	scanf("%d",&q);
	for (i=0;i<q;i++)
	{
		scanf("%d%d",&j,&k);
		j--;
		k--;
		GE[j][k]=0;
		GE[k][j]=0;
	}
	printf("%d\n",Prim(n));
	return 0;
}
int main()
{
    while(scanf("%d",&n),n)
    {
        for(int i=0;i<n;i++)
            scanf("%d%d%d",&x[i],&y[i],&z[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                cost[i][j]=cost[j][i]=abs(z[i]-z[j]);
                dis[i][j]=dis[j][i]=Distance(i,j);
            }
        }
        double now=0,next;
        do{
            next=now;
            now=Prim(now);
        }while(fabs(next-now)>1e-5);
        printf("%.3lf\n",next);
    }
    return 0;
} 
Exemplo n.º 14
0
void main()
{
	int mat[M][M],n,m;
	int c1,c2,cost,ret;
	int i,j;
	while(scanf("%d%d",&n,&m),n)
	{
		for (i=0;i<m;i++)
			for (j=0;j<m;j++)
				mat[i][j]=inf;

		while (n--)
		{
			scanf("%d%d%d",&c1,&c2,&cost);
			mat[c1-1][c2-1]=mat[c2-1][c1-1]=cost;
		}
		ret=Prim(mat,m);
		if (ret<inf)
			printf("%d\n",ret);
		else
			puts("?");
	}
}
Exemplo n.º 15
0
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Arista * a02 = new Arista(3, 2);
    Arista * a03 = new Arista(1, 3);

    Arista * a15 = new Arista(7, 5);

    Arista * a20 = new Arista(3, 0);
    Arista * a25 = new Arista(4, 5);
    Arista * a26 = new Arista(6, 6);

    Arista * a30 = new Arista(1, 0);
    Arista * a37 = new Arista(3, 7);

    Arista * a46 = new Arista(8, 6);
    Arista * a47 = new Arista(3, 7);

    Arista * a51 = new Arista(7, 1);
    Arista * a52 = new Arista(4, 2);
    Arista * a57 = new Arista(2, 7);

    Arista * a62 = new Arista(6, 2);
    Arista * a64 = new Arista(8, 4);
    Arista * a67 = new Arista(8, 7);

    Arista * a73 = new Arista(3, 3);
    Arista * a74 = new Arista(3, 4);
    Arista * a75 = new Arista(2, 5);
    Arista * a76 = new Arista(8, 6);

    Nodo * n0 = new Nodo(0);
    n0->addArista(a02);
    n0->addArista(a03);

    Nodo * n1 = new Nodo(1);
    n1->addArista(a15);

    Nodo * n2 = new Nodo(2);
    n2->addArista(a20);
    n2->addArista(a25);
    n2->addArista(a26);

    Nodo * n3 = new Nodo(3);
    n3->addArista(a30);
    n3->addArista(a37);

    Nodo * n4 = new Nodo(4);
    n4->addArista(a46);
    n4->addArista(a47);

    Nodo * n5 = new Nodo(5);
    n5->addArista(a51);
    n5->addArista(a52);
    n5->addArista(a57);

    Nodo * n6 = new Nodo(6);
    n6->addArista(a62);
    n6->addArista(a64);
    n6->addArista(a67);

    Nodo * n7 = new Nodo(7);
    n7->addArista(a73);
    n7->addArista(a74);
    n7->addArista(a75);
    n7->addArista(a76);

    Prim prim = Prim();
    prim.agregarNodo(n0);
    prim.agregarNodo(n1);
    prim.agregarNodo(n2);
    prim.agregarNodo(n3);
    prim.agregarNodo(n4);
    prim.agregarNodo(n5);
    prim.agregarNodo(n6);
    prim.agregarNodo(n7);

    prim.resolver(0);
    prim.print();

    return a.exec();
}
Exemplo n.º 16
0
int main(int argc, char **argv){
  if(argc != 3){
    usage(argv[0]);
    exit(EXIT_FAILURE);
  }
  
  std::ifstream fin;
  fin.open(argv[1]);
  if(!fin.good()){
    std::cerr << argv[0] << ": ";
    perror(argv[1]);
    exit(EXIT_FAILURE);
  }
  
  std::ofstream fout;
  fout.open(argv[2]);
  if(!fout.good()){
    std::cerr << argv[0] << ": ";
    perror(argv[2]);
    exit(EXIT_FAILURE);
  }
 
  /*** parse file and create graph ***/
  int nGraphs, nVertices, nEdges;//, v1, v2 , weight;
  fin >> nGraphs;
  fout << "There are " << nGraphs << " graphs in " << argv[1] << std::endl;
  for(int i = 0; i < nGraphs; i++){
    // build graph
    fin >> nVertices;
    fin >> nEdges;
    Graph graph(nVertices);
    for(int e = 0; e < nEdges; e++){
      Edge edge;
      fin >> edge;
      if(DEBUG) std::cerr << "received edge: " << edge << std::endl;
      graph.insertEdge(edge);  
    }
    
    if(DEBUG){
      std::cerr << graph.numVertices() << " vertices, " << graph.numEdges() << " edges" << std::endl;
      graph.showEdges();
      graph.showAdj(false);
    }
    
    // run algorithms and time
    Graph kruskal_mst(nVertices);
    Timer kruskal_timer;
    kruskal_timer.start();
    Kruskal(graph, kruskal_mst);
    kruskal_timer.end();
    
    
    Graph prim_mst(nVertices);
    Timer prim_timer;
    prim_timer.start();
    Prim(graph, prim_mst);
    prim_timer.end();
    
    Graph fib_mst(nVertices);
    Timer fib_timer;
    fib_timer.start();
    Fib(graph, fib_mst);
    fib_timer.end();
    
    // report timings
    fout << "Graph " << i+1 << ": " << nVertices << " vertices, " << nEdges << " edges" << std::endl;
    
    fout << "Kruskal: ";
    kruskal_timer.printElapsedSeconds(fout);
    if(DEBUG){
      std::cerr << "Kruskal MST:" << std::endl;
      kruskal_mst.showEdges();
      kruskal_mst.printEdges(fout);
      fout << "\tKRUSKALS: SUM OF EDGES = " << kruskal_mst.sumEdges() << std::endl;// " numEdges=" << kruskal_mst.numEdges() << std::endl;
    }

    fout << "Prim: ";
    prim_timer.printElapsedSeconds(fout);
    extractMST(prim_mst);
    if(DEBUG){
      std::cerr << "Prim MST:" << std::endl;
      prim_mst.showEdges();
      prim_mst.printEdges(fout);  
      fout << "\tPRIMS: SUM OF EDGES = " << prim_mst.sumEdges() << std::endl;//" numEdges=" << prim_mst.numEdges() << std::endl;
    }
    
    fout << "Fib Heap: ";
    fib_timer.printElapsedSeconds(fout);
    extractMST(fib_mst);
    if(DEBUG){
      std::cerr << "Fib MST:" << std::endl;
      fib_mst.showEdges();
      fib_mst.printEdges(fout);	      
      fout << "\tFIB-HEAP: SUM OF EDGES = " << fib_mst.sumEdges() << std::endl;// << " numEdges=" << fib_mst.numEdges() << std::endl;
    }

  }
  
  return 0;
}
void Solve( )
{
        printf( "%.2lf\n", Prim( ) );
}
Exemplo n.º 18
0
// argv = ["randmst", 0, numpoints, numtrials, dimension]
int main(int argc, char **argv)
{	
	// ensure correct usage
	if (argc != 5)
	{
		printf("Usage: ./randmst 0 numpoints numtrials dimension\n");
		return -1;
	}

	//TODO: check to see these are integers
	int dimension = atoi(argv[4]);

	// the number of verticies
	int n = atoi(argv[2]);

	int numtrials = atoi(argv[3]);

	// seed random number generator
	srand(time(NULL));

	// store the sum of the weights, to be divided by numtrials later
	float totalweight = 0;

	float cutoff= 8.0*((float)dimension)/((float) n+1);

	// the array of vertices (each entry is an array)
	float** verts = malloc(n*sizeof(float*));

	// initialize the vertices array
	for (int i = 0; i < n; i++)
	{
		verts[i] = malloc(dimension*sizeof(float));
	}

	// populate graph Graph
	vertex* Graph= malloc(n*sizeof(vertex));

	// perform numtrials number of trials and add weight to totalweight
	for (int t = 0; t < numtrials; t++ )
	{

		// initialize the vertices array
		for (int i = 0; i < n; i++)
		{
			for(int j = 0; j < dimension; j++)
			{
				verts[i][j] = (float)rand()/(float)RAND_MAX;
			}
		}


		// TODO: define prim
		queue q = init(n);
		queue* Q = &q;


		for (int i = 0; i < n; i++)
		{
			vertex v = {INFTY, NULL};
			Graph[i]=v;

			for(int j = 0; j < i; j++)
			{
				float squareDistance = sq_dist(verts[i], verts[j], dimension);
				if(squareDistance<cutoff)
				{
					AdjListNode* jNodePtr= newAdjListNode(j, squareDistance);
					AdjListNode* iNodePtr= newAdjListNode(i, squareDistance);
					if(!Graph[i].adjacentVertices)
					{
						Graph[i].adjacentVertices=jNodePtr;

					}
					else
					{
						jNodePtr -> next = Graph[i].adjacentVertices;
						Graph[i].adjacentVertices= jNodePtr;
					}
					if(!Graph[j].adjacentVertices)
					{
						Graph[j].adjacentVertices=iNodePtr;

					}
					else
					{
						iNodePtr -> next = Graph[j].adjacentVertices;
						Graph[j].adjacentVertices=iNodePtr;
					}
				}
			}
		}


		// populate the weights array with the euclidean distances
		// also build adjacency lists

		float treeweight = Prim(Q, Graph);
		totalweight += treeweight;

		// free shit
		for (int i = 0; i < n; i++)
		{
			AdjListNode* trash = Graph[i].adjacentVertices;
			while(!trash)
			{
				AdjListNode* t = trash->next;
				free(trash);
				trash = t;
			}
		}
		

	}
	// free willy
	for (int i = 0; i < n; i++)
	{
		free(verts[i]);
	}
	free(verts);
	free(Graph);
	// printf("%f\n", totalweight);
	// printf("The average weight of a %i-dimensional minimum spanning tree with with %i verticies is: \n", dimension, n);
	printf("%f %i %i %i \n", totalweight / numtrials, n, numtrials, dimension);

	
}
void  TArvore::Monta_Arvore(TSkaterGrafo *Grafo){
  Heap = new THeap(Grafo->MyGrafo->Size);
  Prim(Grafo);
};
Exemplo n.º 20
-1
void main(){
	MGraph N;
	printf("创建一个无向网:\n");
	CreateGraph(&N);
	DisplayGraph(N);
	Prim(N,"A");	
	DestroyGraph(&N);
	system("pause");
}