0
int main()
{
    int i,j,k,l,m;
    int maxflow=0;
    int first,second,capac;

    int residual[100][100];
    //reading from file
    FILE*fp;
    fp=fopen("ford.txt","r");
    fscanf(fp,"%d %d",&node,&edge);
    cout<<"nodes are"<<node<<"\n"<<"edges are"<<edge<<"\n";
    for(j=1;j<=node;j++)
    {
        for(k=1;k<=node;k++)
        {
            capacity[j][k]=0;
        }  
    }
    for(i=0;i<edge;i++)
    { 
        fscanf(fp,"%d %d %d",&first,&second,&capac);
        cout<<first<<"->"<<second<<"="<<capac<<"\n"; //it is printing this
        capacity[first][second]=capac;//crashes here at last i/p i.e.1=edge-1

    }
    cout<<"abc"; //this is not printed
    for(l=1;l<=node;l++)
    {
        for(m=1;m<=node;m++)
        {
            flow[l][m]=capacity[l][m];
            flow[m][l]=0;
        }
    }
    return 0;
}

It is not even printing the "abc" in the cout statement. I am trying to implement the Ford-Fulkerson algorithm. Here I am reading from a file and initializing capacity flow matrices. After that I am calling maxflow function, which I have omitted here.

4

2 回答 2

1

就目前而言,您的代码更像是 C 而不是 C++。虽然(正如@jrok 评论的那样)缩进和间距之类的东西可以使用一些改进,但我认为更大的变化会更有帮助。与其将一堆不相关的“东西”打包成main,您真的需要/想要将事物拆分为函数(可能还有类来表示程序中的逻辑“片段”)。

至少从外观上看,您可能希望从matrix具有(至少)I/O 函数的类开始。那可能应该使用 astd::vector来存储数据,因此它可以从文件中读取大小,然后分配适当的空间来保存该数据。

class matrix { 
    std::vector<int> data;
    size_t rows;
    size_t columns;
public:
    std::istream &read(std::istream &infile);
    std::ostream &write(std::ostream &outfile);
};

有了一个不错的矩阵定义,您现在拥有的大部分代码似乎都会变成这样的:

std::ifstream in("ford.txt");

matrix capacity;
capacity.read(in); // or `in >> capacity;`

matrix flow = capacity;
于 2012-07-03T16:11:51.307 回答
1

您没有检查以确保您的数组访问在数组的范围内。这是一些执行此操作的代码。我假设流量的定义与容量和残差相同,因为您没有为其提供定义。

此外,我已将循环索引更改为从 0 开始,因为 C 和 C++ 中的数组从 0 开始索引,而不是 1。如果您打算保持数组的第一行和第一列不变,那么您可以将它们更改回来将<s更改为<=s。

此外,我使用 std::endl 刷新输出缓冲区。如果程序在执行该语句后很快崩溃,则输出缓冲可能导致 cout<<"abc" 执行但不输出任何内容。

#define WIDTH 100
#define HEIGHT 100
int capacity[WIDTH][HEIGHT];
int flow[WIDTH][HEIGHT];
int node, edge;
int main() {
    int maxflow=0;
    int first,second,capac;
    int residual[WIDTH][HEIGHT];
    //reading from file
    FILE*fp;
    fp=fopen("ford.txt","r");
    fscanf(fp,"%d %d",&node,&edge);
    cout<<"nodes are"<<node<<"\n"<<"edges are"<<edge<<"\n";
    //check if node is out of bounds...
    if(node >= WIDTH || node >= HEIGHT) {
        //if it is, warn
        cout<<"Warning: node is at least "
            <<WIDTH<<" or "<<HEIGHT<<"."
            <<std::endl;
    }
    //prevent accessing out-of-bounds locations by comparing
    // xIdx and yIdx against WIDTH and HEIGHT, respectively
    for(int xIdx=0; xIdx < node && xIdx < WIDTH; xIdx++) {
        for(int yIdx=0; yIdx < node && yIdx < HEIGHT; yIdx++) {
            capacity[xIdx][yIdx]=0;
        }  
    }
    for(i=0;i<edge;i++) { 
        fscanf(fp,"%d %d %d",&first,&second,&capac);
        cout<<first<<"->"<<second<<"="<<capac<<"\n";
        if(first < WIDTH && second < HEIGHT) {
            capacity[first][second]=capac;
        }
        //Stop execution if one of first or second is out of bounds
        else {
            cout<<"ERROR: ["<<first<<"]["<<second<<"]"
                <<" not within ["<<WIDTH<<"]["<<HEIGHT<<"]"
                <<std::endl;
            return -1;
        }
    }
    cout<<"abc"<<std::endl;
    for(int xIdx=0; xIdx < node && xIdx < WIDTH; xIdx++) {
        for(int yIdx=0; yIdx < node && yIdx < HEIGHT; yIdx++) {
            flow[xIdx][yIdx]=capacity[xIdx][yIdx];
            flow[yIdx][xIdx]=0;
        }
    }
    return 0;
}
于 2012-07-03T18:47:12.520 回答