Kruskal和prime算法的类实现,图的遍历BFS算法。

一.图的遍历

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int n, m;  //行数和列数
const int maxn = 100;
char g[maxn][maxn];  //图
bool vis[maxn][maxn];  //访问标记数组,false表示点没有被访问过
int disx[4] = { 0,0,1,-1 };   //四个
int disy[4] = { 1,-1,0,0 };   //方向
int dx, dy;   //起点位置
int counter;  //黑瓷砖的数目
bool flag = false; //判断是否有起点
struct node {
    int x, y;
}p[maxn];
queue<node> que;
vector<node> vec;
void graph(int n, int m) {
    cout << "请输入行数和列数 :";
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> g[i][j];
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (g[i][j] == '@') {
                dx = i; dy = j;
                flag = true;
                break;
            }
        }
        if (flag == true) break;
    }
    if (flag) {
        cout << "起始点的坐标是";
        printf("( %d , %d )\n\n", dx, dy);
    }
    else cout << "没有起点" << endl;
}
void bfs() {
    vec.clear();
    que.empty();
    memset(vis, false, sizeof vis);
    graph(n, m);
    if (!flag) {
        counter = 0;
        cout << "黑瓷砖的数目是" << counter << endl;
    }
    else {
        counter = 1;
        node point;
        point.x = dx, point.y = dy;
        que.push(point);
        vis[point.x][point.y] = true;
        vec.push_back(point);
        while (!que.empty()) {
            node temp = que.front();
            node next;
            que.pop();
            for (int i = 0; i < 4; ++i) {
                int x = temp.x + disx[i];
                int y = temp.y + disy[i];
                next.x = x; next.y = y;
                if (g[next.x][next.y] == 'b'&&vis[next.x][next.y] == false) {
                    vis[next.x][next.y] = true;
                    que.push(next); ++counter;
                    vec.push_back(next);
                }
            }
        }
        cout << "黑瓷砖的数目是" << counter << endl;
        cout << endl;
        int number = 0;
        cout << "且到达每个黑瓷砖的顺序是 :" << endl;
        for (int i = 0; i < vec.size(); ++i) {
            if (i != vec.size() - 1) {
                ++number;
                printf("( %d , %d)", vec[i].x, vec[i].y);
                cout << " -> ";
                if (number % 5 == 0) cout << endl;
            }
            else printf("( %d , %d)\n", vec[i].x, vec[i].y);
        }
    }
}
int main() {
    bfs();
    return 0;
}

糟糕的上机,prim似乎有bug

二.求无向连通图的生成树

#pragma once
#include<algorithm>
#include <fstream>
#include<iostream>
using namespace std;
const int inf = 1000000000;
int p[100];
int maze[100][100];
template<class T>
struct Edge
{
    int u, v;
    int net;
    T key;
    Edge() :u(-1), v(-1), key(0) {}
};
template<class T>
class MST {
protected:
    Edge<T>*edge;
    int maxSize, e;
    int head[100];
public:MST(int sz = 100) :maxSize(sz), e(0) {
    edge = new Edge<T>[sz];
    memset(head, -1, sizeof(head));
}
       void addedge(Edge<T>& item);
       void establish();
       int Kruskal();
       int Prim(int n);
};
template<class T>
void MST<T>::addedge(Edge<T>& item)
{
    int frm = item.u, to = item.v, w = item.key;
    edge[e].u = frm;
    edge[e].v = to;
    edge[e].key = w;
    edge[e].net = head[frm];
    head[frm] = e++;
    edge[e].u = to;
    edge[e].v = frm;
    edge[e].key = w;
    edge[e].net = head[to];
    head[to] = e++;
}
template<class T>
void MST<T>::establish()
{
    for (int i = 0; i < 100; i++)
        for (int j = 0; j< 100; j++)
            if (i == j)maze[i][j] = 0;
            else maze[i][j] = inf;
    int u, v, w;
    Edge<int> temp;
    while (cin >> u >> v >> w) {
        if (u == 0 && v == 0 && w == 0)break;
        temp.u = u, temp.v = v, temp.key = w;
        maze[u][v] = maze[v][u] = w;
        addedge(temp);
    }
    return;
}
bool cmp(Edge<int> a, Edge<int> b)
{
    return a.key < b.key;
}
int find(int x)
{
    return p[x] == x ? x : p[x] = find(p[x]);
}
template<class T>
int MST<T>::Kruskal()
{

    ofstream outfile;
    
    outfile.open("outdata.txt");
    outfile << "Kruskal计算如下:\n";
    int ans = 0;
    for (int i = 0; i < 50; i++)p[i] = i;
    sort(edge, edge + e, cmp);
    for (int i = 0; i < e; i++) {
        int x = find(edge[i].u);
        int y = find(edge[i].v);
        if (x != y) { ans += edge[i].key;
    //  cout << edge[i].u << " " << edge[i].v << endl;
        outfile << edge[i].u << "    " << edge[i].v<<"     权值:  " << edge[i].key << endl;

              p[x] = y; }
    }
    outfile << "最小生成树中长度:   " << ans << endl;
    outfile.close();//关闭文件,保存文件。
    return ans;
}
Edge<int> dis[100];
int path[100];
template<class T>
int MST<T>::Prim(int n)
{
    
    bool vis[100];
    ofstream outfile;
    outfile.open("outdata.txt", ios::binary | ios::app | ios::in | ios::out);
    outfile << "Prim计算如下:\n\n\n";
    //int dis[100];
    for (int i = 0; i < 100; i++)
        dis[i].key = inf;
        memset(vis, 0, sizeof(vis));
        int ans = 0;
        dis[3].key = 0;
        while (1)
        {
            int k = 0;
            for (int j = 1; j <= n; j++)
            {
                if (!vis[j] && dis[j].key<dis[k].key)      //这一步找未收录顶点中dist值最小的
                    k = j;            
            }
            if (!k) break;
            vis[k] = 1;                        
            ans += dis[k].key;
            if (dis[k].u > 0)
                outfile <<"    "<<dis[k].u << "    " << dis[k].v << "     权值" << dis[k].key << "\n" << endl;
            for (int j = 1; j <= n; j++)
            {
                if (dis[j].key>maze[k][j])
                {
                    dis[j].key = maze[k][j];
                    dis[j].u = k;
                    dis[j].v = j;
                    path[j] = k;
                }
            }
        }
        outfile << "总长度    " << ans << endl;
        outfile.close();//关闭文件,保存文件。
        
        return ans;
}
#include<iostream>
#include"graph.h"
using namespace std;
int main()
{
    MST<int> m;
    int n;
    cin >> n;
    m.establish();
    m.Kruskal();
    m.Prim(n);
    getchar();
    return 0;
}
全部评论

相关推荐

诨号无敌鸭:恭喜佬,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了
点赞 评论 收藏
分享
09-26 17:07
门头沟学院 Java
点赞 评论 收藏
分享
联通 技术人员 总包不低于12
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务