我试图弄清楚如何将特定格式的邻接列表文本文件读入C。该图是有向图的。
格式如下:
10 //number of nodes
a 25 c 15 d 20 //a -> c and a -> d with capacities 15 and 20 respectively
b 25 a 20 e 10
c 0 f 20 d 10
d 0 f 15 g 25
e 0 d 10 g 5
f 0 h 10 i 20
g 0 i 20 j 10
h -10
i -20 h 10 j 10
j -15
//The 25s and the negative numbers are production and demand of each node.
//I have to create a dummy source and a sink to perform ford-fulkerson.
//for example: SourceNode -> a with capacity 25 and SourceNode-> b with capacity 25
//Likewise, h -> SinkNode with capacity 10 etc...
我的问题不是实现Ford-Fulkerson远胜于成功读取该文件。
到目前为止,我在这里,我猜这太可怕了。我不检查是否连接了另一个节点,也不考虑生产/需求,而且通常卡住了:
void read_f() {
int a,b,c,d,e,f,i,j;
FILE* input = fopen("input.txt","r");
//no of nodes
fscanf(input,"%d",&n);
//Empty capacity matrix
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
capacity[i][j] = 0;
}
}
//What to do here?
for(???)
{
fscanf(input,"%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f);
capacity[a][c] = d;
capacity[a][e] = f;
}
fclose(input);
}