在 Linux 下面用 g++ 编译,运行通过。
但是程序跑到最后,该算出的正确结果也已经算出来了,可是就在最后一句出现。
*** Error in `./KL2': corrupted double-linked list: 0x08553170 *** 的错误提示。
#include<iostream>
#include<fstream>
#include<string>
#include<cmath>
#include<time.h>
#include<vector>
#define M 14
#define N 2
using std::cout;
using std::endl;
using std::vector;
using namespace std;
int main(int argc, char* argv[])
{
int i,j,Numnode,Numedge;
ifstream openfile(argv[1]);
openfile>>Numnode>>Numedge; // Still running
int **data;
data=new int* [Numedge];
for(i=0;i<Numedge+1; i++)
data[i]=new int [2];
data[0][0]=Numnode;
data[0][1]=Numedge;
for(i=1;i<Numedge+1;i++)
{
openfile>>data[i][0]>>data[i][1];
}
cout<<"-1"<<endl;
Numnode=data[0][0]; // The number of nodes in this graph
Numedge=data[0][1]; // The number of edges in this graph
//vector<vector<int> > c(Numnode+2,vector<int>(Numnode+2));
int **c;
c=new int* [Numnode];
for(i=0;i<Numnode; i++)
c[i]=new int [Numnode];
int cindex1,cindex2;
for(i=0;i<Numnode;i++)
{
for(j=0; j<Numnode; j++)
{
c[i][j]=0;
}
}
cout<<"Numedge"<<Numedge<<endl;
for(i=1;i<Numedge;i++)
{
cindex1=data[i][0]-1;
cindex2=data[i][1]-1;
cout<<"i="<<i<<endl<<cindex1<<cindex2<<endl;
cout<<c[cindex1][cindex2]<<endl;
c[cindex1][cindex2]=1;
c[cindex2][cindex1]=1;
cout<<c[cindex1][cindex2]<<endl;
}
cout<<"00000"<<endl;
//openfile.close(); //// We stopped here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
cout<<"0"<<endl;
//determine a balanced initial partition of the nodes into sets A and B
int *a;
a=new int [Numnode/2];
int *b;
b=new int [Numnode/2];
for(i=0;i<Numnode;i++)
{
if (i<Numnode/2) // odd
a[i]=i+1; // First label is 1, not 0
else // even
b[i-Numnode/2]=i+1; // First label is 1, not 0
}
/////////////////////////// Calculate D for each a ///////////////////////////
int Ec, Enc; // D=Ec-Enc
int k;
int *Da;
Da=new int [Numnode/2];
int *Db;
Db=new int [Numnode/2];
int n, swapi,swapj,swaptemp;
int gmax,g,Gmax,Kmax,m;
int **SwapTable;
SwapTable=new int*[Numnode/2+1];
for(i=0;i<Numnode/2+1;i++)
SwapTable[i]=new int[2];
int p=0;
cout<<"1"<<endl;
do
{
cout<<"The current do loop is:"<<p<<endl;
p++;
int* ignore_a;
ignore_a=new int[Numnode/2];
int* ignore_b;
ignore_b=new int[Numnode/2]; //// We stopped here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for(i=0;i<Numnode/2;i++)
{
ignore_a[i]=0;
ignore_b[i]=0;
}
cout<<"baga"<<endl;
Gmax=0;
Kmax=0;
for(n=0; n<Numnode/2; n++) // Every time you remove a pair of a and b, so after |V|/2 iterations, the loop stops.
{
/////////////////////////// Calculate D for each a ///////////////////////////
for(i=0;i<Numnode/2;i++)
{
Ec=0; Enc=0;
for(j=1;j<Numedge+1;j++) // Scan through data file except the first line
{
if((a[i]==data[j][0])||(a[i]==data[j][1])) // If an edge involving a particular node in A1 is detected
{
if(a[i]==data[j][0]) // Then data[j][1] will be check based on a and b
{
for(k=0;k<Numnode/2;k++)
{
if((data[j][1]==a[k])) // &&(ignore_a[k]==0)
Enc++;
if((data[j][1]==b[k])) // &&(ignore_b[k]==0)
Ec++;
}
}
else // Then data[j][0] will be check based on a and b
{
for(k=0;k<Numnode/2;k++)
{
if((data[j][0]==a[k])) //&&(ignore_a[k]==0)
Enc++;
if((data[j][0]==b[k])) //&&(ignore_b[k]==0)
Ec++;
}
}
}
}
Da[i]=Ec-Enc;
}
cout<<"2"<<endl;
/////////////////////////// Calculate D for each b //////////////////////
for(i=0;i<Numnode/2;i++)
{
Ec=0; Enc=0;
for(j=1;j<Numedge+1;j++) // Scan through data file except the first line
{
if((b[i]==data[j][0])||(b[i]==data[j][1])) // If an edge involving a particular node in A1 is detected
{
if(b[i]==data[j][0]) // Then data[j][1] will be check based on a and b
{
for(k=0;k<Numnode/2;k++)
{
if((data[j][1]==b[k])) // &&(ignore_b[k]==0)
Enc++;
if((data[j][1]==a[k])) //&&(ignore_a[k]==0)
Ec++;
}
}
else // Then data[j][0] will be check based on a and b
{
for(k=0;k<Numnode/2;k++)
{
if((data[j][0]==b[k])) //&&(ignore_b[k]==0)
Enc++;
if((data[j][0]==a[k])) //&&(ignore_a[k]==0)
Ec++;
}
}
}
}
Db[i]=Ec-Enc;
}
gmax=-31410;
for(i=0;i<Numnode/2;i++)
{
if(ignore_a[i]==0)
{
for(j=0;j<Numnode/2;j++)
{
if(ignore_b[j]==0)
{
g=Da[i]+Db[j]-2*c[a[i]-1][b[j]-1];
if(g>gmax)
{
gmax=g; swapi=i; swapj=j;
}
}
}
}
}
Gmax=Gmax+gmax;
swaptemp=a[swapi];
a[swapi]=b[swapj];
b[swapj]=swaptemp;
ignore_a[swapi]=1;
ignore_b[swapj]=1;
if(Gmax>Kmax)
{
Kmax=Gmax;
m=n;
}
cout<<"The max n (m)="<<m<<endl;
SwapTable[n][0]=swapi;
SwapTable[n][1]=swapj;
}
cout<<"The maximun G is:"<<Kmax<<endl;
//////// Set a and b to the original problem /////////////////////////////////////////////
if(Kmax>0)
{
for(i=0;i<Numnode;i++)
{
if (i<Numnode/2) // odd
a[i]=i+1; // First label is 1, not 0
else // even
b[i-Numnode/2]=i+1; // First label is 1, not 0
}
for(k=0;k<m+1;k++)
{
swapi=SwapTable[k][0];
swapj=SwapTable[k][1];
swaptemp=a[swapi];
a[swapi]=b[swapj];
b[swapj]=swaptemp;
}
}
for(i=0;i<Numnode/2;i++)
{
cout<<"a"<<i<<"="<<a[i]<<" b"<<i<<"="<<b[i]<<"\n"<<endl;
}
}
while(Kmax>0);
cout<<"hahaha"<<endl;
openfile.close();
return 0;
}
输入文件叫做 data.txt, 里面的内容是:
8 13
1 2
1 5
1 6
2 5
2 6
3 6
3 7
3 8
3 4
4 7
4 8
5 6
7 8
已经卡在这里很久了,请高手帮忙看下,为什么会出现这种错误。
1 个解决方案
#1
因为
for(i=0;i<Numedge+1; i++)
data[i]=new int [2];
所以
data=new int* [Numedge];
要改为
data=new int* [Numedge+1];
for(i=0;i<Numedge+1; i++)
data[i]=new int [2];
所以
data=new int* [Numedge];
要改为
data=new int* [Numedge+1];
#1
因为
for(i=0;i<Numedge+1; i++)
data[i]=new int [2];
所以
data=new int* [Numedge];
要改为
data=new int* [Numedge+1];
for(i=0;i<Numedge+1; i++)
data[i]=new int [2];
所以
data=new int* [Numedge];
要改为
data=new int* [Numedge+1];