生成图二

http://www.cnblogs.com/uniqueliu/archive/2011/08/18/2145121.html

 

学习了这篇博主如何在vs2008中显示程序运行时间,很有用啊,不过我原本是想找能在控制台上输出,现就这样吧。

View Code
  1 #include <iostream>
2 #include <vector>
3 #include <time.h>
4
5
6 using namespace std;
7 using std::vector;
8
9
10 struct ArcBox
11 {
12 int headvex,tailvex;
13 ArcBox *hlink,*tlink;
14 float weight;
15 };
16
17 template <class TElemType>
18 class Graph
19 {
20 public:
21
22 void CreateOlgraph();
23 void PrintOlgraph();
24
25
26
27 private:
28
29 struct ArcBox
30 {
31 int headvex,tailvex;
32 ArcBox *hlink,*tlink;
33 float weight;
34 };
35
36
37 template <class TElemType>
38 struct Vertex
39 {
40 TElemType data;
41 ArcBox *firstin,*firstout;
42 };
43
44 struct Olgraph
45 {
46 int vexnum,arcnum;
47 Vertex<TElemType> *vex;
48 };
49 Olgraph olgraph; //有向图的十字链表存储结构
50
51
52 };
53
54
55
56 template <class TElemType>
57 void Graph<TElemType>::CreateOlgraph() // 创建十字链表
58 {
59 ArcBox *p,*q,*l,*s,*w,*r,*g,*f,*t;
60 //输入k
61 int a,b,k,i;
62 cout << "请输入k的值" << endl;
63 cin >> k;
64 a = 3*k + 3;//顶点数
65 b = 6*k + 2;//边数
66 olgraph.vexnum = a;//将输入的顶点数赋给图的顶点数
67 olgraph.arcnum = b;//将输入的边数赋给图的边数
68 olgraph.vex = (Vertex<TElemType> *)malloc(olgraph.vexnum * sizeof(Vertex<TElemType>));
69 for(i = 0;i < olgraph.vexnum;i++)
70 {
71 olgraph.vex[i].data = i+1;//给每个顶点赋值
72 olgraph.vex[i].firstin = olgraph.vex[i].firstout = NULL;
73
74 }
75
76
77 for(i =1;i <= k; i++)
78 {
79 p = (ArcBox *)malloc(sizeof(ArcBox));
80 p->tailvex = 0;
81 p->tlink = olgraph.vex[0].firstout;olgraph.vex[0].firstout = p;
82 p->headvex = i;
83 p->hlink = olgraph.vex[i].firstin;olgraph.vex[i].firstin = p;
84
85 }
86
87 for(i =1;i <= k; i++)
88 {
89 w = (ArcBox *)malloc(sizeof(ArcBox));
90 w->headvex = k+1;
91 w->hlink = olgraph.vex[k+1].firstin;olgraph.vex[k+1].firstin = w;
92 w->tailvex = i;
93 w->tlink = olgraph.vex[i].firstout;olgraph.vex[i].firstout = w;
94
95 }
96
97 for(i =k +1;i <= 2*k; i++)
98 {
99 r = (ArcBox *)malloc(sizeof(ArcBox));
100 r->headvex = 2*k+1;
101 r->hlink = olgraph.vex[2*k+1].firstin;olgraph.vex[2*k+1].firstin = r;
102 r->tailvex = i;
103 r->tlink = olgraph.vex[i].firstout;olgraph.vex[i].firstout = r;
104
105 }
106
107 for(i =2*k+2;i <= 3*k+1; i++)
108 {
109 g = (ArcBox *)malloc(sizeof(ArcBox));
110 g->tailvex = 2*k+1;
111 g->tlink = olgraph.vex[2*k+1].firstout;olgraph.vex[2*k+1].firstout = g;
112 g->headvex = i;
113 g->hlink = olgraph.vex[i].firstin;olgraph.vex[i].firstin = g;
114
115 }
116 for(i =2*k+2;i <= 3*k+1; i++)
117 {
118 f = (ArcBox *)malloc(sizeof(ArcBox));
119 f->headvex = 3*k+2;
120 f->hlink = olgraph.vex[3*k+2].firstin;olgraph.vex[3*k+2].firstin = f;
121 f->tailvex = i;
122 f->tlink = olgraph.vex[i].firstout;olgraph.vex[i].firstout = f;
123
124 }
125
126 q = (ArcBox *)malloc(sizeof(ArcBox));
127 q->headvex = 0;
128 q->hlink = olgraph.vex[0].firstin;olgraph.vex[0].firstin = q;
129 q->tailvex = 2*k;
130 q->tlink = olgraph.vex[2*k].firstout;olgraph.vex[2*k].firstout = q;
131
132
133 l = (ArcBox *)malloc(sizeof(ArcBox));
134 l->headvex = k+1;
135 l->hlink = olgraph.vex[k+1].firstin;olgraph.vex[k+1].firstin = l;
136 l->tailvex = 2*k+2;
137 l->tlink = olgraph.vex[2*k+2].firstout;olgraph.vex[2*k+2].firstout = l;
138
139 s = (ArcBox *)malloc(sizeof(ArcBox));
140 s->headvex = 2*k+1;
141 s->hlink = olgraph.vex[2*k+1].firstin;olgraph.vex[2*k+1].firstin = s;
142 s->tailvex = 3*k+2;
143 s->tlink = olgraph.vex[3*k+2].firstout;olgraph.vex[3*k+2].firstout = s;
144
145 for(i = k + 1;i <= 2*k-1; i++)
146 {
147 t = (ArcBox *)malloc(sizeof(ArcBox));
148 t->tailvex =i;
149 t->tlink = olgraph.vex[i].firstout;olgraph.vex[i].firstout = t;
150 t->headvex = i + 1;
151 t->hlink = olgraph.vex[i + 1].firstin;olgraph.vex[i + 1].firstin = t;
152
153 }
154
155
156
157
158
159 }
160
161 //end create
162
163
164 template <class TElemType>
165 void Graph<TElemType>::PrintOlgraph() // 输出十字链表
166 {
167 int k;
168 ArcBox *p;
169 cout << " The Vertex Outdegree = > " << endl;
170 for(k = 0; k <olgraph.vexnum; k ++)
171 {
172 p = olgraph.vex[k].firstout;
173 cout << " Vertex" << k+1 << ": ";
174 while(p != NULL)
175 {
176
177 cout << "<"<<(p->tailvex)+1 << "," << (p->headvex)+1<< ">" << " ";
178 p = p-> tlink;
179
180 }
181 cout << endl;
182
183 }
184
185 cout << endl; cout << endl; cout << endl;
186
187 cout << " The Vertex Indegree = > " << endl;
188 for(k = 0; k <olgraph.vexnum; k ++)
189 {
190 p = olgraph.vex[k].firstin;
191 cout << " Vertex" << k+1 << ": ";
192 while(p != NULL)
193 {
194
195 cout << "<"<<(p->tailvex)+1 << "," << (p->headvex)+1<< ">" << " ";
196 p = p-> hlink;
197
198 }
199 cout << endl;
200
201 }
202
203 }
204
205 void cost_time()
206 {
207 //#define CLOCKS_PER_SEC 1000
208 // cout<<"调用该程序所花费的时间为:"<<clock()/CLOCKS_PER_SEC<<"秒\n";
209 cout<<"调用该程序所花费的时间为:"<<clock()<<"毫秒\n";
210 }
211
212 int main()
213 {
214
215
216 int a =1;
217 Graph<int> gph;
218 gph.CreateOlgraph();
219 gph.PrintOlgraph();
220 //gph.findcircle();
221 //gph.findvector(vector <ArcBox*> & ve);
222
223 cost_time();
224 return 0;
225 }



posted @ 2012-02-25 00:19  uniquews  阅读(195)  评论(0编辑  收藏  举报