您现在的位置:首页 > 教案格式 > 正文

浮点数 关于Prim算法求最小生成树的问题

2018-01-03 09:01 网络整理 教案网

浮点数_单精度浮点数表示方法_浮点数的二进制表示

#include<iostream> #include<fstream> using namespace std; #define MAX 100 #define MAXCOST 0x7fffffff int graph[MAX][MAX]; int prim(int graph[][MAX], int n) {int lowcost[MAX];int mst[MAX];int i, j, min, minid,...#include<iostream> #include<fstream> using namespace std; #define MAX 100 #define MAXCOST 0x7fffffff int graph[MAX][MAX]; int prim(int graph[][MAX], int n) {int lowcost[MAX];int mst[MAX];int i, j, min, minid, sum = 0;for (i = 2; i <= n; i++){lowcost[i] = graph[1][i];mst[i] = 1;}mst[1] = 0;for (i = 2; i <= n; i++){min = MAXCOST;minid = 0;for (j = 2; j <= n; j++){if (lowcost[j] < min && lowcost[j] != 0){min = lowcost[j];minid = j;}}cout << mst[minid] << "-" << minid << "=" << min << endl;lowcost[minid] = 0;for (j = 2; j <= n; j++){if (graph[minid][j] < lowcost[j]){lowcost[j] = graph[minid][j];mst[j] = minid;}}}return sum; } int main() {int i, j, k, m, n;int cost;ifstream in("Karate.txt");in >> m >> n; for (i = 1; i <= m; i++){for (j = 1; j <= m; j++){graph[i][j] = MAXCOST;}}for (k = 1; k <= n; k++){in >> i >> j >> cost;graph[i][j] = cost;graph[j][i] = cost;}cost = prim(graph, m);return 0; } 我在用这个代码输出最小生成树时,数据的权值为整型时就没有问题,但是如果权值为浮点数时就有问题,不知道怎么修改。浮点数。。浮点数