gpt4 book ai didi

c++ - Kruskal 的算法实现

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:49:45 25 4
gpt4 key购买 nike

我打算用 C++ 实现 Kriskal 的算法,但是...

Unhandled exception at 0x0127160d in DAA.exe: 0xC0000005: Access violation reading location 0xdd2021d4.

它在 getRoot 函数的这一行停止:

while(cities[root].prev != NO_PARENT)

我认为问题出在城市数组中的数据。当我打印数组中的所有数据时,这不是我想要的。城市的名称是这样的“════════════════¤¤¤¤ллллллллю■ю■”和数字(int) - 像这样(-842150451)。以下是完整代码。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

#define NO_PARENT -1

struct city {
char name[11];
int prev;
};

struct path {
unsigned i, j, price;
};

bool comparsion(path p1, path p2) {
return p1.price > p2.price;
}

int getRoot(city *cities, int cityNumber) {
int root = cityNumber, tmp;

while(cities[root].prev != NO_PARENT)
root = cities[root].prev;

while(cityNumber != root) {
tmp = cityNumber;
cityNumber = cities[cityNumber].prev;
cities[tmp].prev = root;
}

return root;
}

bool isListed(city *cities, int n, char cityName[]) {
for(int i = 0; i < n; i++)
if(strcmp(cities[i].name, cityName))
return true;
return false;
}

int getCityNumber(city *cities, int n, char cityName[]) {
for(int i = 0; i < n; i++)
if(strcmp(cities[i].name, cityName))
return i;
return NO_PARENT;
}

int minPrice(city *cities, path *paths, int cityCount, int pathCount) {
unsigned minPrice = 0;
// sort paths by price
std::sort(paths, &paths[pathCount-1], comparsion);

for(int k = 0; k < pathCount; k++) {
printf("path: %d - %d\n", paths[k].i, paths[k].j);
int c1 = getRoot(cities, paths[k].i), c2 = getRoot(cities, paths[k].j);
if(c1 != c2) {
minPrice += paths[k].price;
cities[c2].prev = c1;
}
}

return minPrice;
}

int main() {
int n, m, k;
do {
scanf("%d %d %d", &n, &m, &k);
} while(n < 2 || n > 10001 || m < -1 || m > 100001 || k < -1 || k > 100001);

city* cities = (city*)malloc(n*sizeof(city));
path* paths = (path*)malloc((m + k)*sizeof(path));
int addCities = 0;
char city1[11], city2[11];
for(int i = 0; i < (m + k); i++) {
scanf("%s %s", city1, city2);

if(addCities < n && !isListed(cities, n, city1)) { // if city1 is not into cities
// add it
strcpy(cities[addCities].name, city1);
cities[addCities].prev = NO_PARENT;
addCities++;
}
paths[i].i = getCityNumber(cities, n, city1); // number of city1

if(addCities < n && !isListed(cities, n, city2)) { // if city2 is not into cities
// add it
strcpy(cities[addCities].name, city2);
cities[addCities].prev = NO_PARENT;
addCities++;
}
paths[i].j = getCityNumber(cities, n, city1); // number of city2

if(i >= m)
scanf("%d", &paths[i].price);
}

for(int i = 0; i < (m + k); i++)
printf("%s: %d\n", cities[i].name, cities[i].prev);

// Calculate min price
printf("%d ", minPrice(cities, paths, n, k + m));

system("pause");
return 0;
}

最佳答案

在 isListed() 和 getCityNumber() 中,您使用 strcmp() 来检查字符串是否相等。你这样做的方式有两个问题:

  1. 当两个字符串相等时,strcmp 返回 0,因此您需要检查 if( strcmp(...) == 0 )。这是 C 中的这些奇怪的事情之一。
  2. 在 malloc'ing 之后,您需要将 cities[i].name 设置为某物,例如“未命名”或只是“\0”。否则,将在未初始化的字符串上调用 strcmp - 如果它们不包含 11 个字符内的空字符,它将失败。在 malloc 行之后添加此代码:

    for( int i = 0 ; i < n ; ++ i ) {
    cities[ i ].name[ 0 ] = '\0';
    cities[ i ].parent = NO_PARENT;
    }

关于c++ - Kruskal 的算法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10671918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com