它给出了分段错误。我正在尝试实现福特弗里克森算法,这里我正在从文件中读取

时间:2012-07-03 15:23:43

标签: c++ ford-fulkerson

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;
}

它甚至没有在cout语句中打印“abc”。我正在尝试实施Ford-Fulkerson算法。在这里,我正在读取文件并初始化容量流矩阵。之后我调用maxflow函数,我在这里省略了。

2 个答案:

答案 0 :(得分:1)

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

至少从外观上看,您可能希望从具有(至少)I / O功能的matrix类开始。这可能应该使用std::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;

答案 1 :(得分:1)

您没有检查以确保您的阵列访问位于数组的范围内。这是一些代码。我假设流量定义与容量和残差相同,因为您没有为它提供定义。

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

另外,我使用std :: endl来刷新输出缓冲区。如果程序在执行该语句后很快崩溃,则输出缓冲可能导致cout&lt;&lt;“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;
}
相关问题