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

cout ステートメントで「abc」を出力することすらありません。Ford-Fulkerson アルゴリズムを実装しようとしています。ここでは、ファイルから読み取り、容量フロー マトリックスを初期化しています。その後、ここでは省略した maxflow 関数を呼び出しています。

4

2 に答える 2

1

配列アクセスが配列の境界内にあることを確認していません。これを行うコードを次に示します。定義を提供しなかったため、フローは容量および残余と同じように定義されていると想定しています。

さらに、C および C++ の配列は 1 ではなく 0 からインデックス付けされるため、ループ インデックスを 0 から開始するように変更しました。<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 に答える
1

現状では、コードは C++ よりも C に近いものです。(@jrok がコメントしたように) インデントやスペーシングなどはいくらか改善される可能性がありますが、かなり大きな変更がはるかに役立つと思います。無関係な「もの」の束を に詰め込む代わりに、main関数 (およびおそらくプログラム内の論理的な「部分」を表すクラス) に分割する必要があります。

matrix少なくとも見た目からすると、 (少なくとも) I/O 関数を備えたクラスから始めたいと思うかもしれません。おそらくstd::vector、データを格納するために a を使用する必要があるため、ファイルからサイズを読み取り、そのデータを保持するための適切な量のスペースを割り当てることができます。

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 に答える