2016级数据结构
2017-9-15 08:52
提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务二十 查找(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务二十 查找(一) 的任务作业。

提交了 2016级数据结构 任务 任务二十 查找(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

抱歉老师,任务十八
#include<iostream> using namespace std; #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define MAX_VERTEX_NUM 20 typedef int InfoType; typedef struct ArcNode { int adjvex; struct ArcNode *nextarc; InfoType weight; }ArcNode; typedef char VertexType; typedef struct VNode { VertexType data; ArcNode *firstarc; }VNode, AdjList[MAX_VERTEX_NUM]; typedef struct { AdjList vertices; int vexnum, arcnum; }ALGraph; typedef int ElemType; typedef struct { ElemType *base; ElemType *top; int stacksize; }SqStack; typedef int Status; #define OK 1 #define ERROR 0 Status InitStack(SqStack &S) { S.base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if (!S.base) { return ERROR; } S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK; } Status Push(SqStack &S, ElemType e) { if (S.top - S.base >= S.stacksize) { S.base = (ElemType *)realloc( S.base, (S.stacksize + STACKINCREMENT) * sizeof(ElemType) ); if (!S.base) { return ERROR; } S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++ = e; return OK; } ElemType Pop(SqStack &S) { if (S.base == S.top) { exit(1); } return *--S.top; } Status StackEmpty(SqStack S) { if (S.base == S.top) { return 1; } return 0; } void CreateALGraph(ALGraph &G) { cout << "顶点数目:"; cin >> G.vexnum; cout << "弧边数目:"; cin >> G.arcnum; for (int i = 0; i < G.vexnum; i++) { ArcNode *p_Tail; cout << "Seq " << i << "th:"; cin >> G.vertices[i].data; G.vertices[i].firstarc = NULL; int _do; cout << "Do:"; cin >> _do; if (!_do) { continue; } else { cout << "依次输入以" << G.vertices[i].data << "为尾的弧的弧头与权值:" << endl; for (int j = 0; j < _do; j++) { ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode)); p->nextarc = NULL; cin >> p->adjvex; cin >> p->weight; if (!G.vertices[i].firstarc) { G.vertices[i].firstarc = p; p_Tail = p; } else { p_Tail->nextarc = p; p_Tail = p; } } } } } //ve初始化 int ve[MAX_VERTEX_NUM]; Status IfNecessaryPath(ALGraph G,SqStack &T) { SqStack S; InitStack(S); InitStack(T); int Indegree[MAX_VERTEX_NUM] = {0}; for (int i = 0; i < G.vexnum; i++) { if (!G.vertices[i].firstarc) { continue; } else { ArcNode *p = G.vertices[i].firstarc; while(p) { Indegree[p->adjvex]++; p = p->nextarc; } } } //0入度的节点入栈S for (int i = 0; i < G.vexnum; i++) { if (!Indegree[i]) { Push(S,i); } } int count = 0; while (!StackEmpty(S)) { int j = Pop(S); //j号顶点入栈T并且计数 Push(T,j); count++; for (ArcNode *p = G.vertices[j].firstarc; p; p = p->nextarc) { if (--Indegree[p->adjvex] == 0) { Push(S, p->adjvex); } //MAX{ } if (ve[j] + p->weight > ve[p->adjvex]) { ve[p->adjvex] = ve[j] + p->weight; } } } if (count < G.vexnum) { return ERROR; } else { return OK; } } int vl[MAX_VERTEX_NUM]; Status CalculatePath(ALGraph G) { SqStack T; if (!IfNecessaryPath(G, T)) { return ERROR; } //初始化事件最晚时间 for (int i = 0; i < G.vexnum; i++) { vl[i] = ve[G.vexnum-1]; } //按拓扑排序的逆序求顶点 while (!StackEmpty(T)) { int j = Pop(T); for (ArcNode *p = G.vertices[j].firstarc; p; p = p->nextarc) { int k = p->adjvex; int w = p->weight; //MIN{ } if (vl[k] - w < vl[j]) { vl[j] = vl[k] - w; } } } for (int j = 0; j < G.arcnum; j++) { for (ArcNode *p = G.vertices[j].firstarc; p; p = p->nextarc) { int k = p->adjvex, w = p->weight; int el = vl[k]-w, ee = ve[j]; if (ee == el) { cout <<"<"<< G.vertices[j].data <<"," << G.vertices[k].data <<">"<< endl; } } } } int main(void) { ALGraph G; CreateALGraph(G); CalculatePath(G); return 0; } #include<iostream> using namespace std; #define MAX_VERTEX_NUM 20 typedef int VRType; typedef struct ArcCell { VRType adj; int *info; }ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; typedef char VertexType; typedef struct { VertexType vexs[MAX_VERTEX_NUM]; AdjMatrix arcs; int vexnum, arcnum; }MGraph; int LocateVex(MGraph G, VertexType V) { int i; G.vexs[G.vexnum] = V; for (i = 0; G.vexs[i] != V; i++); return i; } void CreateMGraph(MGraph &G) { cout << "顶点:"; cin >> G.vexnum; cout << "边数:"; cin >> G.arcnum; cout << "构造顶点向量" << endl << "请依次输入顶点:"; for (int i = 0; i < G.vexnum; i++) { cin >> G.vexs[i]; } for (int i = 0; i < G.vexnum; i++) { for (int j = 0; j < G.vexnum; j++) { G.arcs[i][j] = { 0,NULL }; } } cout << "邻接矩阵赋值" << endl; for (int k = 0; k < G.arcnum; k++) { cout << "依次输入第" << k + 1 << "条边的弧尾、弧头、权值:"; VertexType a, b; VRType w; cin >> a >> b >> w; int i = LocateVex(G, a); int j = LocateVex(G, b); G.arcs[i][j].adj = w; } } int SimpleLine(MGraph G, int m_i, int m_j, int k) { //最终矩阵 int Matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //过渡矩阵 int TMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; cout << "单位矩阵:" << endl; for (int i = 0; i < G.vexnum; i++) { for (int j = 0; j < G.vexnum; j++) { if (i == j) { TMatrix[i][j] = 1; } else { TMatrix[i][j] = 0; } cout << TMatrix[i][j] << " "; } cout << endl; } for (int n = 0; n < k; n++) { //矩阵的相乘次数 cout << n + 1 << "次:" << endl; for (int i = 0; i < G.vexnum; i++) { for (int j = 0; j < G.vexnum; j++) { int temp = 0; for (int k = 0; k < G.vexnum; k++) { temp += TMatrix[i][k] * G.arcs[k][j].adj; } Matrix[i][j] = temp; cout << Matrix[i][j] << " "; } cout << endl; } for (int i = 0; i < G.vexnum; i++) { for (int j = 0; j < G.vexnum; j++) { TMatrix[i][j] = Matrix[i][j]; } } cout << endl; } return Matrix[m_i][m_j]; } void IFHavePath(MGraph &G,int i,int j) { for (int n = 1; n <= G.arcnum; n++) { if (SimpleLine(G, i, j, n)) { cout << "长度为" << n << "的路径有。"<<endl; } else { cout << "长度为" << n << "的路径没有。"<<endl; } } } int main(void) { MGraph G; CreateMGraph(G); int i, j; cin >> i >> j; IFHavePath(G,i,j); return 0; }没有及时提交

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务十九 排序(一) 的任务作业。

提交了 2016级数据结构 任务 任务二十 查找(一) 的任务作业。