08 2011 档案
《编程之美》3.10分层遍历二叉树
摘要:#include<iostream>#include<vector>using namespace std;const int Max=30;class Node{public: Node(int d):data(d),left(NULL),right(NULL){} int data; Node *left; Node *right;};template<class T>class queue{public: queue(int i=Max):front(0),rear(0){Q=new T[Max];} void EnQueue(Node *p){Q[r
阅读全文
《编程之美》3.9重建二叉树
摘要:#includeusing namespace std;const int Max=20;struct Node{ Node *pLeft; Node *pRight; char chValue;};template class Stack{public: Stack(int s=Max):size(s),top(-1){a=new T[size];} ~Stack(){delete[] a;} void push(T x) { if(top-1) return a[top--]; } T getT() const { if(top!=-1) return a[top]; } boo...
阅读全文
《编程之美》3.9重建二叉树
摘要:#include<iostream>using namespace std;const int Max=20;struct Node{ Node *pLeft; Node *pRight; char chValue;};template <class T>class Stack{public: Stack(int s=Max):size(s),top(-1){a=new T[size];} ~Stack(){delete[] a;} void push(T x) { if(top<size-1) a[++top]=x; } T pop() { if(top>
阅读全文
还是畅通工程
摘要:题目参见:http://acm.hdu.edu.cn/showproblem.php?pid=1233Prim实现#include<iostream>using namespace std;const int Maxv=101;int map[Maxv][Maxv];int vset[Maxv];//顶点访问记录int lowcost[Maxv];int main(){ int sum; int n,i,j,cost; while(scanf("%d",&n) && n) { int m=n*(n-1)/2; while(m--) { s
阅读全文
最小生成树
摘要:普里姆算法(Prim)普里姆算法基本思想:从图中某个顶点V0开始,将V0到其他顶点的所有边当做候选边,重复以下步骤n-1次,使得其他n-1个顶点并入到树中:1、从候选边中挑选出权值最小的边输出,并将该边另一端相接的顶点V并入树中;2、考察所有剩余顶点Vi,如果(V,Vi)的权值比lowcost[Vi]小则用(V,Vi)的权值更新lowcost[Vi]。普里姆算法模板#define MAXN 101 //定义顶点的最大可达数int n, sum; //顶点数,权值和int map[MAXN][MAXN]; //以邻接矩阵存储权值void prim(){ int vset[MAXN]; //记录顶
阅读全文
浙公网安备 33010602011771号