锘??xml version="1.0" encoding="utf-8" standalone="yes"?>黄色亚洲精品,牛人盗摄一区二区三区视频,久久久久久久高潮http://www.shnenglu.com/Icyflame/category/10621.html瀛︿範綆楁硶zh-cnSat, 05 Sep 2009 02:19:39 GMTSat, 05 Sep 2009 02:19:39 GMT60鍓茬偣涓庢ˉhttp://www.shnenglu.com/Icyflame/archive/2009/07/05/89227.htmlIcyflameIcyflameSun, 05 Jul 2009 08:18:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/07/05/89227.htmlhttp://www.shnenglu.com/Icyflame/comments/89227.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/07/05/89227.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/89227.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/89227.html涓銆佸畾涔?br>      鍓茬偣錛氬鏋滃湪鍥綠涓垹鍘諱竴涓粨鐐箄鍚庯紝鍥綠鐨勮繛閫氬垎鏋濇暟澧炲姞錛屽嵆W(G-u)>W(G)錛屽垯縐扮粨鐐箄涓篏鐨勫壊鐐癸紝鍙堢О鍏寵妭鐐廣?br>      妗ワ細濡傛灉鍦ㄥ浘G涓垹鍘諱竴鏉¤竟e鍚庯紝鍥綠鐨勮繛閫氬垎鏀暟澧炲姞錛屽嵆W(G-e)>W(G)錛屽垯縐拌竟u涓篏鐨勬ˉ錛屽張縐板壊杈規垨鍏寵妭杈廣?br>      鍙岃繛閫氬垎鏀細G涓笉鍚壊鐐圭殑鏋佸ぇ榪為氬瓙鍥劇О涓篏鐨勫弻榪為氬垎鏀紝鍙堢О涓篏鐨勫潡銆?br>浜屻丏FS
      鎻忚堪錛?/strong>鍦ㄥ浜庝換閫変竴涓浘涓粨鐐逛負鏍圭殑DFS鎼滅儲鏍戜腑寤虹珛涓涓狶AB鏁扮粍涓嶭OW鏁扮粍錛孡AB鏁扮粍瀛樺偍涓粨鐐圭殑緙栧彿錛孡OW鏁扮粍瀛樺偍鍚勭偣鍙婂叾瀛愭爲鐨勫悇緇撶偣鑳藉埌杈劇殑鏈灝忕紪鍙風粨鐐圭殑緙栧彿銆?br>
1 //lab涓轟竴涓叏灞鍙橀噺錛屽垵濮嬩負1錛?nbsp;LAB鍚勯」鍒濆涓?
2 DFS(u)
3     LAB[u] = LOW[u] = lab++
4     for each (u, v) in E(G)
5         if LAB[v] is 0
6             DFS(v)
7             LOW[u] = min{LOW[u], LOW[v]}
8         else if  v isnot parent of u
9             LOW[u] = min{LOW[u], LAB[v]}

      絎?琛屼腑錛屽鏋?u, v)鏄爲杈癸紝鍒欏v鍋氭繁搴︿紭鍏堟悳绱紝騫朵笖LOW[u] = min{LOW[u], LOW[v]}錛屽鏋?u, v)鏄弽鍚戣竟錛屽垯LOW[u] = min{LOW[u], LAB[v]}銆?/span>
涓夈佸壊鐐?br>      鎻忚堪錛?/strong>褰撲竴涓粨鐐箄鏄壊鐐規椂蹇呮弧瓚充互涓嬩袱涓潯浠朵箣涓錛?br>            1錛塽涓烘牴涓旇嚦灝戞湁涓ゆ5瀛愭爲錛?br>            2錛塽涓嶄負鏍逛笖瀛樺湪涓涓猽鍦ㄦ繁鎼滄爲涓殑瀛愬コv浣垮緱LOW[v] ≥ LAB[u]銆?br>      紺轟緥錛?/strong>POJ 1523 瑙i鎶ュ憡銆?br>鍥涖佹ˉ
       鎻忚堪錛?/strong>涓鏉¤竟e=(u, v)鏄ˉ錛屽綋涓斾粎褰揺涓烘爲鏋濊竟涓擫OW[v] > LAB[u]銆?br>      紺轟緥錛?/strong>POJ 3352 瑙i鎶ュ憡銆?/span>
      

Icyflame 2009-07-05 16:18 鍙戣〃璇勮
]]>
LCA闂錛堝惈RMQ鐨凷T綆楁硶錛?/title><link>http://www.shnenglu.com/Icyflame/archive/2009/07/04/88987.html</link><dc:creator>Icyflame</dc:creator><author>Icyflame</author><pubDate>Sat, 04 Jul 2009 07:25:00 GMT</pubDate><guid>http://www.shnenglu.com/Icyflame/archive/2009/07/04/88987.html</guid><wfw:comment>http://www.shnenglu.com/Icyflame/comments/88987.html</wfw:comment><comments>http://www.shnenglu.com/Icyflame/archive/2009/07/04/88987.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/Icyflame/comments/commentRss/88987.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/Icyflame/services/trackbacks/88987.html</trackback:ping><description><![CDATA[<p><strong>涓銆佸畾涔変笌瀹氱悊<br></strong><span style="FONT-SIZE: 10pt">      LCA錛歀east Common Ancestors錛堟渶榪戝叕鍏辯鍏堬級錛屽浜庝竴媯墊湁鏍規爲T鐨勪換鎰忎袱涓妭鐐箄錛寁錛屾眰鍑篖CA(T, u, v)錛屽嵆紱昏窡鏈榪滅殑鑺傜偣x錛屼嬌寰梮鍚屾椂鏄痷鍜寁鐨勭鍏堛?br>      鍦ㄧ嚎綆楁硶錛氱敤姣旇緝闀跨殑鏃墮棿鍋氶澶勭悊錛屼絾鏄瓑淇℃伅鍏呰凍浠ュ悗姣忔鍥炵瓟璇㈤棶鍙渶瑕佺敤姣旇緝灝戠殑鏃墮棿銆?br>      紱葷嚎綆楁硶錛氬厛鎶婃墍鏈夌殑璇㈤棶璇誨叆錛岀劧鍚庝竴璧鋒妸鎵鏈夎闂洖絳斿畬鎴愩?br>      RMQ錛氱粰鍑轟竴涓暟緇凙錛屽洖絳旇闂甊MQ(A, i, j)錛屽嵆A[i...j]涔嬮棿鐨勬渶鍊肩殑涓嬫爣銆?br></span><strong>浜屻丏FS+RMQ</strong><br><span style="FONT-SIZE: 10pt">      1錛塖parse Table錛圫T錛夌畻娉?br>      <strong>鎻忚堪錛?/strong><br></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #008000">//</span><span style="COLOR: #008000">鍒濆鍖?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">INIT_RMQ<br></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">max[i][j]涓瓨鐨勬槸閲峧寮濮嬬殑i涓暟鎹腑鐨勬渶澶у鹼紝鏈灝忓肩被浼鹼紝num涓瓨鏈夋暟緇勭殑鍊?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> i : </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> to n<br></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">        max[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][i] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> num[i]<br></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> i : </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> to log(n)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">log(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> j : </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> to n<br></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">            max[i][j] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> MAX(max[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][j], max[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">^</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)]<br></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">鏌ヨ</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">RMQ(i, j)<br></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">    k </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> log(j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> log(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> MAX(max[k][i], max[k][j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">^</span><span style="COLOR: #000000">k</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])</span></div> <p>      <strong>鍒嗘瀽錛?/strong>鍒濆鍖栬繃紼嬪疄闄呬笂鏄竴涓姩鎬佽鍒掔殑鎬濇兂銆傛槗鐭ワ紝鍒濆鍖栬繃紼嬫晥鐜囨槸O(<em>n</em>log<em>n</em>)錛岃屾煡璇㈣繃紼嬫晥鐜囨槸O(1)銆係T鏄竴涓湪綰跨畻娉曘?br>      <strong>紺轟緥錛?/strong><a title="POJ 3368 瑙i鎶ュ憡" href="http://www.shnenglu.com/Icyflame/archive/2009/07/01/88999.html">POJ 3368 瑙i鎶ュ憡</a><br>      2錛夋眰瑙CA闂<br>      <strong>鎻忚堪錛?/strong><br>      錛?錛塂FS錛氫粠鏍慣鐨勬牴寮濮嬶紝榪涜娣卞害浼樺厛閬嶅巻錛屽茍璁板綍涓嬫瘡嬈″埌杈劇殑欏剁偣銆傜涓涓殑緇撶偣鏄痳oot(T)錛屾瘡緇忚繃涓鏉¤竟閮借褰曞畠鐨勭鐐廣傜敱浜庢瘡鏉¤竟鎭板ソ緇忚繃2嬈★紝鍥犳涓鍏辮褰曚簡2n-1涓粨鐐癸紝鐢‥[1, ... , 2n-1]鏉ヨ〃紺恒?br>      錛?錛夎綆桼錛氱敤R[i]琛ㄧずE鏁扮粍涓涓涓間負i鐨勫厓绱犱笅鏍囷紝鍗沖鏋淩[u] < R[v]鏃訛紝DFS璁塊棶鐨勯『搴忔槸E[R[u], R[u]+1, ..., R[v]]銆傝櫧鐒跺叾涓寘鍚玼鐨勫悗浠o紝浣嗘繁搴︽渶灝忕殑榪樻槸u涓巚鐨勫叕鍏辯鍏堛?br>      錛?錛塕MQ錛氬綋R[u] ≥ R[v]鏃訛紝LCA[T, u, v] = RMQ(L, R[v], R[u])錛涘惁鍒橪CA[T, u, v] = RMQ(L, R[u], R[v])錛岃綆桼MQ銆?br>      鐢變簬RMQ涓嬌鐢ㄧ殑ST綆楁硶鏄湪綰跨畻娉曪紝鎵浠ヨ繖涓畻娉曚篃鏄湪綰跨畻娉曘?br>      <strong>紺轟緥錛?/strong><a title="ZOJ 3195 瑙i鎶ュ憡" href="http://www.shnenglu.com/Icyflame/archive/2009/07/02/89107.html">ZOJ 3195 瑙i鎶ュ憡</a>銆?br></span><strong>涓夈乀arjan綆楁硶<br></strong><span style="FONT-SIZE: 10pt">      <strong>鎻忚堪錛?/strong>Tarjan綆楁硶鏄竴涓綰跨畻娉曪紝涔熷氨鏄鍙湁鍏堣幏寰楁墍鏈夌殑鏌ヨ錛屽啀鎸変竴涓壒瀹氱殑欏哄簭榪涜榪愮畻銆?/p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #008000">//</span><span style="COLOR: #008000">parent涓哄茍鏌ラ泦錛孎IND涓哄茍鏌ラ泦鐨勬煡鎵炬搷浣?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">Tarjan(u)<br></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">    visit[u] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> each (u, v) </span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000"> QUERY<br></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> visit[v]<br></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">            ans(u, v) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> FIND(v)<br></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> each (u, v) </span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000"> TREE    <br></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">visit[v]<br></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">            Tarjan(v)<br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">            parent[v] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> u</span></div> </span><font size=2>      <strong>紺轟緥錛?/strong><a title="HDOJ 2586 瑙i鎶ュ憡" href="http://www.shnenglu.com/Icyflame/archive/2009/07/02/89118.html">HDOJ 2586 瑙i鎶ュ憡</a>銆?/font> <img src ="http://www.shnenglu.com/Icyflame/aggbug/88987.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/Icyflame/" target="_blank">Icyflame</a> 2009-07-04 15:25 <a href="http://www.shnenglu.com/Icyflame/archive/2009/07/04/88987.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鏈灝忚垂鐢ㄦ渶澶ф祦闂http://www.shnenglu.com/Icyflame/archive/2009/06/30/88891.htmlIcyflameIcyflameTue, 30 Jun 2009 14:29:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/06/30/88891.htmlhttp://www.shnenglu.com/Icyflame/comments/88891.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/06/30/88891.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/88891.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/88891.html涓銆佸畾涔変笌瀹氱悊
      鏈灝忚垂鐢ㄦ渶澶ф祦錛氳G鏄互s涓烘簮t涓烘眹鐨勭綉緇滐紝c鏄疓鐨勫閲忥紝b鏄疓鐨勫崟浣嶆祦閲忚垂鐢紝涓旀湁b[i][j] = -b[i][j]錛宖鏄疓鐨勬祦錛屽垯b(f)=鈭?fij*bij)錛?i, j)∈E(G) 涓攆ij>0銆傛渶灝忚垂鐢ㄦ渶澶ф祦闂錛屽氨鏄眰緗戠粶G鐨勬渶澶ф祦f涓斾嬌璐圭敤b(f)鏈灝忋傝繖鏍風殑嫻佺О涓烘渶灝忚垂鐢ㄦ渶澶ф祦銆?/span>
浜屻佺畻娉曟濇兂
      鐢‵ord-Fulkerson綆楁硶鐨勬濇兂錛屼笉鏂湴鍦ㄦ畫鐣欑綉緇滀腑瀵繪壘澧炲箍璺紝鍙笉榪囪繖涓騫胯礬鏄綋鍓嶇綉緇滀腑s鍒皌鐨勪互鍗曚綅嫻侀噺璐圭敤涓烘潈鐨勬渶鐭礬錛屽榪欐潯澧炲箍璺繘琛屾搷浣溿傜敱浜庤垂鐢ㄦ湁璐熷鹼紝寤鴻鐢⊿PFA綆楁硶銆?br>涓夈佺畻娉曚粙緇?br>      鎻忚堪錛?/span>

1 MCMF(G, s, t)
2     for each edge(u, v) in E(G)
3         do f[u, v] = 0
4            f[v, u] = 0
5     while exists a path p from s to t in Gf and p is the shortest path
6         do cf(p) = min{cf(u, v) : (u, v) in p}
7            for each edge(u, v) in p
8                do f[u, v] = f[u, v] + cf(p)
9                   f[v, u] = - f[u, v]
      瀹炵幇錛?br>
 1mcmf()
 2{
 3    while(true)
 4    {
 5        for(int i=1; i<=n+m+1; i++)
 6            d[i] = MAX;
 7        d[s] = 0;
 8        spfa(); //p涓瓨鏈夎鐐圭殑鍓嶇戶鐐?/span>
 9        if(p[t] == -1//琛ㄧず宸叉棤澧炲箍璺?/span>
10            break;
11        int minf = INT_MAX;
12        int it = t;
13        while(p[it] != -1)
14        {
15            minf = min(minf, c[p[it]][it] - f[p[it]][it]);
16            it = p[it];
17        }

18        it = t;
19        while(p[it] != -1)
20        {
21            f[p[it]][it] += minf;
22            f[it][p[it]] = -f[p[it]][it];
23            it = p[it];
24        }

25    }

26}

涓夈佺畻娉曠ず渚?br>      POJ 2516 瑙i鎶ュ憡


Icyflame 2009-06-30 22:29 鍙戣〃璇勮
]]>
鍖歸厤闂http://www.shnenglu.com/Icyflame/archive/2009/06/29/88604.htmlIcyflameIcyflameMon, 29 Jun 2009 06:48:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/06/29/88604.htmlhttp://www.shnenglu.com/Icyflame/comments/88604.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/06/29/88604.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/88604.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/88604.html涓銆佸畾涔変笌瀹氱悊
      鍖歸厤錛氳G(V, E)涓烘棤鐜浘錛岃M涓篍鐨勪竴涓潪絀哄瓙闆嗭紝濡傛灉M涓殑浠繪剰涓ゆ潯杈瑰湪G涓笉鐩擱偦錛屽垯縐癕鏄浘G涓殑涓涓尮閰嶃傝嫢瀵瑰浘G鐨勪換浣曞尮閰峂'錛屽潎鏈墊M'|≤|M|錛屽垯縐癕涓篏鐨勬渶澶у尮閰嶃?br>      楗卞拰鐐癸細璁綧鏄浘G涓殑鍖歸厤錛孏涓笌M涓殑杈瑰叧鑱旂殑欏剁偣縐頒負M楗卞拰鐐癸紝鍚﹀垯縐頒負M闈為ケ鍜岀偣銆傝嫢鍥句腑欏剁偣鍧囨槸M楗卞拰鐐癸紝鍒欑ОM涓篏鐨勫畬緹庡尮閰嶃?br>      M浜ら敊璺細璁綪鏄疓鐨勪竴鏉¤礬錛屼笖鍦≒涓紝M鐨勮竟鍜孍-M鐨勮竟浜ら敊鍑虹幇錛屽垯縐癙鏄疓鐨勪竴鏉浜ら敊璺傝嫢M浜ら敊璺疨鐨勪袱涓鐐逛負M闈為ケ鍜岀偣錛屽垯縐癙涓篗鍙騫胯礬銆?nbsp;
      鏍瑰湪x鐨凪浜ら敊瀛愬浘錛氳x涓篏涓璏闈為ケ鍜岀偣銆侴涓敱璧風偣涓簒鐨凪浜ら敊璺墍鑳借繛鎺ョ殑欏剁偣闆嗘墍瀵煎嚭鐨凣鐨勫鍑哄瓙鍥俱?br>      S鐨勯偦闆嗭細璁維涓篏鐨勪換涓欏剁偣闆嗭紝G涓笌S鐨勯《鐐歸偦鎺ョ殑鎵鏈夐《鐐圭殑闆嗗悎錛岀О涓篠鐨勯偦闆嗭紝璁頒綔N(S)銆?br>      鏈浼樺尮閰嶏細瀵逛簬涓涓姞鏉冧簩閮ㄥ浘錛屼竴涓潈鏈澶х殑鍖歸厤鍙綔鏈浼樺尮閰嶃?br>      鍙瀹氭爣錛氭槧灝刲錛歏(G)→R錛屾弧瓚沖G鐨勬瘡鏉¤竟e={u, v}錛屽潎鏈塴(u)+l(v)≥w(u, v)錛屽叾涓瓀(u, v)琛ㄧず杈筫鐨勬潈錛屽垯縐發涓篏鐨勫彲琛岄《鏍囥備護El={(u, v) | {u, v}∈E(G)錛宭(u)+l(v)=w(u, v)}錛孏l涓轟互El涓鴻竟闆嗙殑G鐨勭敓鎴愬瓙鍥撅紝鍒欑ОGl涓簂絳夊瓙鍥俱?br>
浜屻佹渶澶у尮閰嶏紙鍖堢墮鍒╃畻娉曪級
      鎻忚堪錛?/strong>
      錛?錛塆鏄叿鏈夊垝鍒?V1, V2)鐨勪簩鍒嗗浘錛屼換緇欏垵濮嬪尮閰峂錛?br>      錛?錛夎嫢M楗卞拰V1鍒欑粨鏉燂紱
      錛?錛夊惁鍒欙紝鍦╒1涓壘涓M闈為ケ鍜岀偣錛岋紝緗甋={x}錛?T涓虹┖錛?br>      錛?錛夎嫢N(S) = T錛屽垯鍋滄錛屽惁鍒欎換閫変竴鐐箉∈N(S)-T錛?br>      錛?錛夎嫢y涓篗闈為ケ鍜岀偣錛屽垯姹備竴鏉′粠x鍒皔鐨凪鍙騫胯礬P錛岀疆M涓篗寮傛垨P騫惰漿錛?錛夛紱
      錛?錛夊惁鍒欙紝鐢變簬y鏄疢鐨勯ケ鍜岀偣錛屾晠M涓湁涓杈箋y, u}錛岀疆S = S∪{u}錛孴 = T∪{y}錛岃漿錛?錛夈?br>      瀹炵幇錛?br>

 1 HUNGARY
 2     for i : 1 to |V2|
 3         do match[i] = 0    
 4     for each vertex u in V1
 5         do for i : 1 to |V2|
 6                do visit[i] = false
 7            DFS(u)
 8 DFS(u)
 9     for each vertex v in V2
10         if (u, v) in E(G) and visit[v] is false
11             then visit[v]=true
12                  if match[v] is 0 or DFS(match[v]) is true
13                      then match[v] = u
14                           return true
15     return false

      璇存槑錛?/strong>絎?琛岀殑DFS(u)榪囩▼錛屽綋瀛樺湪浠巙寮濮嬬殑M鍙騫胯礬錛屽垯榪斿洖true錛屽茍瀹屾垚M鐨勬墿灞曪紝姝ゆ椂|M|鍔犱竴銆傚鏋滆繑鍥瀎alse錛屽垯琛ㄧず涓嶅瓨鍦∕鍙騫胯礬銆?nbsp;
      紺轟緥錛?/strong>POJ 1274 瑙i鎶ュ憡銆?/span>
涓夈佹渶浼樺尮閰嶏紙KM綆楁硶錛?br>      鎻忚堪錛?/strong>
      錛?錛変粠浠繪剰鍙欏舵爣l寮濮嬶紝紜畾l絳夊瓙鍥綠l錛屽茍涓斿湪Gl涓夊彇鍖歸厤M銆傝嫢M楗卞拰V1錛屽垯M鏄畬緹庡尮閰嶏紝涔熷嵆M鏄渶浼樺尮閰嶏紝綆楁硶緇堟錛?br>      錛?錛夊惁鍒欙紝榪愮敤鍖堢墮鍒╃畻娉曪紝緇堟浜嶴灞炰簬V1錛孴灞炰簬V2涓斾嬌瀵逛簬Gl錛孨(S)=T銆備護al=min{l(x)+l(y)-w(x, y) | x∈S, y∈V2-T}錛屼護l'(u)=l(u)-al濡傛灉u∈S錛沴'(u)=l(u)+al濡傛灉u∈T錛沴'(u)=l(u)錛屽叾瀹冦傜敤l'浠f浛l錛岀敤Gl'浠f浛Gl杞叆錛?錛夈?br>      瀹炵幇錛?/strong>

 1 KUHN-MUNKRES(G)
 2     for each vertex u in V1
 3         do lx[u] = max{w[u][v] | (u, v) in E(G)}
 4     for each vertex v in V2
 5         do ly[v] = 0
 6     for each vertex u in V1
 7         do while(true)
 8                do for each vertex u in V1
 9                       do vx[u] = false
10                   for each vertex v in V2
11                       do vy[v] = false
12                          slack[v] = MAX
13                   if DFS(u) is true
14                       then break
15                   d = min{slack[v] | v in V2 and vy[v] is false}
16                   for each vertex u in V1
17                       do lx[u] = lx[u] - d
18                   for each vertex v in V2
19                       do ly[v] = ly[v] + d
20 DFS(u)
21     vx[u] = true
22     for each vertex v in V2
23         do if lx[u]+ly[v]==w[u][v] and vy[v] is false
24                then vy[v] = true
25                     if match[v] is NIL or DFS(match[v])
26                         then match[v] = u
27                              return true
28             else if lx[u]+ly[v]>w[u][v]
29                 then slack[v] = min{slack[v], lx[u]+ly[v]-w[u][v]}
30     return false
      紺轟緥錛?/span>POJ 2195 瑙i鎶ュ憡

Icyflame 2009-06-29 14:48 鍙戣〃璇勮
]]>
鏈澶ф祦闂http://www.shnenglu.com/Icyflame/archive/2009/06/26/88364.htmlIcyflameIcyflameFri, 26 Jun 2009 11:49:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/06/26/88364.htmlhttp://www.shnenglu.com/Icyflame/comments/88364.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/06/26/88364.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/88364.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/88364.html 涓銆?nbsp;   瀹氫箟涓庡畾鐞?/strong>

嫻佺綉緇滐細G=(V, E)鏄竴涓湁鍚戝浘錛屽叾涓瘡鏉¤竟(u, v)E鍧囨湁涓涓潪璐熷閲?span lang="EN-US">c(u, v) 0錛屽惁鍒?span lang="EN-US">c(u, v)涓?span lang="EN-US">0.嫻佺綉緇滀腑鏈変袱涓壒鍒殑欏剁偣錛氭簮鐐?span lang="EN-US">s鍜屾眹鐐?span lang="EN-US">t銆傚浜庢瘡涓《鐐?span lang="EN-US">v∈V錛岄兘瀛樺湪涓鏉¤礬寰?span lang="EN-US">s…v…t銆?span lang="EN-US">

嫻侊細G涓婄殑涓涓疄鍊煎嚱鏁?span lang="EN-US">(V×VR)錛屾弧瓚?span lang="EN-US">1)瀵逛簬浠繪剰u, vV錛?span lang="EN-US">f(u, v)≤c(u, v)錛?span lang="EN-US">2)瀵逛簬浠繪剰u, vV錛?span lang="EN-US">f(u, v)=-f(u, v)錛?span lang="EN-US">3)瀵逛簬浠繪剰uV-{s, t}錛屸垜f(u, i)=0銆傚畾涔夋祦|f|=鈭?span lang="EN-US">f(s, i)銆?span lang="EN-US">

孌嬬暀緗戠粶錛氱粰瀹氭祦緗戠粶鍜屼竴涓祦錛屽叾孌嬬暀緗戠粶鐢卞彲浠ュ綰蟲洿澶氱綉緇滄祦鐨勮竟緇勬垚銆傚畾涔夋畫鐣欏閲?span style="color: red;" lang="EN-US">cf(u, v)=c(u, v)-f(u, v)銆傛畫鐣欑綉緇?span lang="EN-US">Gf=(V,Ef),鍏朵腑Ef={(u, v)V×V:cf(u, v)>0}銆?span lang="EN-US">

澧炲箍璺緞錛氬鍏夎礬寰?span lang="EN-US">p涓烘畫鐣欑綉緇?span lang="EN-US">Gf涓粠s鍒?span lang="EN-US">t鐨勪竴鏉$畝鍗曡礬寰勩?span lang="EN-US">

嫻佺綉緇滅殑鍓詫細嫻佺綉緇滅殑鍓?span lang="EN-US">(S, T)灝?span lang="EN-US">V鍒掑垎涓?span lang="EN-US">S鍜?span lang="EN-US">T=V-S涓ら儴鍒嗭紝浣垮緱sS錛?span lang="EN-US">t∈T銆傜┛榪囧壊鐨勫噣嫻佸畾涔変負f(S, T)錛屼笖鏈?span lang="EN-US">f(S, T)=|f|銆傚壊鐨勫閲忎負c(u, v)銆備竴涓綉緇滅殑鏈灝忓壊灝辨槸緗戠粶涓墍鏈夊壊涓祦閲忔渶灝忕殑鍓層?span lang="EN-US">

鏈澶ф祦鏈灝忓壊瀹氱悊錛氫互涓嬩笁涓潯浠剁瓑浠鳳細1)f鏄?span lang="EN-US">G鐨勪竴涓渶澶ф祦錛?span lang="EN-US">2)孌嬬暀緗戠粶Gf涓嶅寘鍚騫胯礬寰勶紱3)瀵?span lang="EN-US">G鐨勬煇涓壊(S, T)錛屾湁|f|=c(S, T)銆?span lang="EN-US">

鍓嶇疆嫻侊細鏄竴涓嚱鏁?span lang="EN-US">f錛?span lang="EN-US">V×VR錛屽畠婊¤凍1)瀵逛簬浠繪剰u, vV錛?span lang="EN-US">f(u, v)≤c(u, v)錛?span lang="EN-US">2)瀵逛簬浠繪剰u, vV錛?span lang="EN-US">f(u, v)=-f(u, v)錛?span lang="EN-US">3)瀵逛簬浠繪剰uV-{s, t}錛?span lang="EN-US">f[V, u]≥0銆傚畾涔変綑嫻?span lang="EN-US">e[u]=f[V, u]錛屽浜?span lang="EN-US">u∈V-{s, t}錛屽綋e[u]>0鏃訛紝縐伴《鐐?span lang="EN-US">u婧㈠嚭銆?span lang="EN-US">

楂樺害鍑芥暟錛氬嚱鏁?span lang="EN-US">h錛?span lang="EN-US">V→N婊¤凍h[s]=|V|錛?span lang="EN-US">h[t]=0錛屼笖瀵規瘡鏉℃畫鐣欒竟(u, v)Ef錛屾湁h[u]h[v]+1銆?span lang="EN-US">

瀹硅杈癸細濡傛灉cf(u, v)>0涓?span lang="EN-US">h[u]=h[v]+1錛屽垯縐?span lang="EN-US">(u, v)鏄璁歌竟銆傚惁鍒欙紝(u, v)鏄潪瀹硅杈廣傚璁哥綉緇滀負Gf,h=(V, Ef,h)錛屽叾涓?span lang="EN-US">Ef,h涓哄璁歌竟鐨勯泦鍚堛傛牴鎹璁歌竟鏈夊叧楂樺害鐨勫畾涔夛紝鏄撶煡錛屽璁哥綉緇滀笉瀛樺湪鍥炶礬銆?br>  
浜屻?nbsp;    Ford-Fulkerson鏂規硶

1錛?nbsp;鍩烘湰鐨凢ord-Fulkerson綆楁硶
      鎻忚堪錛?/strong>

1 FORD-FULKERSON(G, s, t)
2     for each edge(u, v) in E(G)
3         do f[u, v] = 0
4            f[v, u] = 0
5     while exists a path p from s to t in Gf
6         do cf(p) = min{cf(u, v) : (u, v) in p}
7            for each edge(u, v) in p
8                do f[u, v] = f[u, v] + cf(p)
9                   f[v, u] = - f[u, v]

     鍒嗘瀽錛?/strong>Ford-Fulkerson榪囩▼鐨勬晥鐜囧彇鍐充簬濡備綍紜畾澧炲箍璺緞銆傚鏋滈夋嫨涓嶅ソ錛屽浜庨潪鏈夌悊鐨勫閲忥紝綆楁硶鏈夊彲鑳戒笉鑳界粓姝€傚鏋滃閲忔槸鏁存暟錛堝鏋滃閲忎笉鏄暣鏁幫紝鍙互涔樹互鐗瑰畾鐨勫洜瀛愯漿鍖栦負鏁存暟錛夈傝繖鏃訛紝絎?~4琛岃繍琛屾椂闂翠負O(E),絎?~9琛岋紝while寰幆鑷沖鎵ц|f*|錛岃繖鏄洜涓哄湪姣忔榪唬鍚庯紝嫻佸艱嚦灝戝鍔?銆傛晠綆楁硶鏁堢巼涓篛(E|f*|)銆傚綋鏈澶ф祦f*杈冨皬鏃訛紝榪欎釜綆楁硶鐨勬晥鐜囪繕鏄笉閿欑殑銆?/span>

2錛塃dmonds-Karp綆楁硶

     鎻忚堪錛?/strong>灝嗗熀鏈殑Ford-Fulkerson綆楁硶鐨勭5琛屼腑鐢ㄥ箍搴︿紭鍏堟悳绱㈡潵瀹炵幇澧炲箍璺痯鐨勮綆楋紝鍗沖騫胯礬寰勬槸孌嬬暀緗戠粶涓粠s鍒皌鐨勬渶鐭礬寰?鍏朵腑姣忔潯杈逛負鍗曚綅璺濈鎴栨潈)錛屽垯鑳藉鏀硅繘Ford-Fulkerson綆楁硶鐨勭晫銆?br>     鍒嗘瀽錛?/strong>闅忕潃綆楁硶鐨勮繍琛岋紝瀵逛簬鎵鏈夐《鐐箆∈V-{s, t}錛屾畫鐣欑綉緇淕f涓殑鏈鐭礬寰勯暱搴?#948;f(s, v)闅忕潃姣忎釜嫻佺殑澧炲姞鑰屽崟璋冮掑銆傜洿瑙傜湅鏉ワ紝姣忔澧炲姞嫻佺殑鎿嶄綔閮藉皢浣垮緱褰撳墠鏈鐭礬涓殑涓鏉¤竟(鍗沖叧閿竟錛宑f(a, b)=cf(p))浠巔涓秷澶憋紝浠庤屼嬌寰楁柊鐢熸垚鐨凣f涓殑鏈鐭礬寰勭殑闀垮害澧炲姞銆傚悓鏃訛紝浠繪剰杈?u, v)鑷沖鑳芥垚涓簗V|/2-1嬈℃垚涓哄叧閿竟銆傝繖鏄洜涓虹i嬈℃垚涓哄叧閿竟鏃舵湁δf(s, v)=δf(s, u)+1錛岃岀i+1嬈℃椂f[u, v]鍙彲鑳戒笌涓婃寮傚彿錛屽垯鏈?#948;f'(s, u)=δf'(s, v)+1錛屼笖鏈?#948;f'(s, v)≥δf(s, v)錛屽垯δf'(s, u)=δf'(s, v)+1)≥δf(s, v)+1=δf(s, u)+2銆傛晠(u, v)姣忔鎴愪負鍏抽敭杈歸兘灝嗕嬌寰?#948;f(s, u)澧炲姞2錛屾湁鐢變簬δf(s, u)鏈澶у間負|V|-2錛屽垯浠繪剰杈?u, v)鑷沖鑳芥垚涓簗V|/2-1嬈℃垚涓哄叧閿竟銆傛晠璇ョ畻娉曠殑鏃墮棿澶嶆潅鎬т負O(VE2)銆?br>      紺轟緥錛?/strong>POJ 1273 瑙i鎶ュ憡

涓夈?nbsp;    Push-Relabel綆楁硶
      鎻忚堪錛?/strong>

1 //Push鎿嶄綔
2 PUSH(u, v)
3     if cf(u, v)<=0 or h[u] != h[v]+1
4         then return
5     df(u, v) = min{e[u], cf(u, v)}
6     f[u, v] = f[u, v] + df(u, v)
7     f[v, u] = -f[u, v]
8     e[u] = e[u] - df(u, v)
9     e[v] = e[v] + df(u, v)

1 //Relabel鎿嶄綔
2 RELABEL(u)
3     if e[u]==0 or there is no v that (u, v) in Ef and h[u]>h[v]
4         then return
5     h[u] = 1 + min{h[v] : (u, v) in Ef}

 1 //鍒濆鍖栧墠緗祦
 2 INTIALIZE-PREFLOW(G, s)
 3     for each vertex u in V[G]
 4         do h[u] = 0
 5            e[u] = 0
 6     for each edge(u, v) in E[G]
 7         do f[u, v] = 0
 8            f[v, u] = 0
 9     h[s] = |V[G]|
10     for each vertex u in Adj[s]
11         do f[s, u] = c[s, u]
12            f[u, s] = -c[s, u]
13            e[u] = c(s, u)
14            e[s] = e[s] - c(s, u)

1 //Push-Relabel綆楁硶
2 PUSH-RELABEL(G, s)
3     INTIALIZE-PREFLOW(G, s)
4     while there exists an applicable push or relabel operation
5         do select an applicable push or relabel operation and perform it

      姝g‘鎬э細鐢ㄥ驚鐜笉鍙樺紡鏉ヨ鏄?br>      1錛夊垵濮嬪寲錛欼NITILIZATION-FREFLOW鍒濆鍖杅涓哄墠緗祦
      2錛変繚鎸侊細綆楁硶涓彧浣跨敤浜唒ush涓巖elabel鎿嶄綔錛宺elabel鎿嶄綔鍙獎鍝嶉珮搴︼紝涓嶅獎鍝峟錛涜宲ush(u, v)鎿嶄綔錛屽彧浼氬鍔犵偣v鐨勬祦鍏ラ噺鍗砯(V, v)錛?br>鎵浠ュ鏋滄搷浣滀互鍓嶆槸鍓嶇疆嫻侊紝鎿嶄綔鍚庤繕鏄墠緗祦
      3錛夌粓姝細鍦ㄧ粓姝㈡椂錛孷-{s, t}涓殑姣忎釜欏剁偣鐨勪綑嫻佸繀涓?錛堝鏋滃瓨鍦ㄤ笉涓?鐨勭偣錛屽垯蹇呭彲浠ヨ繘琛宲ush鎴杛elabel鎿嶄綔錛夛紝涓旀渶緇堝緱鍒扮殑鏄竴涓墠緗祦錛屾晠鏈緇堝緱鍒扮殑鏄竴涓祦銆傚張鍦ㄦ渶緇堢殑孌嬬暀緗戠粶涓紝涓嶅瓨鍦╯鍒皌鐨勮礬寰勶紙鍚﹀垯錛屽瀛樺湪涓鏉¤礬寰剆,v1,...,t錛屽垯s鍙互鍚戝帇鍏ユ祦錛屼嬌寰梫1婧㈠嚭錛夛紝鏍規嵁鏈澶ф祦鏈灝忓壊瀹氱悊錛宖鏄渶澶ф祦銆?br>      鍒嗘瀽錛?/strong>棣栧厛錛岃瘉鏄庡浜庝換鎰忔孩鍑虹偣u錛屽潎瀛樺湪涓鏉″湪Gf涓婄殑u鍒皊鐨勮礬錛岃U={v|鍦℅f瀛樺湪涓鏉鍒皏鐨勮礬寰剗錛岃U#=V-U錛屽亣璁緎涓嶅睘浜嶶錛屽浜庨《鐐瑰(v, w)錛寁灞炰簬U錛寃灞炰簬U#錛屾湁f(v,  w)≤0錛屽惁鍒欙紝濡傛灉f(v, w)>0錛屽垯鏈塩f(v, w)=c(v, w)-f(v, w)=c(v, w)+f(w, v)>0錛岃繖涓巜涓嶅睘浜嶶鐭涚浘錛乪[u] =f(V, U)=F(U, U)+f(U#, U)=F(U#, U)≤0錛岃屽浜巚∈V-{s}錛宔[v]≥0錛屽張e[u]>0錛屽垯U涓繀鏈塻錛岀煕鐩撅紒
      relabel鎿嶄綔錛氬浜庢簮鐐逛笌浼氱偣錛屼笉瀛樺湪relabel鎿嶄綔錛岃屽浜庡叾瀹冮《鐐箄錛屽垵濮嬫椂錛宧[u]=0≤2|V|-1錛屽綋u榪涜relabel鏃訛紝u婧㈠嚭錛屽垯Gf涓繀瀛樺湪涓鏉鍒皊鐨勮礬寰刾=<u=v0,v1,...,vk=s>錛岀敱楂樺害鍑芥暟鐨勫畾涔?u, v)∈Ef錛屾湁h[u]≤h[v]+1錛屽垯h[u]=h[v0]≤h[vk]+k≤h[s]+|V|-1=2|V|-1錛岀畻娉曚腑鎬誨叡鐨剅elabel鎿嶄綔嬈℃暟涓篛(V2)銆?br>      楗卞拰push鎿嶄綔錛氳繘琛屾搷浣滃悗錛?u, v)杈逛粠Ef涓秷澶憋紝鍒欑О璇ush鎿嶄綔涓洪ケ鍜宲ush鎿嶄綔錛岃繘琛屼竴嬈?u, v)鐨勯ケ鍜宲ush鎿嶄綔蹇呮湁h[u]=h[v]+1錛屽悓鏃訛紝瑕佽繘琛屼笅涓嬈?u, v)鐨勯ケ鍜宲ush鎿嶄綔錛屽繀鍏堢粡榪囦竴嬈?v, u)鐨勯ケ鍜宲ush鎿嶄綔錛屽垯涓ゆ楗卞拰鎿嶄綔瀹[u]澧炲姞2錛屽張h[u]≤2|V|-1錛屽垯姣忎釜欏剁偣鑷沖榪涜|V|嬈¢ケ鍜宲ush鎿嶄綔錛屽垯鎬誨叡鐨勯ケ鍜宲ush鎿嶄綔嬈℃暟涓篛(|V||E|)銆?br>      涓嶉ケ鍜宲ush鎿嶄綔錛歱ush鎿嶄綔涓櫎鍘婚ケ鍜宲ush鎿嶄綔錛屽墿涓嬬殑灝辨槸涓嶉ケ鍜宲ush鎿嶄綔銆傚畾涔変竴涓嚱鏁癵錛屽間負鎵鏈塭鍊煎ぇ浜?鐨勯《鐐圭殑楂樺害鍜岋紝鏁単≥0銆傝冨療涓夌鎿嶄綔瀵筭鍊肩殑褰卞搷錛?錛塺elabel鎿嶄綔涓嶄細鏀瑰彉婧㈠嚭鎬т笖鍙細鏀瑰彉涓涓偣錛岃屼竴涓偣鐨勫彉鍖栬嚦澶氫負2|V|-1錛屽垯g鑷沖澧炲姞2|V|-1錛?錛夐ケ鍜宲ush鎿嶄綔鍙兘鑳藉鍔犱竴涓孩鍑虹偣錛実鑷沖澧炲姞2|V|-1錛?錛変笉楗卞拰push(u, v)鎿嶄綔浼氫嬌u鍙樹負涓嶆孩鍑猴紝鑰屽湪v浠庝笉婧㈠嚭鍒版孩鍑烘椂錛屽噺灝忕殑鏈灝戯紝涓?錛堝洜涓篽[u]=h[v]+1)銆傚垯涓嶉ケ鍜宲ush鎿嶄綔鐨勬鏁拌嚦澶氫負O(|V|2|E|+|V|3)銆?br>      緇間笂錛孭ush-Relabel綆楁硶鏄紜殑涓旀晥鐜囦負O(V2E)銆?br>      紺轟緥錛?/strong>POJ 1459 瑙i鎶ュ憡

鍥涖?nbsp;    Relabel-To-Front綆楁硶
      鎻忚堪錛圥ush銆丷elabel涓嶪nitalize-Flow鎿嶄綔鍙傜湅Push-Relab錛夛細
 1 //Discharge鎿嶄綔
 2 DISCHARGE(u)
 3     while e[u]>0
 4         do v = current[u]
 5            if  v == NIL
 6               then RELABEL(u)
 7                    current[u] = head[N[u]]
 8            else if cf(u, v)>0 and h[u]=h[v]+1
 9               then PUSH(u, v)
10            else
11               current[u] = next-neighbor[v]

 1 //Relabel-To-Front綆楁硶
 2 RELABEL-TO-FRONT(G, s, t)
 3     INITIALIZE-PREFLOW(G, s)
 4     L = V[G]-{s, t}, in any order
 5     for each vertex u in V[G]-{s, t}
 6         do current[u] = head[N[u]]
 7     u = head[L]
 8     while u != NIL
 9         do old-height = h[u]
10            DISCHARGE(u)
11            if h[u] > old-height
12                then move u to the front of list L
13            u = next[u]

      姝g‘鎬э細Relabel-To-Front綆楁硶鍙湁鍦≒ush涓巖elabel鎿嶄綔鏃舵墠浼氭敼鍙榝錛屾晠瀹冩槸Push-Ralabel鐨勪竴涓疄鐜幫紝鎵浠ュ彧瑕佽瘉鏄庡綋綆楁硶緇堟鏃跺彧瑕佸啀鏃燩ush涓嶳elabel鎿嶄綔鍗沖彲銆傜敤寰幆涓嶅彉寮忚瘉鏄庯細
      1錛夊垵濮嬪寲錛氳繍琛孖NITIALIZE-PREFLOW鍚庯紝鏃犲璁歌竟錛屾晠浠繪剰欏剁偣搴忓垪灝辨槸鎷撴墤鎺掑簭鐨勯《鐐廣?br>      2錛変繚鎸侊細瀹硅緗戠粶鍙氳繃Push鍜孯elabel鎿嶄綔鏀瑰彉錛屽浜嶱ush鎿嶄綔錛屼笉浼氫駭鐢熷璁歌竟錛屾晠u鐨勫墠闈㈢殑欏剁偣涓巙涓嶄細鏈変綑嫻侊紱鑰屽浜嶳elabel(u)鎿嶄綔錛屼笉浼氫駭鐢熻繘鍏鐨勫璁歌竟錛屽彧浼氫駭鐢熺寮u鐨勫璁歌竟錛屾敞鎰廟elabel-To-Front綆楁硶鐨?2琛岋紝灝唘縐誨叆L鐨勫墠绔紝淇濊瘉浠繪剰紱誨紑u鐨勫璁歌竟閮芥弧瓚蟲嫇鎵戞帓搴忥紝鍚屾椂錛屼繚璇乽鐨勫墠闈笌u娌℃湁浣欐祦銆?br>      3錛夌粓姝細寰幆緇堟鏃訛紝u鎭板ソ鍦↙鏈鍚庯紝浜庢槸錛孡涓墍鏈夌殑欏剁偣鍧囨棤浣欐祦錛屼篃灝卞啀鏃燩ush涓嶳elabel鎿嶄綔銆?br>      鍒嗘瀽錛?/strong>璇ョ畻娉曟槸Push-Relabel綆楁硶鐨勪竴縐嶅疄鐜幫紝鎵浠ユ瘡涓《鐐筊elabel鎿嶄綔鐨勭晫涓篛(V)錛屽垯鍏ㄩ儴欏剁偣鐨凴elabel鎿嶄綔鐨勭晫涓篛(V2)銆傚綆楁硶鐨?1~13琛岃〃鏄庝袱嬈elabel鎿嶄綔涔嬮棿錛屾渶澶氳繘琛寍L|嬈★紝鍗硘V|嬈ischarge鎿嶄綔銆傛瘡嬈ischarge鎿嶄綔鑷沖鍙細鏈?涓嶉ケ鍜孭ush鎿嶄綔錛屽垯涓嶉ケ鍜屾搷浣滅殑鐣屼負O(V3)錛屽悓鏃墮氳繃Push-Ralabel綆楁硶瀵歸ケ鍜孭ush鎿嶄綔鐨勫垎鏋愶紝鏁翠釜綆楁硶鐨勭晫涓篛(V3+VE)=O(V3)銆?br>      紺轟緥錛?/strong>
POJ 1149 瑙i鎶ュ憡
      

Icyflame 2009-06-26 19:49 鍙戣〃璇勮
]]>
鏈鐭礬闂http://www.shnenglu.com/Icyflame/archive/2009/05/22/83614.htmlIcyflameIcyflameFri, 22 May 2009 14:49:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/05/22/83614.htmlhttp://www.shnenglu.com/Icyflame/comments/83614.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/05/22/83614.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/83614.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/83614.html  鍦ㄥ姞鏉冨浘涓紝鎴戜滑緇忓父闇瑕佹壘鍑轟袱涓寚瀹氱偣涔嬮棿鐨勬渶鐭礬錛岃繖綾婚棶棰樻湁濡備笅涓ょ褰㈠紡錛?/span>
   1銆佸崟涓偣鍒板浘涓悇涓偣鐨勮窛紱?br>  
2銆佸浘涓換鎰忎袱涓偣涔嬮棿鐨勮窛紱?/span>

涓銆?鍗曚釜鐐瑰埌鍥句腑鍚勪釜鐐圭殑璺濈



榪欑被棰樼洰涓昏鏈変袱涓畻娉曪細
Bellman-Ford綆楁硶錛屾椂闂村鏉傛т負O(n3)錛屽叧浜庡叾綆楁硶鐨勬弿榪板強鍏朵紭鍖栵細
Dijkstra綆楁硶錛屾椂闂村鏉傛т負O(n2)錛屾弿榪板涓嬶細
 1 Dijkstra(G, u)
 2     for each vertex v in V(G)
 3         L[v] = ∞
 4     L[u] = 0
 5     S = {u}
 6     while S != V(G)
 7         v = vertex in V(G)-S with the minimum L-value
 8         S = S + {v}
 9         for each vertex a in V(G)-S
10             if L[v] + w[v, a] < L[v]
11                 L[v] = L[v] + w[v, a]
瀹氱悊錛欴ijkstra綆楁硶鑳芥眰鍑簎鍒癎涓叾瀹冨悇涓偣鐨勮窛紱繪渶鐭?/span>
璇佹槑錛氫護k琛ㄧず6琛岃凱浠g殑嬈℃暟
(1) 褰搆=0鏃訛紝鍗沖垵濮嬪寲鍚庯紝L[u]涓?錛孲涓簕u}錛屾樉鐒舵弧瓚沖涓嬩袱涓潯浠訛細
  • 瀵逛簬鍦⊿涓殑浠繪剰欏剁偣v閮芥湁L[v]涓簎鍒皏鐨勬渶鐭礬鐨勯暱搴?/span>
  • 瀵逛簬涓嶅啀S涓殑浠繪剰鐐箆閮芥湁L[v]涓簎鍙粡榪嘢涓殑鐐瑰埌v鐨勬渶鐭礬鐨勯暱搴?/span>
(2) 鍋囪k-1嬈¤凱浠e悗錛屾弧瓚充笂榪版潯浠訛紝瀵逛簬絎琸嬈¤凱浠f椂錛岄夊彇vk浣滀負鍔犲叆S鐐廣?/span>
    鍋囪L[vk]涓嶆槸浠?span lang="EN-US">u鍒?span lang="EN-US">vk鐨勬渶鐭礬鐨勯暱搴︼紝鐢變簬vk涓嶅湪k-1嬈¤凱浠e悗鐨?span lang="EN-US">S
涓紝鍒欐牴鎹笂榪版潯浠?span lang="EN-US">2
鍙煡鍦?span lang="EN-US">u鍒?span lang="EN-US">vk鐨勬渶鐭礬P錛?span lang="EN-US">u=v1,v2,…,vk錛屼腑蹇呭瓨鍦ㄤ竴涓粡榪囦竴涓笉鍦?span lang="EN-US">S涓殑鐐?span lang="EN-US">vi(涓嶄負vk)錛屼嬌寰?span lang="EN-US">v1,…,vi-1鍦?span lang="EN-US">S涓紝鍒?span lang="EN-US">L[vi]涓?span lang="EN-US">u鍒?span lang="EN-US">vi鐨勬渶鐭礬寰楅暱搴︼紝鍒欐湁L[vi]<u鍒?span lang="EN-US">vk鐨勬渶鐭礬鐨勯暱搴?span lang="EN-US"><L[vk]錛岃繖涓庣畻娉曠7琛屼腑vk鐨勯夊彇鏉′歡鐭涚浘銆傝瘉姣曘?span lang="EN-US">

浜屻?鍥句腑浠繪剰涓や釜鐐逛箣闂寸殑璺濈



榪欑被闂鏈変釜鍗佸垎鐩磋鐨勬柟娉曪紝灝辨槸瀵規瘡涓偣榪愯Dijkstra綆楁硶錛屾椂闂村鏉傛т負O(n3)錛岃屼笖涔熸槸涓涓ц兘杈冨ソ鐨勬柟娉曘?/span>
涓嬮潰鏄竴涓憲鍚嶇殑綆楁硶鈥擣loyd-Warshall綆楁硶錛屾椂闂村鏉傛т篃鏄疧(n3)錛?br>
 1 Warshall(G)
 2     for i 1 to n
 3         for j 1 to n
 4             if vi in adj[vj]
 5                 d[i, j] = w[i, j]
 6             else
 7                 d[i, j] = ∞
 8     for k 1 to n
 9         for i 1 to n
10             for j 1 to n
11                 d[i, j] = min(d[i, j], d[i, k]+d[k, j])
榪欎釜綆楁硶鍏跺疄鏄竴涓姩鎬佽鍒掔殑褰㈠紡錛屼護d[i, j, k]琛ㄧずvi涓巚j緇忚繃鍓峩涓偣鐨勬渶鐭窛紱伙紝鍒欏彲寰楅掑綊寮忥細
d[i, j, 0] = w[i, j] 褰搗i涓巚j鐩擱偦
d[i, j, 0] = ∞ 褰搗i涓巚j涓嶇浉閭?/span>
d[i, j, k+1] = min(d[i, j, k], d[i, k, k]+ d[k, j, k])




Icyflame 2009-05-22 22:49 鍙戣〃璇勮
]]>
Bellman-Ford 綆楁硶鍙婂叾浼樺寲(杞?http://www.shnenglu.com/Icyflame/archive/2009/05/02/81662.htmlIcyflameIcyflameFri, 01 May 2009 17:32:00 GMThttp://www.shnenglu.com/Icyflame/archive/2009/05/02/81662.htmlhttp://www.shnenglu.com/Icyflame/comments/81662.htmlhttp://www.shnenglu.com/Icyflame/archive/2009/05/02/81662.html#Feedback0http://www.shnenglu.com/Icyflame/comments/commentRss/81662.htmlhttp://www.shnenglu.com/Icyflame/services/trackbacks/81662.htmlhttp://hi.baidu.com/jzlikewei/blog/item/94db7950f96f995a1038c2cd.html
        http://hi.baidu.com/jzlikewei/blog/item/5343d134b54c6f48251f14cd.html
鍦ㄦ錛屾湰浜烘劅璋㈠師浣滆呯殑鍒嗕韓

Bellman-Ford綆楁硶涓庡彟涓涓潪甯歌憲鍚嶇殑Dijkstra綆楁硶涓鏍鳳紝鐢ㄤ簬姹傝В鍗曟簮鐐規渶鐭礬寰勯棶棰樸?/span>Bellman-ford綆楁硶闄や簡鍙眰瑙h竟鏉冨潎闈炶礋鐨勯棶棰樺錛岃繕鍙互瑙e喅瀛樺湪璐熸潈杈圭殑闂錛堟剰涔夋槸浠涔堬紝濂藉ソ鎬濊冿級錛岃?/span>Dijkstra綆楁硶鍙兘澶勭悊杈規潈闈炶礋鐨勯棶棰橈紝鍥犳 Bellman-Ford綆楁硶鐨勯傜敤闈㈣騫挎硾涓浜涖備絾鏄紝鍘熷鐨?/span>Bellman-Ford綆楁硶鏃墮棿澶嶆潅搴︿負 O錛?/span>VE錛?/span>,姣?/span>Dijkstra綆楁硶鐨勬椂闂村鏉傚害楂橈紝鎵浠ュ父甯歌浼楀鐨勫ぇ瀛︾畻娉曟暀縐戜功鎵蹇界暐錛屽氨榪炵粡鍏哥殑銆婄畻娉曞璁恒嬩篃鍙粙緇嶄簡鍩烘湰鐨?/span>Bellman-Ford綆楁硶錛屽湪鍥藉唴甯歌鐨勫熀鏈俊鎭濂ヨ禌鏁欐潗涓篃鍧囨湭鎻愬強錛屽洜姝よ綆楁硶鐨勭煡鍚嶅害涓庤鎺屾彙搴﹂兘涓嶅Dijkstra綆楁硶銆備簨瀹炰笂錛屾湁澶氱褰㈠紡鐨?/span>Bellman-Ford綆楁硶鐨勪紭鍖栧疄鐜般傝繖浜涗紭鍖栧疄鐜板湪鏃墮棿鏁堢巼涓婂緱鍒扮浉褰撴彁鍗囷紝渚嬪榪戜竴涓ゅ勾琚儹鎹х殑SPFA錛?/span>Shortest-Path Faster Algoithm 鏇村揩鐨勬渶鐭礬寰勭畻娉曪級綆楁硶鐨勬椂闂存晥鐜囩敋鑷崇敱浜?/span>Dijkstra綆楁硶錛屽洜姝ゆ垚涓轟俊鎭濂ヨ禌閫夋墜緇忓父璁ㄨ鐨勮瘽棰樸傜劧鑰岋紝闄愪簬璧勬枡鍖箯錛屾湁鍏?/span>Bellman-Ford綆楁硶鐨勮澶氶棶棰樺父甯稿洶鎵板ゥ璧涢夋墜銆傚錛氳綆楁硶鍊煎緱鎺屾彙涔堬紵鎬庢牱鐢ㄧ紪紼嬭璦鍏蜂綋瀹炵幇錛熸湁鍝簺浼樺寲錛熶笌SPFA綆楁硶鏈夊叧緋諱箞錛熸湰鏂囪瘯鍥懼Bellman-Ford綆楁硶鍋氫竴涓瘮杈冨叏闈㈢殑浠嬬粛銆傜粰鍑哄嚑縐嶅疄鐜扮▼搴忥紝浠庣悊璁哄拰瀹炴祴涓ゆ柟闈㈠垎鏋愪粬浠殑鏃墮棿澶嶆潅搴︼紝渚涘ぇ瀹跺湪澶囨垬鐪侀夊拰鍚庣畫鐨?/span>noi鏃跺弬鑰冦?/span>

Bellman-Ford綆楁硶鎬濇兂

Bellman-Ford綆楁硶鑳藉湪鏇存櫘閬嶇殑鎯呭喌涓嬶紙瀛樺湪璐熸潈杈癸級瑙e喅鍗曟簮鐐規渶鐭礬寰勯棶棰樸傚浜庣粰瀹氱殑甯︽潈錛堟湁鍚戞垨鏃犲悜錛夊浘 G=錛?/span>V,E錛夛紝鍏舵簮鐐逛負s錛屽姞鏉冨嚱鏁?/span> w鏄?/span> 杈歸泦 E 鐨勬槧灝勩傚鍥?/span>G榪愯Bellman-Ford綆楁硶鐨勭粨鏋滄槸涓涓竷灝斿鹼紝琛ㄦ槑鍥句腑鏄惁瀛樺湪鐫涓涓粠婧愮偣s鍙揪鐨勮礋鏉冨洖璺?/span>鑻ヤ笉瀛樺湪榪欐牱鐨勫洖璺紝綆楁硶灝嗙粰鍑轟粠婧愮偣s鍒?/span> 鍥?/span>G鐨勪換鎰忛《鐐?/span>v鐨勬渶鐭礬寰?/span>d[v]銆?/span>

Bellman-Ford綆楁硶嫻佺▼鍒嗕負涓変釜闃舵錛?/span>

錛?錛?span style="FONT: 7pt Times New Roman; font-size-adjust: none; font-stretch: normal">    鍒濆鍖栵細灝嗛櫎婧愮偣澶栫殑鎵鏈夐《鐐圭殑鏈鐭窛紱諱及璁″?/span> d[v] ←+∞, d[s] ←0;

錛?錛?span style="FONT: 7pt Times New Roman; font-size-adjust: none; font-stretch: normal">    榪唬姹傝В錛氬弽澶嶅杈歸泦E涓殑姣忔潯杈硅繘琛屾澗寮涙搷浣滐紝浣垮緱欏剁偣闆哣涓殑姣忎釜欏剁偣v鐨勬渶鐭窛紱諱及璁″奸愭閫艱繎鍏舵渶鐭窛紱伙紱錛堣繍琛寍v|-1嬈★級

錛?錛?span style="FONT: 7pt Times New Roman; font-size-adjust: none; font-stretch: normal">    媯楠岃礋鏉冨洖璺細鍒ゆ柇杈歸泦E涓殑姣忎竴鏉¤竟鐨勪袱涓鐐規槸鍚︽敹鏁涖傚鏋滃瓨鍦ㄦ湭鏀舵暃鐨勯《鐐癸紝鍒欑畻娉曡繑鍥?/span>false錛岃〃鏄庨棶棰樻棤瑙o紱鍚﹀垯綆楁硶榪斿洖true錛屽茍涓斾粠婧愮偣鍙揪鐨勯《鐐?/span>v鐨勬渶鐭窛紱諱繚瀛樺湪 d[v]涓?/span>

綆楁硶鎻忚堪濡備笅錛?/span>

Bellman-Ford(G,w,s) 錛?/span>boolean   //鍥?/span>G 錛岃竟闆?/span> 鍑芥暟 w 錛?/span>s涓烘簮鐐?/span>

1        for each vertex v ∈ V錛圙錛?do        //鍒濆鍖?span> 1闃舵

2            d[v] ←+∞

3        d[s] ←0;                             //1闃舵緇撴潫

4        for i=1 to |v|-1 do               //2闃舵寮濮嬶紝鍙岄噸寰幆銆?/span>

5           for each edge錛坲,v錛?∈E(G) do //杈歸泦鏁扮粍瑕佺敤鍒幫紝絀蜂婦姣忔潯杈廣?/span>

6              If d[v]> d[u]+ w(u,v) then      //鏉懼紱鍒ゆ柇

7                 d[v]=d[u]+w(u,v)               //鏉懼紱鎿嶄綔   2闃舵緇撴潫

8        for each edge錛坲,v錛?∈E(G) do

9            If d[v]> d[u]+ w(u,v) then

10            Exit false

11    Exit true

涓嬮潰緇欏嚭鎻忚堪鎬ц瘉鏄庯細

   棣栧厛鎸囧嚭錛屽浘鐨勪換鎰忎竴鏉℃渶鐭礬寰勬棦涓嶈兘鍖呭惈璐熸潈鍥炶礬錛屼篃涓嶄細鍖呭惈姝f潈鍥炶礬錛屽洜姝ゅ畠鏈澶氬寘鍚?/span>|v|-1鏉¤竟銆?/span>

   鍏舵錛屼粠婧愮偣s鍙揪鐨勬墍鏈夐《鐐瑰鏋?/span> 瀛樺湪鏈鐭礬寰勶紝鍒欒繖浜涙渶鐭礬寰勬瀯鎴愪竴涓互s涓烘牴鐨勬渶鐭礬寰勬爲銆?/span>Bellman-Ford綆楁硶鐨勮凱浠f澗寮涙搷浣滐紝瀹為檯涓婂氨鏄寜欏剁偣璺濈s鐨勫眰嬈★紝閫愬眰鐢熸垚榪欐5鏈鐭礬寰勬爲鐨勮繃紼嬨?/span>

鍦ㄥ姣忔潯杈硅繘琛?span>1 閬嶆澗寮涚殑鏃跺欙紝鐢熸垚浜嗕粠s鍑哄彂錛屽眰嬈¤嚦澶氫負1鐨勯偅浜涙爲鏋濄備篃灝辨槸璇達紝鎵懼埌浜嗕笌s鑷沖鏈?鏉¤竟鐩歌仈鐨勯偅浜涢《鐐圭殑鏈鐭礬寰勶紱瀵規瘡鏉¤竟榪涜絎?閬嶆澗寮涚殑鏃跺欙紝鐢熸垚浜嗙2灞傛鐨勬爲鏋濓紝灝辨槸璇存壘鍒頒簡緇忚繃2鏉¤竟鐩歌繛鐨勯偅浜涢《鐐圭殑鏈鐭礬寰?#8230;…銆傚洜涓烘渶鐭礬寰勬渶澶氬彧鍖呭惈|v|-1 鏉¤竟錛屾墍浠ワ紝鍙渶瑕佸驚鐜瘄v|-1 嬈°?/span>

姣忓疄鏂戒竴嬈℃澗寮涙搷浣滐紝鏈鐭礬寰勬爲涓婂氨浼氭湁涓灞傞《鐐硅揪鍒板叾鏈鐭窛紱伙紝姝ゅ悗榪欏眰欏剁偣鐨勬渶鐭窛紱誨煎氨浼氫竴鐩翠繚鎸佷笉鍙橈紝涓嶅啀鍙楀悗緇澗寮涙搷浣滅殑褰卞搷銆傦紙浣嗘槸錛屾瘡嬈¤繕瑕佸垽鏂澗寮涳紝榪欓噷嫻垂浜嗗ぇ閲忕殑鏃墮棿錛屾庝箞浼樺寲錛熷崟綰殑浼樺寲鏄惁鍙錛燂級

濡傛灉娌℃湁璐熸潈鍥炶礬錛岀敱浜庢渶鐭礬寰勬爲鐨勯珮搴︽渶澶氬彧鑳芥槸|v|-1錛屾墍浠ユ渶澶氱粡榪噟v|-1閬嶆澗寮涙搷浣滃悗錛屾墍鏈変粠s鍙揪鐨勯《鐐瑰繀灝嗘眰鍑烘渶鐭窛紱匯傚鏋?d[v]浠嶄繚鎸?+∞錛屽垯琛ㄦ槑浠巗鍒皏涓嶅彲杈俱?/span>

濡傛灉鏈夎礋鏉冨洖璺紝閭d箞絎?span> |v|-1 閬嶆澗寮涙搷浣滀粛鐒朵細鎴愬姛錛岃繖鏃訛紝璐熸潈鍥炶礬涓婄殑欏剁偣涓嶄細鏀舵暃銆?/span>

渚嬪瀵逛簬涓婂浘錛岃竟涓婃柟妗嗕腑鐨勬暟瀛椾唬琛ㄦ潈鍊鹼紝欏剁偣A,B,C涔嬮棿瀛樺湪璐熸潈鍥炶礬銆係鏄簮鐐癸紝欏剁偣涓暟瀛楄〃紺鴻繍琛孊ellman-Ford綆楁硶鍚庡悇鐐圭殑鏈鐭窛紱諱及璁″箋?/span>

姝ゆ椂d[a] 鐨勫間負1錛屽ぇ浜巇[c]+w(c,a)鐨勫?2錛岀敱姝[a]鍙互鏉懼紱涓?2錛岀劧鍚巇[b]鍙堝彲浠ユ澗寮涗負-5,d[c]鍙堝彲浠ユ澗寮涗負-7.涓嬩竴涓懆鏈燂紝d[a]鍙堝彲浠ユ洿鏂頒負鏇村皬鐨勫鹼紝榪欎釜榪囩▼姘歌繙涓嶄細緇堟銆傚洜姝わ紝鍦ㄨ凱浠f眰瑙f渶鐭礬寰勯樁孌電粨鏉熷悗錛屽彲浠ラ氳繃媯楠岃竟闆咵鐨勬瘡鏉¤竟(u,v)鏄惁婊¤凍鍏崇郴寮?span> d[v]> d[u]+ w(u,v) 鏉ュ垽鏂槸鍚﹀瓨鍦ㄨ礋鏉冨洖璺?/span>

浜屻佸熀鏈?/span> Bellman-Ford 綆楁硶鐨?/span> pascal瀹炵幇銆?/span>

   瑙?/span> bellmanford.pas 鏂囦歡

涓夈佸熀鏈畻娉曚箣涓婄殑浼樺寲銆?/span>

鍒嗘瀽 Bellman-Ford綆楁硶錛屼笉闅劇湅鍑猴紝澶栧眰寰幆錛堣凱浠f鏁幫級|v|-1瀹為檯涓婂彇寰楁槸涓婇檺銆傜敱涓婇潰瀵圭畻娉曟紜х殑璇佹槑鍙煡錛岄渶瑕佺殑榪唬閬嶆暟絳変簬鏈鐭礬寰勬爲鐨勯珮搴︺傚鏋滀笉瀛樺湪璐熸潈鍥炶礬錛屽鉤鍧囨儏鍐典笅鐨勬渶鐭礬寰勬爲鐨勯珮搴﹀簲璇ヨ繙榪滃皬浜?/span> |v|-1錛屽湪姝ゆ儏鍐典笅錛屽浣欐渶鐭礬寰勬爲楂樼殑榪唬閬嶆暟灝辨槸鏃墮棿涓婄殑嫻垂錛岀敱姝わ紝鍙互渚濇鏉ュ疄鏂戒紭鍖栥?/span>

浠庣粏鑺備笂鍒嗘瀽錛屽鏋滃湪鏌愪竴閬嶈凱浠d腑錛岀畻娉曟弿榪頒腑絎?/span>7琛岀殑鏉懼紱鎿嶄綔鏈墽琛岋紝璇存槑璇ラ亶榪唬鎵鏈夌殑杈歸兘娌℃湁琚澗寮涖傚彲浠ヨ瘉鏄庯紙鎬庝箞璇佹槑錛燂級錛氳嚦姝ゅ悗錛岃竟闆嗕腑鎵鏈夌殑杈歸兘涓嶉渶瑕佸啀琚澗寮涳紝浠庤屽彲浠ユ彁鍓嶇粨鏉熻凱浠h繃紼嬨傝繖鏍鳳紝浼樺寲鐨勬帾鏂藉氨闈炲父綆鍗曚簡銆?/span>

璁懼畾涓涓竷灝斿瀷鏍囧織鍙橀噺 relaxed錛屽垵鍊間負false銆傚湪鍐呭眰寰幆涓紝浠呭綋鏈夎竟琚垚鍔熸澗寮涙椂錛屽皢 relaxed 璁劇疆涓?/span>true銆傚鏋滄病鏈夎竟琚澗寮涳紝鍒欐彁鍓嶇粨鏉熷灞傚驚鐜傝繖涓鏀硅繘鍙互鏋佸ぇ鐨勫噺灝戝灞傚驚鐜殑榪唬嬈℃暟銆備紭鍖栧悗鐨?/span> bellman-ford鍑芥暟濡備笅銆?/span>

function bellmanford(s:longint):boolean;

     begin

        for i:=1 to nv do

          d[i]:=max;

        d[s]:=0;

        for i:=1 to nv-1 do

         begin

         relaxed:=false;

          for j:=1 TO ne do

          if(d[edges[j].s]<>max) and (d[edges[j].e]>d[edges[j].s]+edges[j].w)

               then begin

d[edges[j].e]:=d[edges[j].s]+edges[j].w ;

relaxed:=true;

                         end;

                if not relaxed then break;

end;

        for i:=1 to ne do

          if d[edges[j].e]>d[edges[j].s]+edges[j].w then exit(false);

        exit(true);

     end;

榪欐牱鐪嬩技騫沖嚒鐨勪紭鍖栵紝浼氭湁鎬庢牱鐨勬晥鏋滃憿錛熸湁鐮旂┒琛ㄦ槑錛屽浜庨殢鏈虹敓鎴愭暟鎹殑騫沖潎鎯呭喌錛屾椂闂村鏉傚害鐨勪及綆楀叕寮忎負

1.13|E|                    if |E|<|V|

0.95*|E|*lg|V|              if |E|>|V|

浼樺寲鍚庣殑綆楁硶鍦ㄥ鐞嗘湁璐熸潈鍥炶礬鐨勬祴璇曟暟鎹椂錛岀敱浜庢瘡嬈¢兘浼氭湁杈硅鏉懼紱錛屾墍浠?/span>relaxed姣忔閮戒細琚疆涓?/span>true錛屽洜鑰屼笉鍙兘鎻愬墠緇堟澶栧眰寰幆銆傝繖瀵瑰簲浜嗘渶鍧忔儏鍐碉紝鍏舵椂闂村鏉傚害浠嶆棫涓?/span>O(VE)銆?/span>

浼樺寲鍚庣殑綆楁硶鐨勬椂闂村鏉傚害宸茬粡鍜岀敤浜屽弶鍫嗕紭鍖栫殑Dijkstra綆楁硶鐩歌繎浜嗭紝鑰岀紪鐮佺殑澶嶆潅紼嬪害榪滄瘮鍚庤呬綆銆傚姞涔?/span>Bellman-Ford綆楁硶鑳藉鐞嗗悇縐嶈竟鍊兼潈鎯呭喌涓嬬殑鏈鐭礬寰勯棶棰橈紝鍥犺岃繕鏄潪甯鎬紭縐鐨勩?/span>Usaco3.2.6 鐨勭▼搴忚bellmanford_1.pas

鍥涖?/span>SPFA 綆楁硶

   SPFA鏄洰鍓嶇浉褰撲紭縐鐨勬眰鏈鐭礬寰勭殑綆楁硶錛屽煎緱鎴戜滑鎺屾彙銆?/span>

   SPFA瀵?/span>Bellman-Ford綆楁硶浼樺寲鐨勫叧閿箣澶勫湪浜庢剰璇嗗埌錛?span style="COLOR: #ff6600">鍙湁閭d簺鍦ㄥ墠涓閬嶆澗寮涗腑鏀瑰彉浜嗚窛紱諱及璁″肩殑鐐癸紝鎵嶅彲鑳藉紩璧蜂粬浠殑閭繪帴鐐圭殑璺濈浼拌鍊肩殑鏀瑰彉銆?/span>鍥犳錛岀敤涓涓厛榪涘厛鍑虹殑闃熷垪鏉ュ瓨鏀捐鎴愬姛鏉懼紱鐨勯《鐐廣傚垵濮嬫椂錛屾簮鐐?/span>s鍏ラ槦銆傚綋闃熷垪涓嶄負絀烘椂錛屽彇鍑哄棣栭《鐐癸紝瀵瑰畠鐨勯偦鎺ョ偣榪涜鏉懼紱銆傚鏋滄煇涓偦鎺ョ偣鏉懼紱鎴愬姛錛屼笖璇ラ偦鎺ョ偣涓嶅湪闃熷垪涓紝鍒欏皢鍏跺叆闃熴傜粡榪囨湁闄愭鐨勬澗寮涙搷浣滃悗錛岄槦鍒楀皢涓虹┖錛岀畻娉曠粨鏉熴?/span>SPFA綆楁硶鐨勫疄鐜幫紝闇瑕佺敤鍒頒竴涓厛榪涘厛鍑虹殑闃熷垪 queue 鍜屼竴涓寚紺洪《鐐規槸鍚﹀湪闃熷垪涓殑 鏍囪鏁扮粍 mark銆備負浜嗘柟渚挎煡鎵炬煇涓《鐐圭殑閭繪帴鐐癸紝鍥鵑噰鐢ㄤ復鐣岃〃瀛樺偍銆?/span>

   紼嬪簭瀛樺偍鍦?/span> spfa.pas涓備互usaco 3.2.6 璇曢2涓轟緥銆傜敤閭繪帴琛ㄥ啓鐨勭▼搴忋?/span>

   闇瑕佹敞鎰忕殑鏄細浠呭綋鍥句笉瀛樺湪璐熸潈鍥炶礬鏃訛紝SPFA鑳芥甯稿伐浣溿傚鏋滃浘瀛樺湪璐熸潈鍥炶礬錛岀敱浜庤礋鏉冨洖璺笂鐨勯《鐐規棤娉曟敹鏁涳紝鎬繪湁欏剁偣鍦ㄥ叆闃熷拰鍑洪槦寰榪旓紝闃熷垪鏃犳硶涓虹┖錛岃繖縐嶆儏鍐典笅SPFA鏃犳硶姝e父緇撴潫銆?/span>

鍒ゆ柇璐熸潈鍥炶礬鐨?/span>鏂規寰堝錛屼笘闂存祦浼犳渶騫跨殑鏄褰曟瘡涓粨鐐硅繘闃熸鏁幫紝瓚呰繃|V|嬈¤〃紺烘湁璐熸潈

榪樻湁涓縐嶆柟娉曚負璁板綍榪欎釜緇撶偣鍦ㄨ礬寰勪腑澶勪簬鐨勪綅緗紝ord[i]錛屾瘡嬈℃洿鏂扮殑鏃跺?/span>ord[i]=ord[x]+1錛岃嫢瓚呰繃|V|鍒欒〃紺烘湁璐熷湀.....

鍏朵粬鏂規硶榪樻湁寰堝錛屾垜鍙嶅掕寰楁祦浼犳渶騫跨殑鏂規硶鏄渶鎱㈢殑.......

鍏充簬SPFA鐨勬椂闂村鏉傚害錛屼笉濂藉噯紜及璁★紝涓鑸涓烘槸 O錛?/span>kE錛夛紝k鏄父鏁?/span>

浜斻佹椂闂存晥鐜囧疄嫻?/span>

涓婅堪浠嬬粛鐨?/span>Bellman-Ford綆楁硶鍙婁袱縐嶇殑浼樺寲錛屽彧鏄湪鐞嗚涓婂垎鏋愪簡鏃墮棿澶嶆潅搴︼紝鐢ㄥ疄闄呯殑鏁版嵁嫻嬭瘯錛屼細鏈変粈涔堢粨鏋滃憿錛熶負姝わ紝鎴戜滑閫夋嫨 usaco 3.2.6銆?/span>

Spfa鐨勬椂闂存晥鐜囪繕鏄緢楂樼殑銆傚茍涓?/span>spfa鐨勭紪紼嬪鏉傚害瑕佹瘮Dijksta+heap浼樺寲瑕佸ソ鐨勫銆?/span>

鍏佺粨璁?/span>

緇忚繃浼樺寲Bellman-Ford綆楁硶鏄潪甯鎬紭鍖栫殑姹傚崟婧愭渶鐭礬寰勭殑綆楁硶錛?/span>SPFA鏃墮棿鏁堢巼瑕佷紭浜庣涓縐嶄紭鍖栧艦寮忥紝浣嗙涓縐嶄紭鍖栧艦寮忕殑緙栫爜澶嶆潅搴︿綆浜?/span>SPFA銆備袱縐嶄紭鍖栧艦寮忕殑緙栫▼澶嶆潅搴﹂兘浣庝簬Dijkstra綆楁硶銆傚鏋滃湪鍒ゆ柇鏄惁瀛樺湪璐熸潈鍥炶礬錛屾帹鑽愪嬌鐢ㄧ涓縐嶄紭鍖栧艦寮忥紝鍚﹀垯鎺ㄨ崘浣跨敤SPFA銆?/span>




Icyflame 2009-05-02 01:32 鍙戣〃璇勮
]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              国产精品久久久久久妇女6080| 欧美高清在线一区| 亚洲日本欧美日韩高观看| 亚洲一区二区三区四区在线观看| 欧美精品一区二区三区久久久竹菊 | 亚洲黄色天堂| 国产乱码精品| 中文在线资源观看视频网站免费不卡| 欧美日韩精品伦理作品在线免费观看| 一区二区三区视频在线| 每日更新成人在线视频| 久久久久久久久综合| 国产麻豆精品久久一二三| 91久久国产综合久久蜜月精品| 欧美午夜精彩| 亚洲黄色精品| 最新精品在线| 久久午夜羞羞影院免费观看| 久久久精品动漫| 国产一区二区日韩| 亚洲成色777777在线观看影院| 欧美日韩一区不卡| 亚洲区一区二| 在线亚洲电影| 国产精品国产a级| 一区二区三区导航| 亚洲图片欧美日产| 久久久久一区二区| 中文精品一区二区三区| 欧美精品国产一区二区| 亚洲伦理自拍| 亚洲午夜一区二区三区| 国产精品国产三级国产普通话99 | 久久久久网址| 国产揄拍国内精品对白| 久久国产精品99国产| 久久伊伊香蕉| 亚洲乱亚洲高清| 欧美日韩国产va另类| 中文日韩电影网站| 欧美一区二区三区在线免费观看| 欧美 日韩 国产 一区| 欧美激情国产精品| 在线亚洲美日韩| 国产精品第三页| 欧美在线三级| 91久久线看在观草草青青| 一区二区三区国产| 免费国产自线拍一欧美视频| 亚洲啪啪91| 午夜精品久久久久久久白皮肤| 免费国产自线拍一欧美视频| 亚洲精品影视| 久久爱另类一区二区小说| 在线不卡a资源高清| 欧美久久久久久久| 亚洲欧美影音先锋| 欧美插天视频在线播放| 一区二区三区高清| 韩国免费一区| 欧美日韩三级在线| 久久精品一区蜜桃臀影院| 亚洲精品在线看| 久久久久久久久综合| 国产精品私房写真福利视频| 久久九九热免费视频| 亚洲美女福利视频网站| 久久色在线播放| 99香蕉国产精品偷在线观看| 国产午夜精品一区理论片飘花| 亚洲免费在线视频一区 二区| 亚洲图片激情小说| 在线观看视频一区二区欧美日韩| 久久久福利视频| 在线视频亚洲欧美| 欧美激情中文不卡| 久久九九免费| 国产综合自拍| 国产精品免费一区豆花| 欧美a级片一区| 欧美影院久久久| 一区二区三区四区五区在线| 欧美国产激情| 一区二区三区欧美成人| 欧美日韩亚洲一区二区三区在线观看| 亚洲国产综合视频在线观看| 欧美一区网站| 亚洲男人的天堂在线aⅴ视频| 国产欧美日韩精品在线| 欧美日韩一卡二卡| 欧美电影电视剧在线观看| 久久久欧美精品sm网站| 亚洲欧美日韩精品久久久久| 久久精品男女| 亚洲欧美一区二区三区在线| 一区二区高清| 亚洲精品在线视频| 欧美午夜免费电影| 久久www免费人成看片高清| 妖精视频成人观看www| 亚洲国产日韩欧美在线图片| 欧美成人精品| 欧美夫妇交换俱乐部在线观看| 日韩视频在线播放| 亚洲激情网站| 亚洲乱码国产乱码精品精天堂| 国产精品国产成人国产三级| 欧美日韩国产综合网 | 一区二区欧美精品| 亚洲精品一线二线三线无人区| 欧美一级免费视频| 亚洲一区二区在线播放| 国内精品免费午夜毛片| 狠狠色丁香久久婷婷综合丁香| 欧美高清视频www夜色资源网| 亚洲午夜久久久久久久久电影网| 久久综合伊人| 欧美大片一区二区三区| 欧美国产极速在线| 亚洲日本一区二区| 99re热这里只有精品免费视频| 久久伊人一区二区| 欧美aa国产视频| 最新精品在线| 亚洲视频一起| 欧美有码在线观看视频| 老司机成人在线视频| 免费视频亚洲| 国产精品va在线播放| 欧美www在线| 欧美日韩精品国产| 国产麻豆精品视频| 依依成人综合视频| 在线一区二区三区做爰视频网站| 亚洲成人在线| 国内成人精品视频| 亚洲国产视频一区二区| 亚洲视频电影在线| 久久精品72免费观看| 欧美国产日韩一区二区| 久久精品天堂| 亚洲第一二三四五区| 一区二区三区av| 久久国产色av| 亚洲国内欧美| 国产精品高潮呻吟视频| 亚洲午夜羞羞片| 亚洲日本在线视频观看| 一区二区三区成人 | 亚洲乱码一区二区| 一区二区三区产品免费精品久久75| 亚洲第一网站免费视频| 一本色道久久88综合日韩精品 | 亚洲高清不卡在线| 亚洲午夜小视频| 免费久久99精品国产自在现线| 久久久久看片| 国产精品草莓在线免费观看| 欧美日韩综合一区| 伊人男人综合视频网| 亚洲综合色视频| 最新国产成人av网站网址麻豆| 欧美电影在线观看完整版| 一区二区三区视频在线| 久热精品视频| 国产日韩亚洲欧美综合| 国产一区二区三区在线观看精品 | 一区二区三区国产盗摄| 在线亚洲免费视频| 麻豆精品网站| 午夜精品短视频| 欧美午夜视频一区二区| 亚洲国产欧美在线人成| 久久精品在线观看| 亚洲香蕉网站| 国产精品成人播放| 亚洲理伦电影| 亚洲国产精品123| 久久免费视频一区| 欧美精品一区二区精品网| 极品日韩久久| 久久久av水蜜桃| 欧美一级精品大片| 国产精品午夜视频| 亚洲欧美bt| 亚洲一区二区高清视频| 欧美日韩在线大尺度| 一本一本a久久| 日韩亚洲精品视频| 欧美视频在线视频| 亚洲无线视频| 亚洲视频网在线直播| 国产精品对白刺激久久久| 中文国产亚洲喷潮| 一本久道久久综合狠狠爱| 欧美日韩免费观看一区三区| 亚洲九九爱视频| 99re国产精品| 国产精品露脸自拍| 久久av免费一区|