資源簡介
最短路徑問題是圖論中的一個經典問題,其中的Dijkstra算法一直被認為是圖論中的好算法,但有的時候需要適當的調整Dijkstra算法才能完成多種不同的優化路徑的查詢。
對于某城市的公交線路,乘坐公交的顧客希望在這樣的線路上實現各種優化路徑的查詢。設該城市的公交線路的輸入格式為:
線路編號:起始站名(該站坐標);經過的站點1名(該站坐標);經過的站點2名(該站坐標);……;經過的站點n名(該站坐標);終點站名(該站坐標)。該線路的乘坐價錢。該線路平均經過多少時間來一輛。車速。
例如:63:A(32,45);B(76,45);C(76,90);……;N(100,100)。1元。5分鐘。1/
代碼片段和文件信息
#include
#include
#include
#include
using?namespace?std;
#define?NoEdge?99999//無路線
int?number;//公交線路數
int?count=0;//多次出現的地點數
int?*area;
double?*price;//車票價格數組
int?*speed;//車速數組
double?**DistanceMap;//以距離為權值的鄰接矩陣
double?**PriceMap;//以票價為權值的鄰接矩陣
double?**TimeMap;//以時間為權值的鄰接矩陣
int?*numIndex;//車站編號索引
string?*nameIndex;//車站姓名索引
int?*priceIndex;//票價索引
int?*lpriceIndex;//處理后的票價
int?range;//總車站數
double?Restrict(double?a)//double保留兩位小數方法
{
????double?tem=int(a*100);
????return?tem/100;
}
double?getDistance(double?adouble?bdouble?cdouble?d)//求兩點間距離方法
{
????return?Restrict(sqrt((a-c)*(a-c)+(b-d)*(b-d)));
}
class?RChainNode//鏈表結點類
{
????friend?class?BusRoutine;
????friend?class?RChain;
public:
????string?station;
????double?px;
????double?py;
????RChainNode?*next;
????RChainNode?*prior;
};
class?RChain//鏈表類
{
????friend?class?BusRoutine;
public:
????RChain()//構造函數?
????{
????????first?=?0;
????}
????~RChain();//析構函數?
????bool?IsEmpty()
????{
????????return?first?==?0;
????}
????int?Length()?const;
????RChainNode?*getFirst()//得到鏈表頭結點
????{
????????return?first;
????}
????RChain?&Insert(const?string?&s?const?double?&xconst?double?&y);
????RChain?&IInsert(RChainNode?*rn);
????void?Output(ostream?&out)?const;
????string?getStation()//返回站點名
????{
????????return?first->station;
????}
????RChain?&Rearrange(double?&y);
????RChain?&reCharge(RChain?&r);
????RChain?&dRejudge();
private:
????RChainNode?*first;
};
RChain::~RChain()//在類外聲明析構函數?
{
????RChainNode?*next;
????while?(first)
????{
????????next?=?first->next;
????????delete?first;
????????first?=?next;
????}
}
int?RChain::Length()?const//Length()是屬于RChain類的,可以引用,但不能修改本類中的數據成員?
{
????RChainNode?*current?=?first;
????int?len?=?0;
????while?(current)
????{
????????len++;
????????current?=?current->next;
????}
????return?len;
}
RChain?&RChain::Insert(const?string?&s?const?double?&x?const?double?&y)//從后插入新結點
{
????if?(!first)
????{
????????first?=?new?RChainNode;
????????first->station?=?s;
????????first->px?=?x;
????????first->py?=?y;
????????first->next?=?0;
????????first->prior?=?0;
????}
????else
????{
????????RChainNode?*p?=?first;
????????while?(p->next)
????????????p?=?p->next;
????????RChainNode?*r?=?new?RChainNode;
????????r->station=s;
????????r->px?=?x;
????????r->py?=?y;
????????p->next?=?r;
????????r->prior?=?p;
????????r->next=0;
????}//插入在表尾,形成類似與C1C2C3C4...Cn的鏈表?
????return?*this;
}
RChain?&RChain::IInsert(RChainNode?*rn)//從后插入一個已有結點
{
????if(first)
????{
????????RChainNode?*tem=rn;
????????RChainNode?*p?=?first;
????????while?(p->next)
????????????p?=?p->next;
????????p->next=tem;
????????tem->prior=p;
????}
????else
????????first=rn;
}
RChain?&RChain::Rearrange(double?&y)//將車站鏈表處理,得到距離及對應累加序號
{
????RChainNode?*Current=first;
????for(Current;?Current->next;?Current=Current->next)
??
評論
共有 條評論