voidfindWay(int targetLocation)const{ initStatus(); for (int i = 2; i <= targetLocation; ++i) { status[i] = 100000;//足够大了,用它来表示到初始节点最近的距离 }
queue<int> nodeQueue;
for (int i = 1; i <= nodeNum; ++i) { if (nodes[1][i] == 1){ nodeQueue.push(i); int wt = status[1] + weight[1][i]; if (wt<status[i]){ status[i] = wt; } } } while (!nodeQueue.empty()){ int nodeNow = nodeQueue.front(); nodeQueue.pop();//记下第一个元素并将其弹栈
for (int i = 0; i <= nodeNum; ++i) { if (nodes[nodeNow][i] == 1){//有边而且还没到达过 nodeQueue.push(i); int wt = status[nodeNow] + weight[nodeNow][i]; if (wt<status[i]){ status[i] = wt; } } } } cout<<status[targetLocation]<<endl; } };
intmain(){ cout << "Input" << endl; int m = 0, n = 0; scanf("%d,%d",&n,&m); Graph graph = *newGraph(); graph.nodeNum = n; int start = 0, end = 0, weight = 0; for (int i = 0; i < m; ++i) { scanf("%d,%d,%d",&start,&end,&weight); if (start<end){ graph.nodes[start][end] = 1; graph.weight[start][end] = weight; } else{ graph.nodes[end][start] = 1; graph.weight[end][start] = weight; }