Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

跪求:我总是RUNTIME ERROR ,有没有哪个哥们帮我看看,是什么原因啊?

Posted by 200730740307 at 2009-03-15 20:17:29 on Problem 1094
我总是RUNTIME ERROR ,有没有哪个哥们帮我看看,是什么原因啊?

#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#include <stack>
#include<queue>
// 利用 拓扑排序解题
using namespace std;

#define MAX_NAME 3//顶点字符串的最大长度+1
typedef char VertexType[MAX_NAME];//图的顶点字符类型,最大长度2
typedef int InfoType;//顶点的权值类型
#define MAX_VERTEX_NUM 26
typedef enum {DG,DN,AG,AN}GraphKind;//{有向图,有向网,无向图,无向网}
typedef struct ArcNode{
	int adjvex;//该弧指向的顶点位置
	
	struct ArcNode *nextarc;//指向下一条弧的指针
	InfoType *info;//网的权值指针
}ArcNode;
//表结点

typedef struct {
	char data;//顶点信息
	ArcNode *firstarc;//第一表结点的地址,指向第一依附该结点的弧的指针
}Vnode,AdjList[MAX_VERTEX_NUM];//头结点

typedef struct {
	AdjList vertices;
	int  vexnum,arcnum;//图的当前 的顶点数和弧数
	int kind;//图的种类标志
}ALGraph ;

void FindInDegree(ALGraph G,int *indegree){
	int j=0;
	for(int i =0;i<G.vexnum;i++){
		ArcNode *p;
		if(G.vertices[i].data>0)
			for(p=G.vertices[i].firstarc;p;p=p->nextarc)
			{
				indegree[p->adjvex]++;
			}
	}
}

int TopologicalSort(ALGraph &G,int k,stack<int>  &s,queue<char> &q ){
	//有向图G采用邻接表的存储结构
	//若无回路,则输出G的一个拓扑排序 ,并返回 1 ,否则返回 0
	int i;
	int * indegree=(int *)malloc(G.vexnum*sizeof(int));// 定义indegree数组
	for(i =0;i<G.vexnum;i++)
		indegree[i]=0;//初始化
	FindInDegree(G,indegree);// 对各顶点求入度indegree
	
	int num ;
	bool sure=true;
	for (num=0,i =0;i<G.vexnum;i++)
		if(!indegree[i]&&G.vertices[i].data>0){s.push(i);num++;}//入度为0的顶点进栈
		//if(num>1){cout<<"Sorted sequence cannot be determined."<<endl;return 0;}
        //if(num==0){cout<<"Inconsistency found after 2 relations."<<endl;return 0;}
		
		int count=0;// 对输出 的顶点计数
		while (!s.empty())
		{
			if(s.size() > 1)
				//return 0;不能直接返回0表示不确定,还要继续看完看能不能发现环
				sure = false;
			
			i=s.top();
			s.pop();
			q.push(G.vertices[i].data);count++;//i号顶点进队并计数
			ArcNode *p;
			for(num=0,p=G.vertices[i].firstarc;p;p=p->nextarc)
			{
				int k2 =p->adjvex;// 对i 号顶点的每个邻接点的入度减1
				if(!(--indegree[k2])){
					s.push(k2);//若入度减为0 则让入栈
					num++;
				}
			}//if(num>1){cout<<"Sorted sequence cannot be determined."<<endl;return 0;}
            //  if(num==0){cout<<"Inconsistency found after 2 relations."<<endl;return 0;}
		}
		free(indegree);
		if(count<k){
while (!s.empty())
				s.pop();
			
			while (!q.empty())
				q.pop();
			return -1;}//先判断是否有环,k是实际有效的结点 数
		else  {
			if(sure && q.size()==G.vexnum)//无环时再看是否已经能排好序,能排好序有两种情况即多种拓扑排序中的一种或者仅确的
				
				//一种这里必须是后者才行,否则返回不确定
			{ while (!s.empty())
				s.pop();
			
			
				return 1;}
			
			else
			{while (!s.empty())
				s.pop();
			
			while (!q.empty())
				q.pop();
			return 0;}
			
			
			/**cout<<"Sorted sequence determined after 4 relations: ";
			while (!q.empty())
			{char a=q.front();	cout<<a;q.pop();}
			cout<<'.'<<'endl;
			return 1;*/
		}
		
		
}

//CreateGraph
int  CreateGraph(ALGraph *G,int n,int m){
	int i,j=0,t,k=0;//k 为计数变量
	bool flag=true;
	int ok=0;
	ArcNode *p;
	ArcNode *temp;
	(*G).kind=0;
    (*G).vexnum=n;
	(*G).arcnum=m;
	stack<int>  s;
	queue<char> q ;
	for (i=0;i<(*G).arcnum;i++)
	{
		//构建顶点向量
		
		char a,b;
		cin>>a>>b>>b;
		//a=getchar();
		//a=getchar();
		if(a>'Z'||a<'A')cout<<"Your intput is not right.The program is going to exit.";
		for( j=0;j<k;j++)
			if(a==(*G).vertices[j].data)break;
			if(j>=k) {(*G).vertices[j].data=a; (*G).vertices[j].firstarc=NULL;k++;}//j弧尾
			//getchar();
			//a=getchar();
			if(b>'Z'||b<'A')cout<<"Your intput is not right.The program is going to exit.";
			for( t=0;t<k;t++)
				if(b==(*G).vertices[t].data)break;
				if(t>=k)    {(*G).vertices[t].data=b;(*G).vertices[t].firstarc=NULL;k++;}//t 弧头
				p=(ArcNode*)malloc(sizeof(ArcNode));
				for(temp=(*G).vertices[j].firstarc;temp;temp=temp->nextarc)
					
					if(t==temp->adjvex)
					{
						flag=false;
						break;
					}
					if(!flag) continue;
					p->adjvex=t;
					p->nextarc=(*G).vertices[j].firstarc;//插入在表头
					(*G).vertices[j].firstarc=p;
					//	getchar();
					int sign=TopologicalSort((*G),k,s,q);
					if(sign==1&&!ok){
						printf("Sorted sequence determined after %d relations: ",k);
						while (!q.empty())
						{char a=q.front();	cout<<a;q.pop();}
						cout<<'.'<<endl;
						ok=1;
					}
					if(sign==-1&&!ok){
						printf("Inconsistency found after %d relations.\n",k);
						ok=1;
					}
					
	}
	return ok;
	
}



int  main(){
	int n,m,ok;
	cin>>n>>m;
	
	while(n||m)
	{  
		ALGraph g;
	//	g.vertices=(Vnode*)malloc(n*sizeof(Vnode));
		ok=CreateGraph(&g,n,m);// k为图实际有效的结点
		if(!ok){cout<<"Sorted sequence cannot be determined."<<endl;}
		cin>>n>>m;
		
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator