邻接矩阵:构造有权图

该文描述了一个使用数组处理学生排序的问题,当接收到插入位置和学号时,需在正确位置插入学生学号。程序还涉及邻接矩阵的构建,用于表示有权图,包括无向图和有向图的处理。示例输入和输出展示了合法和非法的插入情况。
摘要由CSDN通过智能技术生成

问题描述 :

使用数组编程:

一群学生排成一行,输入一个位置和一个学号,在该位置插入一个学生。

第一个学生的位置为1,第n个学生的位置为n。

输入说明 :

第一行输入学生信息:

第一个整数n(0<=n<=100),表示共有n个学生,其后有n个整数,表示n个学生的学号

第二行及以后各行,每行输入两个整数,表示要插入的位置及学号。

输出说明 :

每次插入一个学生后,在一行里输出完整的学号序列,学号之间以一个空格分隔。

如果需要插入学生的位置不合法,则输出“invalid”(不包括双引号)

注:如果已有10个学生,插入在最前面,则插入位置为1,而要插入在最后面,则插入位置为11。


输入范例 :

10 50 51 52 53 54 49 4 5 10 11
12 22
11 20
3 2
1 3

输入范例 :

invalid
50 51 52 53 54 49 4 5 10 11 20
50 51 2 52 53 54 49 4 5 10 11 20
3 50 51 2 52 53 54 49 4 5 10 11 20

// 邻接矩阵:构造有权图.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
#include <string>
using namespace std;
template <class TypeOfVer, class TypeOfEdge>
class adjmatrix_graph {
public:
    int Vers;        //顶点数 
    int Edges;       //边数 
    vector<vector<TypeOfEdge>> edge;
    vector<TypeOfVer>  ver;    //存放结点值 
    TypeOfEdge noEdge;  //邻接矩阵中的∞的表示值
    string GraphKind;   //图的种类标志 


    adjmatrix_graph()
    {
        Vers = 0;
        Edges = 0;
        edge.clear();
        ver.clear();
        noEdge = 0;
    }
    bool PrintMatrix() {

        for (int i = 0; i < Vers; i++)
        {
            for (int j = 0; j < Vers - 1; j++)
                cout << edge[i][j] << " ";
            cout << edge[i][Vers - 1] << " ";
            cout << endl;
        }
        return 1;
    }//输出邻接矩阵 
    bool GraphisEmpty() { return Vers == 0; }  //判断图空否
    string GetGraphKind() { return GraphKind; }
    //bool GetVer(int u, TypeOfVer& data); //取得G中指定顶点的值 
    //int GetFirstAdjVex(int u, int& v); //返回G中指定顶点u的第一个邻接顶点的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1 
    //int GetNextAdjVex(int u, int v, int& w); //返回G中指定顶点u的下一个邻接顶点(相对于v)的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1
    //bool PutVer(int u, TypeOfVer data); //对G中指定顶点赋值 onst TypeOfVer& data); //往G中添加一个顶点 
    //int LocateVer(TypeOfVer data); //返回G中指定顶点的位置 

    bool CreateGraph(bool need_emp)
    {
        cin >> GraphKind;//图的类型 
        cin >> Vers;//结点数
        ver.resize(Vers);
        for (int i = 0; i < Vers; i++)//结点集
            cin >> ver[i];

        if (need_emp)
            cin >> noEdge;//无边标记

        vector<TypeOfEdge> line;//邻接矩阵初始化
        for (int j = 0; j < Vers; j++)
        {
            for (int i = 0; i < Vers; i++)
                line.push_back(noEdge);
            edge.push_back(line);
        }

        cin >> Edges;//边数
        vector<int> x_p, y_p, w_p;
        for (int i = 0; i < Edges; i++)
        {
            int c_x, c_y;
            cin >> c_x >> c_y;
            x_p.push_back(c_x);
            y_p.push_back(c_y);
        }
        //图的类型识别
        bool have_dir = false, have_w = false;
        if (GraphKind == "DG")//DG(有向图)
            have_dir = true, have_w = false;
        if (GraphKind == "DN")//DN(有向网)
            have_dir = true, have_w = true;
        if (GraphKind == "UDG")//UDG(无向图)
            have_dir = false, have_w = false;
        if (GraphKind == "UDN")//UDN(无向网)
            have_dir = false, have_w = true;

        if (have_w)
            for (int i = 0; i < Edges; i++)
            {
                int c_w;
                cin >> c_w;
                w_p.push_back(c_w);
            }

        for (int i = 0; i < Edges; i++)
        {
            if (have_dir == false)//无向图操作
            {
                if (have_w == true)//带权值的网的操作
                    edge[x_p[i]][y_p[i]] = edge[y_p[i]][x_p[i]] = w_p[i];
                else//无权值操作
                    edge[x_p[i]][y_p[i]] = edge[y_p[i]][x_p[i]] = 1;
            }
            else
            {
                if (have_w == true)//带权值的网的操作
                    edge[x_p[i]][y_p[i]] = w_p[i];
                else//无权值操作
                    edge[x_p[i]][y_p[i]] = 1;
            }
        }
        return 1;
    }
    int GetVerNum() { return Vers; }    //取得当前顶点数 
    int GetEdgeNum() { return Edges; }  //取得当前边数 
    bool Insert_Edge(int u, int v) {
        edge[u][v] = 1;
    }
    bool Insert_Edge(int u, int v, TypeOfEdge w) {
        edge[u][v] = w;
    }
    //bool DeleteVer(const TypeOfVer& data); //往G中删除一个顶点
    bool Delete_Edge(int u, int v) {
        edge[u][v] = noEdge;
    }
    bool Delete_Edge(int u, int v, TypeOfEdge w) {

        edge[u][v] = noEdge;
        
    }
    //void DFS_Traverse(int u); //DFS遍历(外壳部分)
    //void BFS_Traverse(int u); //BFS遍历
    //~adjmatrix_graph(); //析构函数 

};
int main()
{
    adjmatrix_graph<int, int>g;
    g.CreateGraph(1);
    cout << g.GetGraphKind() << endl;
    for (auto i : g.ver) {
        cout << i << " \n"[i == g.ver.back()];
    }
    cout << endl;
    g.PrintMatrix();

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值