資源簡介
從文件讀入。程序運行時輸入源點和目的節(jié)點,運行輸出在這兩點之間的所有路徑,并寫到輸出文件中,非常高效。
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
int?INFINITY?=?0xffffffff;
int?main()
{
//????ifstream?in(“Graph10-20.txt“);
????ifstream?in(“graphExtention.txt“);
????//ifstream?in(“Graph7-12(1).txt“);
????ofstream?out(“paths.txt“);
????if(!in)
????{
????????cout?<“Error?open?graph.txt“?<????????return?1;
????}
????int?n?m?num;
????int?i?j;
int?pathCount?=?0;
????in?>>?n;
????int?size?=?n?;
????int?**graph?=?new?int*[size];
????for(i?=?0;?i?????{
????????graph[i]?=?new?int[size];
????????//?for(j?=?0;?j?????}
????for(i?=?0;?i?????????for(j?=?0;?j?????????????in?>>?graph[i][j];
????cout?<“Graph?ok“?<
????cout?<“Input?your?start?and?end“?<????int?from?to;
????cin?>>?from?>>?to;
????assert(from?????assert(to?
????vector?vqueue;
????vector?parent;
????int?pos?=?0;
????int?cur;
????int?top;
????int?count;
????stack?path;
????bool?visited?=?false;
????vector?whichOne;
????vector?nodeCount;
????for(i?=?0;?i?
????vqueue.push_back(from);
????parent.push_back(-1);
whichOne.push_back(1);
//nodeCount[from]++;
bool?changed?=?false;
????while(pos?????{
????????top?=?vqueue[pos++];
????????//?push?every?node?adjecent?to?the?queue
????????for(i?=?0;?i?????????{
????????????if(i?==?from)?continue;
????????????if(graph[top][i]?!=?0)
????????????{
????????????????//visited?before?
????????????????{
????????????????????visited?=?false;
????????????????????cur?=?pos?-?1;
????????????????????while(parent[cur]?!=?-1?&&?!visited)
????????????????????{
????
評論
共有 條評論