我的任务是找到可以作为服务器运行的计算机,因为它具有到其他计算机的最短路径。
输入第一行有多台计算机。 其他行有连接的计算机数量。 最后一行总是0 0 例如:
5
5 4
1 2
4 3
1 4
0 0
当所有计算机获得消息时,输出第一行包含最少时间(跳转)。 第二行包含可用作服务器的所有计算机数量。 例如:
2
1 4
在我的本地计算机上,正在运行。但是当我在大学服务器中发送我的代码进行测试时,2/5公共测试正在运行,但是3/5给了我错误:
Caught fatal signal 11
stderr
*** Error in `solution': corrupted size vs. prev_size: 0x0000000001a36940 ***
======= Backtrace: =========
[0x4bb811]
[0x4c47bb]
[0x4c7d87]
[0x42688e]
[0x429f47]
[0x42aa4d]
[0x400817]
[0x49a0b6]
[0x49a2aa]
[0x401039]
======= Memory map: ========
00400000-005a3000 r-xp 00000000 fd:01 776099 /box/solution
007a2000-007ab000 rw-p 001a2000 fd:01 776099 /box/solution
007ab000-007b0000 rw-p 00000000 00:00 0
01a1f000-01a42000 rw-p 00000000 00:00 0 [heap]
2b1ae7340000-2b1ae7349000 rw-p 00000000 00:00 0
7fffd3bb6000-7fffd3bd7000 rw-p 00000000 00:00 0 [stack]
7fffd3bfb000-7fffd3bfe000 r--p 00000000 00:00 0 [vvar]
7fffd3bfe000-7fffd3c00000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
我找不到自己的错误,希望有人可以提供帮助。 我的代码:
#include <fstream>
using namespace std;
int BreathFirstSearch(int** matrix, int size, int row){//function to walk around matrix and find every path from vertex to vertex
int pathLen=0;
int q[size+1], dis[size+1], queue_input = 0, queue_output = 0, look_at;
bool stop = false;
for(int i=1;i<=size+1;i++){
q[i]=0;//queue for element I need to look at
dis[i]=-1; //to now if I have looked at it and to count how many jumps there are between row vertex and every other vertex
}
dis[row] = 0;//the row from matrix can go to itself with 0 jumps
q[queue_input]=row;
while(q[queue_output]!=0){
look_at=q[queue_output];
queue_output++;
if(!stop){
for(int col=1; col<=size; col++){//walking through every matrix collon
if(matrix[look_at][col]==1 && dis[col]==-1){//if matrix collon has 1 to show that there is string between it and the row element. also check if it have already been ir queue
queue_input++;
q[queue_input]=col;//add collon to queue
dis[col]=dis[look_at]+1;//add +1 to path from row to collon element
}
}
}
int s=0;
for(int i=1; i<=size; i++){
for(int k=1; k<=size; k++){
if(q[k]==i){
s++;//count how many elements row has
break;
}
}
}
if(s==size) stop=true; //if row has as much elements as matrix size, then I know that there wont be more and can stop
}
int maxDis = 0;
for (int i = 1; i <= size; i++){
if (dis[i] > maxDis){
maxDis = dis[i];
}
}
return maxDis;//return only farthest distance
}
int main(){
ifstream fin ("serveris.in");
ofstream fout ("serveris.out");
if (fin.is_open()){
int size, x, y;
fin >> size; //read size of matrix from file
int** matrix = new int*[size];
for(int i = 1; i <= size; ++i){
matrix[i] = new int[size];
}
int *result = new int[size];
for(int i=1; i<=size; i++){//fill matrix with zeros
for(int k=1; k<=size; k++){
matrix[i][k]=0;
}
}
while(fin.good()){
fin >> x >> y;
if(x!=0){//fill 1 in their places
matrix[x][y]=1;
matrix[y][x]=1;
}
}
for(int row=1; row<=size; row++){//go through every row of matrix
result[row]=BreathFirstSearch(matrix, size, row);//in result array I put in only farthest path
}
int min = result[1];
for(int i=1; i<=size; i++){//find shortest path between vertex
if(result[i]<min)min=result[i];
}
fout<<min<<endl;
for(int i=1; i<=size; i++){//find in witch row/s this path was
if(result[i]==min)fout<< i<<' ';
}
for(int i=1; i<=size; i++) delete[] matrix[i];
delete[] matrix;
delete[] result;
}
fin.close();
fout.close();
}
示例是在服务器上给出错误。这个是有效的:
4
1 2
1 3
1 4