数据结构课程设计:管道铺设施工的最佳方案代码

  1. 类定义:
    void CreateGraph(ALGraph &G)
     {
     	int i,j,k;
     	char vi,vj;
     	WeightType weight;
     	ArcNode*p,*q;
     	std::cout<<"请输入顶点个数,边数和图的类型:\n";
     	std::cin>>G.vexnum >>G.arcnum >>G.kind ;
     	for(i=0;i<G.vexnum ;i++)
     	{
     		std::cout<<"请输入各个顶点:\n";
     		std::cin>>G.vertices[i].data ;
     		G.vertices[i].firstarc=NULL;
    	 }
    	 for(k=0;k<G.arcnum ;k++)
    	 {
    	 	std::cout<<"请输入两顶点和其边的权值:\n";
    	 	std::cin>>vi>>vj>>weight;
    	 	i=LocateVex(G,vi);
    	 	j=LocateVex(G,vj);
    	 	p=(ArcNode*)malloc(sizeof(ArcNode));
    		p->adjvex=j;
    		p->weight=weight;
    		p->nextarc=G.vertices [i].firstarc;
    		G.vertices [i].firstarc=p;
    		if(G.kind == 2)
    		{
    			q=(ArcNode*)malloc(sizeof(ArcNode));
    			q->adjvex=i;
    			q->weight=p->weight;
    			q->nextarc=G.vertices [j].firstarc;
    			G.vertices [j].firstarc=
  • 9
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
弗洛伊德算法找出超市最佳选址位置 #include #include using namespace std; const int maxv=20; #include #include #include #include #include struct point { char name[10]; int id; int x; int y; //int dst[5]; }; struct node { char src[10]; char dst[10]; int cost; }; /////最小生成树 struct mintree//结构体记录最小生成树的相关信息 { int begin,end;//起点 终点 int length;//边的权值 }; const int maxweight=9999; class Graphm//图的矩阵表式 { public: int map[maxv][maxv]; //某点到某点两点间的的距离 int mark[maxv]; //加入进来的点的集合 point Vert[maxv];//顶点信息 int Edge[maxv][maxv];//存放边的信息 int numV;//顶点数 int numE;//边数 int path[maxv][maxv];//每对顶点之间路劲 int dist[maxv][maxv];//c每对顶点之间的路径 void shortpath(int n); mintree tree[maxv]; Graphm(); char* findbyid(int i); int findbyname(char m[]); void CreatG();//创建无向图 void print();//矩阵显示图的信息 void draw(); void Prim(); node Node[maxv]; int edgenumE; int minx(int x,int y); }; int Graphm::minx(int x,int y) { if(x<y) return x; else return y; } Graphm::Graphm()//矩阵初始化 { for(int i=0;i<maxv;i++) for(int j=0;j<maxv;j++) { if(i==j) Edge[i][j]=0; else Edge[i][j]=maxweight;} numV=0; numE=0; } int Graphm::findbyname(char m[]) { int j; for(j=0;j<numV;j++) { if(strcmp(Vert[j].name,m)==0) return Vert[j].id; } } char* Graphm::findbyid(int i) { int j; for(j=0;j<numV;j++) { if(Vert[j].id==i) return Vert[j].name; } } void Graphm::CreatG()//创建无向图 { FILE *fp;//定义文件流指针,用于打开读取的文件 int i,k; if((fp=fopen("data.txt","rb")) == NULL ) { /* 打开文件 */ printf("Cannot open file !"); return ; }//只读方式打开文件text.txt fscanf(fp,"%d",&numV);//读入点数 fscanf(fp,"%d",&numE); cout<<numV<<endl; cout<<numE<<endl; char q,z; for(i=0;i<numV;i++)//从文件中读取信息 显示到屏幕 { fscanf(fp,"%s",&Vert[i].name);//读取d顶点信息 Vert[i].id=i+1; cout<<Vert[i].name<<endl; } for(k=0;k<numE;k++)//从文件中读取信息保存到矩阵中 { int dis; char i[10],j[10]; fscanf(fp,"%s""%s""%d",i,j,&d
根据提供的引用内容,以下是使用C++实现Prim算法和Kruskal算法求解最小生成树的代码: 1. Prim算法 ```c++ #include <iostream> #include <cstring> using namespace std; const int N = 510; int g[N][N], dist[N]; bool st[N]; int prim(int n) { memset(dist, 0x3f, sizeof dist); int res = 0; for (int i = 0; i < n; i++) { int t = -1; for (int j = 1; j <= n; j++) if (!st[j] && (t == -1 || dist[t] > dist[j])) t = j; if (i && dist[t] == 0x3f3f3f3f) return -1; if (i) res += dist[t]; st[t] = true; for (int j = 1; j <= n; j++) dist[j] = min(dist[j], g[t][j]); } return res; } int main() { int n, m; cin >> n >> m; memset(g, 0x3f, sizeof g); while (m--) { int a, b, c; cin >> a >> b >> c; g[a][b] = g[b][a] = min(g[a][b], c); } int t = prim(n); if (t == -1) puts("impossible"); else cout << t << endl; return 0; } ``` 2. Kruskal算法 ```c++ #include <iostream> #include <algorithm> using namespace std; const int N = 200010; int p[N]; struct Edge { int a, b, w; bool operator< (const Edge &W) const { return w < W.w; } }edges[N]; int find(int x) { if (p[x] != x) p[x] = find(p[x]); return p[x]; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) cin >> edges[i].a >> edges[i].b >> edges[i].w; sort(edges, edges + m); for (int i = 1; i <= n; i++) p[i] = i; int res = 0, cnt = 0; for (int i = 0; i < m; i++) { int a = edges[i].a, b = edges[i].b, w = edges[i].w; a = find(a), b = find(b); if (a != b) { p[a] = b; res += w; cnt++; } } if (cnt < n - 1) puts("impossible"); else cout << res << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大灰狼家的小绵羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值