zoukankan      html  css  js  c++  java
  • 采用邻接矩阵表示法创建无向网

    //采用邻接矩阵表示法创建无向网
    #include <iostream>
    using namespace std;
    
    #define MaInt 32767
    #define MVNum 100
    #define OK 1
    
    typedef char VerTextType;
    typedef int ArcType;
    
    typedef struct {
    	VerTextType Vexs[MVNum];
    	ArcType arcs[MVNum][MVNum];
    	int vexnum, arcnum;
    }AMGrach;
    
    int LocateVex(AMGrach G, VerTextType v) {
    	for (int i = 0;i < G.vexnum;i++) {
    		if (G.Vexs[i] == v)
    			return i;
    	}
    	return -1;
    }
    
    int CreateUDN(AMGrach& G) {
    	int i, j, k;
    	cout << "请输入总顶点数,总边数,以空格隔开:";
    	cin >> G.vexnum >> G.arcnum;
    	cout << endl;
    
    	cout << "输入点的名称,如a" << endl;
    		for (i = 0;i < G.vexnum;i++) {
    			cout << "input the " << i << " name";
    			cin >> G.Vexs[i];
    		}
    	cout << endl;
    
    	for (i = 0;i < G.vexnum;i++)
    		for (j = 0;j < G.vexnum;++j)
    			G.arcs[i][j] = MaInt;
    	cout << "输入边依附的顶点及权值,如 a b 5" << endl;
    
    	for (k = 0;k < G.arcnum;++k) {
    		VerTextType v1, v2;
    		ArcType w;
    		cout << "input the" << (k + 1) << " side of weigth";
    		cin >> v1 >> v2 >> w;
    		i = LocateVex(G, v1);
    		j = LocateVex(G, v2);
    		G.arcs[i][j] = w;
    		G.arcs[j][i] = G.arcs[i][j];
    	}
    	return 0;
    }
    
    int main() {
    	cout << "采用邻接矩阵表示法创建无向网";
    	AMGrach G;
    	int i, j;
    	CreateUDN(G);
    	cout << endl;
    	for (i = 0;i < G.vexnum;i++) {
    		for (j = 0;j < G.vexnum;++j) {
    
    			if (j != G.vexnum - 1) {
    
    				if (G.arcs[i][j] != MaInt) {
    					cout << G.arcs[i][j] << "	";
    				}
    				else {
    					cout << "~" << "	";
    				}
    
    			}
    			else {
    
    				if (G.arcs[i][j] != MaInt)
    					cout << G.arcs[i][j] << endl;
    				else
    					cout << "~" << endl;
    			}
    
    		}
    	}
    	cout << endl;
    	return 0;
    }
    
  • 相关阅读:
    UDP:用户数据报协议(User Datagram Protocol)
    线程池的使用
    SQL Server表和模式空间使用情况http://www.51myit.com/thread2466911.html
    bytetobmp and bmptobyte(Image)
    c# TCP例子转载
    POJ 4047Garden
    NYOJ 102 次方求模
    Sum
    POJ 1094 Sorting It All Out(经典拓扑,唯一排序)
    POJ 2387 Til the Cows Come Home(Dijkstra)
  • 原文地址:https://www.cnblogs.com/ygjzs/p/11877585.html
Copyright © 2011-2022 走看看