我编写了有关实现Prim's Algorithm的代码,并构建了 从顶点1开始的最小生成树。
以下是输入格式:
第一行是案件数。
每种情况都以顶点数开头。
假设我这样输入:
2
4
0,1,0,4
1,0,3,2
0,3,0,5
4,2,5,0
6
0,7,9,0,0,6
7,0,0,2,0,0
9,0,0,0,5,0
0,2,0,0,0,3
0,0,5,0,0,4
6,0,0,3,4,0
这意味着我有2个测试用例,而'4'意味着有4行顶点。
示例:
0,1,0,4
1,0,3,2
0,3,0,5
4,2,5,0
会是这样
|0||1||2||3|
0| 0 1 0 4
1| 1 0 3 2
2| 0 3 0 5
3| 4 2 5 0
0到4需要4个距离,2-2需要3个距离,等等
第一个输出是正确的
0-1 1
1-2 3
1-3 2
但是第二个输出也与第一个输出相同,这意味着错误
0-1 1
1-2 3
1-3 2
这是我的代码
#include <stdio.h>
#include <limits.h>
#include<stdbool.h>
// Number of vertices in the graph
#define V 100
// A utility function to find the vertex with
// minimum key value, from the set of vertices
// not yet included in MST
int minKey(int key[], bool mstSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
int v;
for (v = 0; v < V; v++)
if (mstSet[v] == false && key[v] < min)
min = key[v], min_index = v;
return min_index;
}
// A utility function to print the
// constructed MST stored in parent[]
int printMST(int parent[], int n, int graph[V][V])
{
int i;
char front1;
char back2;
//printf("Edge \tWeight\n");
for (i = 1; i < V; i++)
if(graph[i][parent[i]]!=0)
{
front1 = parent[i];
front1 += 16;
back2 = i;
back2 +=16;
printf("%d-%d %d \n", parent[i], i, graph[i][parent[i]]);
}
else
break;
}
// Function to construct and print MST for
// a graph represented using adjacency
// matrix representation
void primMST(int graph[V][V])
{
int i,count,v;
// Array to store constructed MST
int parent[V];
// Key values used to pick minimum weight edge in cut
int key[V];
// To represent set of vertices not yet included in MST
bool mstSet[V];
// Initialize all keys as INFINITE
for (i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] = false;
// Always include first 1st vertex in MST.
// Make key 0 so that this vertex is picked as first vertex.
key[0] = 0;
parent[0] = -1; // First node is always root of MST
// The MST will have V vertices
for (count = 0; count < V-1; count++)
{
// Pick the minimum key vertex from the
// set of vertices not yet included in MST
int u = minKey(key, mstSet);
// Add the picked vertex to the MST Set
mstSet[u] = true;
// Update key value and parent index of
// the adjacent vertices of the picked vertex.
// Consider only those vertices which are not
// yet included in MST
for (v = 0; v < V; v++)
// graph[u][v] is non zero only for adjacent vertices of m
// mstSet[v] is false for vertices not yet included in MST
// Update the key only if graph[u][v] is smaller than key[v]
if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
}
// print the constructed MST
printMST(parent, V, graph);
}
int main()
{
printf("%c",one1);
int i,j,limit1,limit2;
int column_a=0;
int row_a=0;
char datawithco[1000];
char *pch;
int graph[V][V];
scanf("%d",&limit1);
for(i=0; i<limit1; i++)
{
scanf("%d",&limit2);
for(j=0; j<limit2; j++)
{
scanf("%s",datawithco);
pch = strtok (datawithco,",");
while (pch != NULL)
{
//printf ("%s\n",pch);
graph[row_a][column_a] = atoi(pch);
column_a++;
pch = strtok (NULL, ",");
}
column_a=0;
row_a++;
}
primMST(graph);
graph[row_a][column_a] = '\0';
}
return 0;
}
第二个输出应该是
0-5 6
5-3 3
3-1 2
5-4 4
4-2 5
答案 0 :(得分:1)
完成每种情况的MST后,您无需重置row_a
变量。
for(i=0; i<limit1; i++)
{
scanf("%d",&limit2);
for(j=0; j<limit2; j++)
{
scanf("%s",datawithco);
pch = strtok (datawithco,",");
while (pch != NULL)
{
//printf ("%s\n",pch);
graph[row_a][column_a] = atoi(pch);
column_a++;
pch = strtok (NULL, ",");
}
column_a=0;
row_a++;
}
primMST(graph);
graph[row_a][column_a] = '\0';
row_a=0; //<---added
}
此外,您在每种情况下都不会重设graph
。对于您提供的这些特殊情况,这很好,因为第二种情况下的顶点数大于第一种情况下的顶点数,因此第二种情况下的像元将覆盖第一种情况下的像元,否则前一种情况留下的单元格将被视为有效单元格,这显然是错误的。因此,您应该将图形重置添加到primMST()
函数中,或在每种情况的开始处添加