刷题记录

六度空间

仅仅是菜鸟做个记录而已,代码没有参考性,大神请略过。

//数据结构学习记录
PTA链接原题链接

`

#include 
#include 

#define MAXN 10001			//最大顶点数 
#define INFINITY 65535        /* ∞设为双字节无符号整数的最大值65535*/
#define ERROR 0 

int G[MAXN][MAXN], Nv, Ne;
int Visited[MAXN];/* 标记V已访问 */
typedef int Vertex;

struct Node {
	int Data;
	struct Node *Next;
};
 
struct QNode {
	struct Node *rear;
	struct Node *front;
};
typedef struct QNode *Queue;

int IsEmpty(Queue Q);
Queue CreateQueue();
int DeleteQ(Queue PtrQ);
void InsertQ(int item, Queue PtrQ);

void BuildGraph ();
void SDS ();
bool IsEdge(Vertex V, Vertex W);
int BFS (Vertex V);
void Build ();

int main()
{
	//Build(); 
	BuildGraph();
	for (int i = 0; i < MAXN; i++) {
		Visited[i] = false; 
	}
	SDS();
	return 0;
}

void Build () //Build()做测试用
{	//用邻接矩阵表示图 
	int i, j, v1, v2; 
	//初始化图 
	//scanf("%d %d", &Nv, &Ne);
	Nv = 10; Ne = 9;
	G[0][0] = INFINITY;
	for (i=1; i<=Nv; i++) {
		for (j=1; j<=Nv; j++) {
			G[i][j] = INFINITY;
		}
	}
	//插入边 
	G[1][2] = 1; G[2][1] = 1;
	G[2][3] = 1; G[3][2] = 1;
	G[3][4] = 1; G[4][3] = 1;
	G[4][5] = 1; G[5][4] = 1;
	G[5][6] = 1; G[6][5] = 1;
	G[6][7] = 1; G[7][6] = 1;
	G[7][8] = 1; G[8][7] = 1;
	G[8][9] = 1; G[9][8] = 1;
	G[9][10] = 1; G[10][9] = 1;

/*	for (i=1; i<=Ne; i++) {
		scanf("%d %d", &v1, &v2);
		G[v1][v2] = 1;
		G[v2][v1] = 1;
	} */
	//图建立完成 
}
void BuildGraph ()
{	//用邻接矩阵表示图 
	int i, j, v1, v2; 
	//初始化图 
	scanf("%d %d", &Nv, &Ne);
	G[0][0] = INFINITY;
	for (i=1; i<=Nv; i++) {
		for (j=1; j<=Nv; j++) {
			G[i][j] = INFINITY;
		}
	}
	//插入边 
	for (i=1; i<=Ne; i++) {
		scanf("%d %d", &v1, &v2);
		G[v1][v2] = 1;
		G[v2][v1] = 1;
	} 
	//图建立完成 
}

void SDS ()
{
	int V, count;
	double out;
	for (V=1; V<=Nv; V++) {
	for (int i = 0; i < MAXN; i++) {
		Visited[i] = false; 
	}
		count = BFS(V);
		double f=100.00*count/Nv;
		printf("%d: %.02f%%\n", V, f);
		//printf("%d: 有%d个符合理论\n", V, count);
		out = count/Nv;
		//printf("%d: %.2lf\n", V, out);
		//Output(count/Nv);
	}
	/*	V=4;
	count = BFS(V);
	printf("%d有%d个符合理论\n", V, count);
	*/
}

/* IsEdge(Graph, V, W)检查是否图Graph中的一条边,即W是否V的邻接点。  */
bool IsEdge(Vertex V, Vertex W)
{
	return G[V][W]front == NULL);
};
 
Queue CreateQueue() {
	Queue PtrQ;
	PtrQ = (Queue)malloc(sizeof(struct QNode));
	struct Node *rear;
	struct Node *front;
	rear = (Node*)malloc(sizeof(struct Node));
	rear = NULL;
	front = (Node*)malloc(sizeof(struct Node));
	front = NULL;
	PtrQ->front = front;
	PtrQ->rear = rear;
	return PtrQ;
};
 
int DeleteQ(Queue PtrQ) {
	struct Node *FrontCell;
	int FrontElem;
 
	if (IsEmpty(PtrQ)) {
		printf("队列空");
		return ERROR;
	}
	FrontCell = PtrQ->front;
	if (PtrQ->front == PtrQ->rear)
		PtrQ->front = PtrQ->rear = NULL;
	else {
		PtrQ->front = PtrQ->front->Next;
	}
	FrontElem = FrontCell->Data;
	free(FrontCell);
	return FrontElem;
}
 
void InsertQ(int item, Queue PtrQ) {
	struct Node *FrontCell;
	FrontCell = (Node*)malloc(sizeof(struct Node));
	FrontCell->Data = item;
	FrontCell->Next = NULL;
 
	if (IsEmpty(PtrQ)) {
		PtrQ->front = FrontCell;
		PtrQ->rear = FrontCell;
	}
	else {
		PtrQ->rear->Next = FrontCell;
		PtrQ->rear = FrontCell;
	}
};

你可能感兴趣的:(刷题记录)