日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

PHP的SPFA,由于是之前的c代码,风格你懂的........(夹带php队列实现)

發(fā)布時(shí)間:2024/1/17 php 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP的SPFA,由于是之前的c代码,风格你懂的........(夹带php队列实现) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??

<?php echo?"<meta?http-equiv='Content-Type'?content='text/html;charset=utf-8'>"; error_reporting(?E_ALL&~E_NOTICE?); define("Inf",0x7fffffff); define("MaxV",10000); define("MaxE",10000); class?Edge {var?$to,$nex,$w;function?__construct($to,$nex,$w){$this->to=$to;$this->nex=$nex;$this->w=$w;}? } class?A {public?$N,$M;public?$edge?=?array();public?$size;public?$head?=?array();public?$dist?=?array();public?$cot?=?array();public?$vst?=?array();public?$path?=?array();function?InsertEdge($from,$to,$w){$this->edge[$this->size]?=?new?Edge($to,$this->head[$from],$w);$this->head[$from]?=?$this->size++;?}function?spfa($s){$from;$to;$que?=?new?queue(30);for($i=0;$i<=$this->N;$i++){$this->path[$i]=$i;$this->dist[$i]=Inf;}$this->path[$s]=0;$this->dist[$s]=0;$que->InQ($s);$this->vst[$s]=1;while($que->QIsFull()!=0){$from=$que->getFrontDate();//$que.pop();//array_shift($que);$que->OutQ();$vst[$from]=0;$cot[$from]++;if($this->cot[$from]>=$this->N)return?0;for($i=$this->head[$from];$i+1;$i=$this->edge[i]->next)//當(dāng)head=-1的時(shí)候,停止,證明其沒有{$to=$this->edge[$i].to;if($this->dist[$to]>$this->dist[$from]+$this->edge[$i]->w){$this->dist[$to]?=?$this->dist[$from]?+?$this->edge[$i]->w;if(!$this->vst[$to])$this->$que->InQ($to);$this->vst[$to]=1;}}}for($i=1;$i<=$N;$i++)echo?"dist".$this->i."??=??".$this->dist[i]."<br>";return?1;}function?init(){$from;$to;$w;$this->size=0;//memset(head,-1,sizeof(head));for($i=0;$i<count($this->head);$i++)$this->head[i]?=?-1;}function?main(){$this->init();$this->M?=?3;$this->N?=?3;$from?=?1;$w?=?3?;$to?=?2;$this->InsertEdge($from,$to,$w);$from?=?2;$w?=?1?;$to?=?3;$this->InsertEdge($from,$to,$w);$from?=?3;$w?=?3?;$to?=?2;$this->InsertEdge($from,$to,$w);if(!$this->spfa(1))echo?"存在負(fù)環(huán)回路";else?echo?"存在最短路徑";return?0;} }class?queue{??protected?$front;//隊(duì)頭??protected?$rear;//隊(duì)尾??protected?$queue=array('0'=>'隊(duì)尾');//存儲(chǔ)隊(duì)列??protected?$maxsize;//最大數(shù)??public?function?__construct($size){??$this->initQ($size);??}??//初始化隊(duì)列??private?function?initQ($size){??$this->front=0;??$this->rear=0;??$this->maxsize=$size;??}??//判斷隊(duì)空??public?function?QIsEmpty(){??return?$this->front==$this->rear;??}??//判斷隊(duì)滿??public?function?QIsFull(){??return?($this->front-$this->rear)==$this->maxsize;??}??//獲取隊(duì)首數(shù)據(jù)??public?function?getFrontDate(){??return?$this->queue[$this->front]->getData();??}??//入隊(duì)??public?function?InQ($data){??if($this->QIsFull())echo?$data.":滿了!(隊(duì)滿不能入隊(duì),請(qǐng)等待!)<br>";??else?{??$this->front++;??for($i=$this->front;$i>$this->rear;$i--){??//echo?$data;??if($this->queue[$i])unset($this->queue[$i]);??$this->queue[$i]=$this->queue[$i-1];??}??$this->queue[$this->rear+1]=new?data($data);?}??}??//出隊(duì)??public?function?OutQ(){??if($this->QIsEmpty())echo?"隊(duì)空不能出隊(duì)!<br>";??else{??unset($this->queue[$this->front]);??$this->front--;}} } class?data?{??//數(shù)據(jù)??private?$data;??public?function?__construct($data){??$this->data=$data;??}??public?function?getData(){??return?$this->data;??}??public?function?__destruct(){?}?? }?? ?> <?php$a?=?new?A();$a->main(); ?>


轉(zhuǎn)載于:https://my.oschina.net/MrHou/blog/147911

總結(jié)

以上是生活随笔為你收集整理的PHP的SPFA,由于是之前的c代码,风格你懂的........(夹带php队列实现)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。