锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久夜色精品国产亚洲,久久久久99精品成人片三人毛片,久久久久一本毛久久久http://www.shnenglu.com/kuramawzw/category/11448.htmlBUAAzh-cnWed, 12 Aug 2009 14:04:28 GMTWed, 12 Aug 2009 14:04:28 GMT60Bellman-Ford 綆楁硶鍙婂叾浼樺寲SPFAhttp://www.shnenglu.com/kuramawzw/archive/2009/08/12/93085.htmlkuramawzwkuramawzwWed, 12 Aug 2009 13:49:00 GMThttp://www.shnenglu.com/kuramawzw/archive/2009/08/12/93085.htmlhttp://www.shnenglu.com/kuramawzw/comments/93085.htmlhttp://www.shnenglu.com/kuramawzw/archive/2009/08/12/93085.html#Feedback0http://www.shnenglu.com/kuramawzw/comments/commentRss/93085.htmlhttp://www.shnenglu.com/kuramawzw/services/trackbacks/93085.htmlBellman-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>1錛?/span>    鍒濆鍖栵細灝嗛櫎婧愮偣澶栫殑鎵鏈夐《鐐圭殑鏈鐭窛紱諱及璁″?/span> d[v] +∞, d[s] 0;

錛?/span>2錛?/span>    榪唬姹傝В錛氬弽澶嶅杈歸泦E涓殑姣忔潯杈硅繘琛屾澗寮涙搷浣滐紝浣垮緱欏剁偣闆?/span>V涓殑姣忎釜欏剁偣v鐨勬渶鐭窛紱諱及璁″奸愭閫艱繎鍏舵渶鐭窛紱伙紱錛堣繍琛?/span>|v|-1嬈★級

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

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

 1 G:鍥綠
 2 E(G):杈圭殑闆嗗悎
 3 S: 婧愰《鐐?br> 4 Dis[i]:琛ㄧずs鍒癷鐨勬渶鐭窛紱?鍒濆涓?/span>+
 5 D[s]=0;
 6 for (int i=0;i<|v|-1;i++)
 7  for each (u,v)∈E(G)
 8    if(dis[u]+w(u,v)<dis[v]
 9        dis[v]=dis[u]+w(u,v);
10 for each (u,v)∈E(G)
11  if(d[v]>d[u]+w(u,v)
12    return false;//榪斿洖false,璇存槑瀛樺湪璐熸潈鍥炶礬
13 return true;
14 
   

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

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

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

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

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

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

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

  

Bellman-Ford鐨勯槦鍒楀疄鐜?/span>SPFA

 綆楁硶澶ц嚧嫻佺▼鏄敤涓涓槦鍒楁潵榪涜緇存姢銆傚垵濮嬫椂灝嗘簮鍔犲叆闃熷垪銆傛瘡嬈′粠闃熷垪涓彇鍑轟竴涓厓绱狅紝騫跺鎵鏈変笌浠栫浉閭葷殑鐐硅繘琛?/span>鏉懼紱錛岃嫢鏌愪釜鐩擱偦鐨勭偣鏉懼紱鎴愬姛錛屽垯灝嗗叾鍏ラ槦銆傜洿鍒伴槦鍒椾負絀烘椂綆楁硶緇撴潫銆?/span>

榪欎釜綆楁硶錛岀畝鍗曠殑璇村氨鏄槦鍒椾紭鍖栫殑bellman-ford,鍒╃敤浜嗘瘡涓偣涓嶄細鏇存柊嬈℃暟澶鐨勭壒鐐瑰彂鏄庣殑姝ょ畻娉?/span>

SPFA鈥斺擲hortest Path Faster Algorithm錛屽畠鍙互鍦?/span>O(kE)鐨勬椂闂村鏉傚害鍐呮眰鍑烘簮鐐瑰埌鍏朵粬鎵鏈夌偣鐨勬渶鐭礬寰勶紝鍙互澶勭悊璐熻竟銆?/span>SPFA鐨勫疄鐜扮敋鑷蟲瘮Dijkstra鎴栬?/span>Bellman_Ford榪樿綆鍗曪細

璁?/span>Dist浠h〃S鍒?/span>I鐐圭殑褰撳墠鏈鐭窛紱伙紝Fa浠h〃S鍒?/span>I鐨勫綋鍓嶆渶鐭礬寰勪腑I鐐逛箣鍓嶇殑涓涓偣鐨勭紪鍙楓傚紑濮嬫椂Dist鍏ㄩ儴涓?/span>+∞錛屽彧鏈?/span>Dist[S]=0錛?/span>Fa鍏ㄩ儴涓?/span>0銆?/span>

緇存姢涓涓槦鍒楋紝閲岄潰瀛樻斁鎵鏈夐渶瑕佽繘琛岃凱浠g殑鐐廣傚垵濮嬫椂闃熷垪涓彧鏈変竴涓偣S銆傜敤涓涓竷灝旀暟緇勮褰曟瘡涓偣鏄惁澶勫湪闃熷垪涓?/span>

姣忔榪唬錛屽彇鍑洪槦澶寸殑鐐?/span>v錛屼緷嬈℃灇涓句粠v鍑哄彂鐨勮竟v->u錛岃杈圭殑闀垮害涓?/span>len錛屽垽鏂?/span>Dist[v]+len鏄惁灝忎簬Dist[u]錛岃嫢灝忎簬鍒欐敼榪?/span>Dist[u]錛屽皢Fa[u]璁頒負v錛屽茍涓旂敱浜?/span>S鍒?/span>u鐨勬渶鐭窛紱誨彉灝忎簡錛屾湁鍙兘u鍙互鏀硅繘鍏跺畠鐨勭偣錛屾墍浠ヨ嫢u涓嶅湪闃熷垪涓紝灝卞皢瀹冩斁鍏ラ槦灝俱傝繖鏍蜂竴鐩磋凱浠d笅鍘葷洿鍒伴槦鍒楀彉絀猴紝涔熷氨鏄?/span>S鍒版墍鏈夌殑鏈鐭窛紱婚兘紜畾涓嬫潵錛岀粨鏉熺畻娉曘傝嫢涓涓偣鍏ラ槦嬈℃暟瓚呰繃n錛屽垯鏈夎礋鏉冪幆銆?/span>

SPFA 鍦ㄥ艦寮忎笂鍜屽搴︿紭鍏堟悳绱㈤潪甯哥被浼鹼紝涓嶅悓鐨勬槸瀹藉害浼樺厛鎼滅儲涓竴涓偣鍑轟簡闃熷垪灝變笉鍙兘閲嶆柊榪涘叆闃熷垪錛屼絾鏄?/span>SPFA涓竴涓偣鍙兘鍦ㄥ嚭闃熷垪涔嬪悗鍐嶆琚斁鍏ラ槦鍒楋紝涔熷氨鏄竴涓偣鏀硅繘榪囧叾瀹冪殑鐐逛箣鍚庯紝榪囦簡涓孌墊椂闂村彲鑳芥湰韜鏀硅繘錛屼簬鏄啀嬈$敤鏉ユ敼榪涘叾瀹冪殑鐐癸紝榪欐牱鍙嶅榪唬涓嬪幓銆傝涓涓偣鐢ㄦ潵浣滀負榪唬鐐瑰鍏跺畠鐐硅繘琛屾敼榪涚殑騫沖潎嬈℃暟涓?/span>k錛屾湁鍔炴硶璇佹槑瀵逛簬閫氬父鐨勬儏鍐碉紝k鍦?/span>2宸﹀彸

 1 鍥綠
 2 闃熷垪 queue<int> q;
 3 Inque[i] 鏍囪i鏄惁鍦ㄩ槦鍒楅噷錛屽垵濮嬫墍鏈変負false
 4 S: 婧愰《鐐?br> 5 Dis[i]:琛ㄧずs鍒癷鐨勬渶鐭窛紱?鍒濆涓?/span>+
 6 
 7 Dis[s]=0;
 8 q.push(s);
 9 inque[s]=true;
10 while(q.size()>0)
11 {
12     Int t=q.front();
13 q.pop();
14 inque[t]=false;
15 for t’s adjacent vertex v
16  if(dis[t]+w(t,v)<dis[v])
17 {
18     Dis[v]=dis[t]+w(t,v);
19    If(!inque[v])
20    {
21         q.push(v);
22        inque[v]=true;
23     } 
24 }
25 
26 }
27 

   USACO 3.2 Sweet Butter
  

Sweet Butter

Greg Galperin -- 2001

Farmer John has discovered the secret to making the sweetest butter in all of Wisconsin: sugar. By placing a sugar cube out in the pastures, he knows the N (1 <= N <= 500) cows will lick it and thus will produce super-sweet butter which can be marketed at better prices. Of course, he spends the extra money on luxuries for the cows.

FJ is a sly farmer. Like Pavlov of old, he knows he can train the cows to go to a certain pasture when they hear a bell. He intends to put the sugar there and then ring the bell in the middle of the afternoon so that the evening's milking produces perfect milk.

FJ knows each cow spends her time in a given pasture (not necessarily alone). Given the pasture location of the cows and a description of the paths the connect the pastures, find the pasture in which to place the sugar cube so that the total distance walked by the cows when FJ rings the bell is minimized. FJ knows the fields are connected well enough that some solution is always possible.

PROGRAM NAME: butter

INPUT FORMAT

  • Line 1: Three space-separated integers: N, the number of pastures: P (2 <= P <= 800), and the number of connecting paths: C (1 <= C <= 1,450). Cows are uniquely numbered 1..N. Pastures are uniquely numbered 1..P.
  • Lines 2..N+1: Each line contains a single integer that is the pasture number in which a cow is grazing. Cow i's pasture is listed on line i+1.
  • Lines N+2..N+C+1: Each line contains three space-separated integers that describe a single path that connects a pair of pastures and its length. Paths may be traversed in either direction. No pair of pastures is directly connected by more than one path. The first two integers are in the range 1..P; the third integer is in the range (1..225).

SAMPLE INPUT (file butter.in)

3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5

INPUT DETAILS

This diagram shows the connections geometrically:

OUTPUT FORMAT

  • Line 1: A single integer that is the minimum distance the cows must walk to a pasture with a sugar cube.

SAMPLE OUTPUT (file butter.out)

8
OUTPUT DETAILS:
Putting the cube in pasture 4 means: cow 1 walks 3 units; cow 2 walks 5
units; cow 3 walks 0 units -- a total of 8.
瑙g瓟:
   榪欓亾棰樼洿鎺ョ敤涓鑸殑Dijkstra綆楁硶O(P2),涓鍏辮皟鐢≒嬈ijkstra,鎬諱綋澶嶆潅搴(P3),p=800,鑲畾瓚呮椂,鍦ㄨ繖閲岀敤SPFA綆楁硶,O(k*c),k鏄?宸﹀彸鐨勫父鏁?
璋冪敤p嬈?鏁翠綋澶嶆潅搴(p*c*k).鍦?.2縐掑彲浠ュ緱鍑鴻В.闄勫師鐮?/pre>
/*
ID: kuramaw1
PROG: butter
LANG: C++
*/

#include 
<fstream>
#include 
<queue>

using std::ifstream;
using std::ofstream;
using std::queue;
using std::endl;
using std::vector;


#define  MAX_EDGE 1451
#ifndef INT_MAX 
#define  INT_MAX 2147483647
#endif

struct graph
{
    
struct Edge
    {
        
short n; // next adjacent edge
        short v; // to which vertex
        short c; // weight
        Edge(const short _n=-1,const short _v=-1,const short _c=0):n(-n),v(_v),c(-c)
        {

        }
        Edge(
const Edge &e):n(e.n),v(e.v),c(e.c)
        {

        }
        Edge 
& operator =(const Edge &e)
        {
            n
=e.n;
            v
=e.v;
            c
=e.c;
            
return *this;

        }
    };
    
struct Ver
    {
        
short w;
        
short e;//frist e
        Ver(const short _w=0,const short _e=-1):w(_w),e(_e)
        {

        }
        Ver(
const Ver &v):w(v.w),e(v.e)
        {

        }
        Ver 
& operator =(const Ver &v)
        {
            w
=v.w;
            e
=v.e;
            
return *this;
        }
    };
    typedef std::vector
<Edge> EdgeSet;
    typedef std::vector
<Ver> VertSet;
    
    VertSet _V;
    EdgeSet _E;

    
// interfaces
    inline void Reset(const short &n)
    {
        _V.resize(n);
        _E.clear();
        _E.reserve(MAX_EDGE);
    }
    inline 
void IncVetWei(const short &i)
    {
        _V[i].w
++;
    }
    inline 
void InsertEdge(short u, short v, short c)
    {
        Edge e;
        e.v 
= v, e.c = c, e.n = _V[u].e;
        _V[u].e 
= _E.size();
        _E.push_back(e);

        e.v 
= u, e.c = c, e.n = _V[v].e;
        _V[v].e 
= _E.size();
        _E.push_back(e);
    }
    
int short_dis_sum(const short &s)
    {
        vector
<int> dis;
        queue
<short> q;
        vector
<bool> b_in_que;
        dis.resize(_V.size(),INT_MAX);
        b_in_que.resize(_V.size(),
false);

        q.push(s);
        dis[s]
=0;
        b_in_que[s]
=true;
        
while(q.size()>0)
        {
            
short t=q.front();
            q.pop();
            b_in_que[t]
=false;
            
            
short e=_V[t].e;
            
while(e!=-1)
            {
                Edge 
&edge=_E[e];
                
if(dis[t]+edge.c<dis[edge.v])
                {
                    dis[edge.v]
=dis[t]+edge.c;
                    
if(!b_in_que[edge.v])
                    {
                        q.push(edge.v);
                        b_in_que[edge.v]
=true;
                    }
                }
                e
=edge.n;
            }
        }

        
int sum(0);
        
for(short i=0;i<dis.size();i++)
         
if(_V[i].w>0)
         {
             sum
+=_V[i].w*dis[i];
         }
       
return sum;
    }

};

graph g;

short n,p,c;

int main()
{
   ifstream 
in("butter.in");
   
in>>n>>p>>c;
   
   g.Reset(p);
   
for(short i=0;i<n;i++)
   {
       
short v;
       
in>>v;
       g.IncVetWei(v
-1);
   }
   
for(short i=0;i<c;i++)
   {
       
short u,v,w;
       
in>>u>>v>>w;
       g.InsertEdge(u
-1,v-1,w);
   }

   
in.close();

   
int min_dis=INT_MAX;

   
for(int i=0;i<p;i++)
   {
       
int dis=g.short_dis_sum(i);
       
if(dis<min_dis)
           min_dis
=dis;
   }

   
//out
   ofstream out("butter.out");
   
out<<min_dis<<endl;
   
out.close();

}
 
 
 

 



kuramawzw 2009-08-12 21:49 鍙戣〃璇勮
]]>Eulerian tourhttp://www.shnenglu.com/kuramawzw/archive/2009/08/12/93078.htmlkuramawzwkuramawzwWed, 12 Aug 2009 13:18:00 GMThttp://www.shnenglu.com/kuramawzw/archive/2009/08/12/93078.htmlhttp://www.shnenglu.com/kuramawzw/comments/93078.htmlhttp://www.shnenglu.com/kuramawzw/archive/2009/08/12/93078.html#Feedback0http://www.shnenglu.com/kuramawzw/comments/commentRss/93078.htmlhttp://www.shnenglu.com/kuramawzw/services/trackbacks/93078.html闂鎻忚堪:
      
緇欏畾涓涓棤鍚戝浘G,涓鏉¤礬寰勭粡榪囧浘G鐨勬瘡涓鏉¤竟,涓斾粎緇忚繃涓嬈?span lang=EN-US>,榪欐潯璺緞縐頒負嬈ф媺璺緞(Eulerian Tour),濡傛灉嬈ф媺璺緞鐨勮搗濮嬮《鐐瑰拰緇堢偣鏄悓涓欏剁偣,鍒欑О涓烘鎷夊洖璺?span lang=EN-US>(Eulerian circuit).
    綆楁硶:
   
鏃犲悜鍥?span lang=EN-US>G瀛樺湪嬈ф媺璺緞鐨勫厖瑕佹潯浠?span lang=EN-US>:鍥?span lang=EN-US>G鏄繛閫氱殑,涓旇嚦澶氶櫎涓や釜鐐瑰(鍙互涓?span lang=EN-US>0涓?span lang=EN-US>,榪炴帴鍥句笉鍙兘鏈変笖浠呮湁涓涓《鐐圭殑搴︿負濂囨暟)鍏跺畠鎵鏈夐《鐐圭殑搴︿負鍋舵暟.
   
鏃犲悜鍥?span lang=EN-US>G瀛樺湪嬈ф媺鍥炶礬鐨勫厖瑕佹潯浠?span lang=EN-US>:鍥?span lang=EN-US>G鏄繛閫氱殑涓旀墍鏈夐《鐐圭殑搴︿負鍋舵暟;
    
綆楁硶鎻忚堪:
    
 1 tour: 鏁扮粍,鐢ㄤ簬瀛樺偍嬈ф媺璺緞,鍙嶅簭杈撳嚭鍗充負嬈ф媺璺緞
 2 pos: int      
 3 
   find_eulerian_circuit()      
 4 {             
 5     pos=0;            
 6     find_circuit(1);      
 7 }     
 8 
   find_eulerian_tour()     
 9 {            
10    find a vertex i ,the degree of which is odd           
11    pos=0;            
12    find_circuit(i);    
13 }      
14 
   find_circuit(vertex i)      
15 {         
16     while(exist j,(i,j) is the edge of G)          
17     {               
18        remove edge(i,j);                
19        find_circuit(j);           
20     }            
21     tour[pos++]=i;      
22 
23 
 

 USACO  3.2 Riding the fence,灝辨槸涓涓眰嬈ф媺璺緞鐨勯棶棰?span>.
 
闂鎻忚堪:    

Farmer John owns a large number of fences that must be repaired annually. He traverses the fences by riding a horse along each and every one of them (and nowhere else) and fixing the broken parts.

Farmer John is as lazy as the next farmer and hates to ride the same fence twice. Your program must read in a description of a network of fences and tell Farmer John a path to traverse each fence length exactly once, if possible. Farmer J can, if he wishes, start and finish at any fence intersection.

Every fence connects two fence intersections, which are numbered inclusively from 1 through 500 (though some farms have far fewer than 500 intersections). Any number of fences (>=1) can meet at a fence intersection. It is always possible to ride from any fence to any other fence (i.e., all fences are "connected").

Your program must output the path of intersections that, if interpreted as a base 500 number, would have the smallest magnitude.

There will always be at least one solution for each set of input data supplied to your program for testing.

PROGRAM NAME: fence

INPUT FORMAT

Line 1:

The number of fences, F (1 <= F <= 1024)

Line 2..F+1:

A pair of integers (1 <= i,j <= 500) that tell which pair of intersections this fence connects.

SAMPLE INPUT (file fence.in)

9

1 2

2 3

3 4

4 2

4 5

2 5

5 6

5 7

4 6

OUTPUT FORMAT

The output consists of F+1 lines, each containing a single integer. Print the number of the starting intersection on the first line, the next intersection's number on the next line, and so on, until the final intersection on the last line. There might be many possible answers to any given input set, but only one is ordered correctly.

SAMPLE OUTPUT (file fence.out)

1

2

3

4

2

5

4

6

5

7

   瑙g瓟:綆鍗曠殑嬈ф媺璺緞闂,鍥鵑噰鐢ㄩ偦鎺ヨ〃瀛樺偍,闄勫師鐮?span>

  
/*
ID: kuramaw1
PROG: fence
LANG: C++
*/

#include 
<fstream>

using std::ifstream;
using std::ofstream;
using std::endl;

#ifdef _DEBUG
#include 
<iostream>
using std::cout;
#endif

#define  MAX_V 500
#define  MAX_EDGE 1025

#define  MAX(a,b) ((a)>(b)?(a):(b))

struct grapha
{
    
struct node
    {
        
short v;
        node 
* next;
        node(
const short _v=-1):v(_v),next(NULL)
        {

        }
        
    };

    
struct ver
    {
        node 
* r;
        
short d;//degree
        ver():d(0)
        {
            r
=new node();

        }
        
~ver()
        {
            node 
* n=r;
            
while(n!=NULL)
            {
                node 
* t=n;
                n
=n->next;
                delete t;
            }
        }
        inline 
void add_neighbor(const short &v)
        {
            node 
* t=new node(v);
            node 
* p=r;
            node 
* n=p->next;
            
while(n!=NULL && v>n->v)
            {
                p
=n;
                n
=n->next;
            }
            p
->next=t;
            t
->next=n;
            d
++;
        }
        inline 
void  remove_neighbor(const short &v)
        {
            node 
* p=r;
            node 
* n=p->next;
            
while(n!=NULL && v!=n->v)
            {
                p
=n;
                n
=n->next;
            }
            
if(n!=NULL)
            {
                p
->next=n->next;
                delete n;
                d
--;
            }

        }
    };

    ver v[MAX_V];
    
short n;
    
short * tour;
    
short pos;

    grapha():n(
0),tour(NULL)
    {

    }

    
void add_edge(const short  &_u,const short &_v)
    {
        v[_u
-1].add_neighbor(_v-1);
        v[_v
-1].add_neighbor(_u-1);
        
short t=MAX(_u,_v);
        
if(t>n)
            n
=t;
    }

    
void find_tour(const short &s)
    {
            
while(v[s].d>0)
            {
                
short j=v[s].r->next->v;
                v[s].remove_neighbor(j);
                v[j].remove_neighbor(s);
                find_tour(j);
            }
            tour[pos
++]=s+1;

    }

    
void Eulerian_tour(short * _tour)
    {
        tour
=_tour;
        pos
=0;
        
bool b=false;
        
for(int i=0;i<n;i++)
         
if(v[i].d % 2!=0)
         {
             find_tour(i);
             b
=true;
             
break;
         }
       
if(!b)
           find_tour(
0);

    }


};

grapha g;
short tour[MAX_EDGE];
short f;
int main()
{
    ifstream 
in("fence.in");
    
in>>f;
    
for(short i=0;i<f;i++)
    {
        
short u,v;
        
in>>u>>v;
        g.add_edge(u,v);
    }

    
//do
    g.Eulerian_tour(tour);



    
//out
    ofstream out("fence.out");
    
for(int i=f;i>=0;i--)
     
out<<tour[i]<<endl;
    
out.close();
}

    


kuramawzw 2009-08-12 21:18 鍙戣〃璇勮
]]>
亚洲精品高清国产一久久| 国产精品久久久久蜜芽| 国产精品一久久香蕉国产线看| 人妻无码久久一区二区三区免费| 久久99精品久久只有精品| 91性高湖久久久久| 久久久久高潮综合影院| 久久―日本道色综合久久| 亚洲精品视频久久久| 国产情侣久久久久aⅴ免费| 久久影院午夜理论片无码 | 久久久无码精品亚洲日韩蜜臀浪潮| 久久人人爽人人爽人人AV东京热| 精品综合久久久久久88小说| av色综合久久天堂av色综合在| 免费观看久久精彩视频| 国内精品伊人久久久久777| 国产L精品国产亚洲区久久| 色婷婷综合久久久中文字幕| 亚洲国产成人久久综合区| 色成年激情久久综合| 色诱久久久久综合网ywww| 人人狠狠综合久久亚洲高清| 亚洲一区中文字幕久久| 国内精品人妻无码久久久影院 | 久久人人爽人人人人爽AV| 久久久久人妻一区精品| 久久久久久狠狠丁香| 久久99国内精品自在现线| 久久久久国产精品嫩草影院| 国产精品久久久久久久久软件| 久久久99精品成人片中文字幕| 99久久精品国内| 国产成人精品久久二区二区| 波多野结衣中文字幕久久 | 久久精品国产亚洲AV久| 久久笫一福利免费导航 | 狠狠色婷婷久久一区二区三区| 伊人久久综合无码成人网| 亚洲中文精品久久久久久不卡| 亚洲国产精品久久久天堂|