Chinaunix首页 | 论坛 | 博客
  • 博客访问: 532421
  • 博文数量: 252
  • 博客积分: 6057
  • 博客等级: 准将
  • 技术积分: 1635
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-21 10:17
文章分类

全部博文(252)

文章存档

2013年(1)

2012年(1)

2011年(32)

2010年(212)

2009年(6)

分类: C/C++

2010-05-04 16:59:57


数据结构课程,图,用关键路径方法解决AOE网络工程规划问题(条件约束下的周游)
我的代码如下
[#include
#include
using namespace std; 


class vertex 
{
  public:
  int n,etime,ltime;
};

class Edge 
{
  public:
  int n,weight,from,to;
  Edge(){from=-1;to=-1;weight=0;};
};


class Graph 

public:
  int numVertex;
  int numEdge;
  Graph(int numVert)
  {
  numVertex=numVert;
  numEdge=0;
  };
  ~Graph(){};
   
  bool IsEdge(Edge oneEdge)
  {
  return oneEdge.weight>0 && oneEdge.to>=0;
  };  

};


class Graphm:public Graph 
{

public:
  int **Matrix;
  Graphm(int numVert):Graph(numVert)
  {
  int i,j;
  Matrix=new int * [numVert];
  for(i=0;i   Matrix[i]=new int[numVert];
  for (i=0;i   for(j=0;j   Matrix[i][j]=0;
  };
  ~Graphm()
  {
  for(int i=0;i   delete []Matrix[i];
  delete []Matrix;
  };
   
};

class solve 

private:
  Graphm* pro;
  vertex * event;
  Edge * task;
public:
  solve(fstream file);
  ~solve(){delete[]pro;delete[]task;};
  int MaxPath(int n);
  int ReversalPath(int n);
  void output();
};

solve::solve(fstream file)
{
if(file.eof()) cout<<"End."< else
{
  int i;
  int temp;
  file>>temp;
  pro=new Graphm(temp);
  file>>pro->numEdge;
  event=new vertex[temp];
  for(i=0;i   {
  event[i].n=i;
  event[i].etime=event[i].ltime=0;

  }

  task=new Edge[pro->numEdge];
  for(i=0;inumEdge;i++)
  {
  file>>task[i].from>>task[i].to>>task[i].weight;
  pro->Matrix[task[i].from][task[i].to]=task[i].weight;
  }
}
}

int solve::MaxPath(int n)
{
  if(n==0) return 0;
  int temp=0;
  for(int i=0;inumVertex-1;i++)
  {
  if(pro->Matrix[i][n]&&(pro->Matrix[i][n]+MaxPath(i)>temp))
  temp=pro->Matrix[i][n]+MaxPath(i);
  }
  event[n].etime=temp;

  return temp;
}

int solve::ReversalPath(int n) 
{
  int i;
  if(n==pro->numVertex-1) return 0;
  int temp=0;
  for(i=1;inumVertex;i++)
  {
  if(pro->Matrix[n][i]&&(ReversalPath(i)+pro->Matrix[n][i])>temp)
  temp=ReversalPath(i)+pro->Matrix[n][i];
  }
  event[n].ltime=event[pro->numVertex-1].etime-temp;

  return temp;

}

void solve::output() 
{
  int i;
  MaxPath(pro->numVertex-1);
  ReversalPath(0);
event[pro->numVertex-1].ltime=event[pro->numVertex-1].etime;

  for(i=0;inumEdge;i++)
  if(task[i].weight==event[task[i].to].ltime-event[task[i].from].etime)
  cout<<"A_"<   cout<<"\n"<numVertex-1].etime<}


int main()
{
  fstream file;
  file.open("input.txt");
  solve * hehe;
  while(!file.eof())
  {
hehe=new solve(file);
  hehe->output();
  delete []hehe;
hehe=NULL;

  }
  file.close();
  return 0;
}
]
编 译通过
程序是Debug版本,环境VC6 Enterprise Edition。

运行时报错如下
Debug Assertion Failed!
文件路径……
Expression:_CrtIsValidHeapPointer(pUserData)
For information……

运行只给出第一组数据输出,即报错;
将input文件每组数据截下来输入给出的输出都是正确的,但 连续输入无法运行;
input文件一组输入格式如下(int型)
m n//分别为节点数、边数
之后n行
from to weight//边的from、to、weight

我在网上找了找,没有针对性的解决,有的说是指针的问题。
希望大家帮我指出问 题,谢谢。



回复于:2009-05-21 14:01:29
access violation指针无效,估计是该次执行完毕后指针指向的空间释放掉了,
下次执行 时又没有申请新的空间,造成指针无效了.再多看看指针用的地方吧.


_CrtIsValidHeapPointer(pUserData)奇怪的问题?



Debug   Assertion   Failed!  
  Program:   d:\dd\err.exe  
  File:dbgheap.c  
  Line:1132  
  Expression:   _CrtIsValidHeapPointer(pUserData)  
   
  err是我的程序.  
   
  程序是调用一个DLL,   在DLL的接口只添加一个参数char*   op,   好奇怪的问题.  
  请各位在侠指点!

sinall()回 复于 2005-12-19 14:18:12 得分 20

如上文所述(MSDN):  
  _CrtIsValidHeapPointer确认内存地址在本地堆。……如果静态链接C运行库,那么,dll拥有一个独立于应用程序(调用它的exe) 的本地堆。(所以你上面的程序会Debug   Assertion   Failed),如果没有定义_DEBUG,那么_CrtIsValidHeapPointer将被预处理器移除。  
   
  所以方法有二:  
  1、动态链接C运行库。  
  2、设置为release版本。  

lxpws(你被耍了)回 复于 2005-12-20 14:29:41 得分 20

是说此指针与指定堆不符,原因楼上有说明(根运行库的使用有关系),估计是欲在dll内释放exe中分配的内存,或相反。。。建议内存谁分配谁释 放。

问题因应为11楼所指出的。在dll内释放exe中分配的内存。


指 针和引用的区别总结

1.从现象上看:指针在运行时可以改变其所指向的值,而引用一旦和某个对象绑定后就不再改变
2.从内存分配上看:程序为指针变量分配内存区 域,而引用不分配内存区域
3.从编译上看:程序在编译时分别将指针和引用添加到符号表上,符号表上记录的是变量名及变量所对应地址。指针变量在符 号表上对应的地址值为指针变量的地址值,而引用在符号表上对应的地址值为引用对象的地址值。符号表生成后就不会再改,因此指针可以改变指向的对象(指针变 量中的值可以改),而引用对象不能改。

阅读(2187) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~