锘??xml version="1.0" encoding="utf-8" standalone="yes"?>一本久久综合亚洲鲁鲁五月天,999久久久国产精品,久久久久久久久无码精品亚洲日韩http://www.shnenglu.com/luyulaile/category/11037.htmlI canzh-cnSun, 17 Jun 2012 01:43:35 GMTSun, 17 Jun 2012 01:43:35 GMT60Dijkstra(榪澃鏂壒鎷?綆楁硶http://www.shnenglu.com/luyulaile/archive/2012/06/16/179006.htmlluisluisFri, 15 Jun 2012 19:53:00 GMThttp://www.shnenglu.com/luyulaile/archive/2012/06/16/179006.htmlhttp://www.shnenglu.com/luyulaile/comments/179006.htmlhttp://www.shnenglu.com/luyulaile/archive/2012/06/16/179006.html#Feedback0http://www.shnenglu.com/luyulaile/comments/commentRss/179006.htmlhttp://www.shnenglu.com/luyulaile/services/trackbacks/179006.html銆Dijkstra(榪澃鏂壒鎷?綆楁硶鏄吀鍨嬬殑鍗曟簮鏈鐭礬寰勭畻娉曪紝鐢ㄤ簬璁$畻涓涓妭鐐瑰埌鍏朵粬鎵鏈夎妭鐐圭殑鏈鐭礬寰勩備富瑕佺壒鐐規槸浠ヨ搗濮嬬偣涓轟腑蹇冨悜澶栧眰灞傛墿灞曪紝鐩村埌鎵╁睍鍒扮粓鐐逛負姝€侱ijkstra綆楁硶鏄緢鏈変唬琛ㄦх殑鏈鐭礬寰勭畻娉曪紝鍦ㄥ緢澶氫笓涓氳紼嬩腑閮戒綔涓哄熀鏈唴瀹規湁璇︾粏鐨勪粙緇嶏紝濡傛暟鎹粨鏋勶紝鍥捐錛岃繍絳瑰絳夌瓑銆侱ijkstra涓鑸殑琛ㄨ堪閫氬父鏈変袱縐嶆柟寮忥紝涓縐嶇敤姘鎬箙鍜屼復鏃舵爣鍙鋒柟寮忥紝涓縐嶆槸鐢∣PEN, CLOSE琛ㄧ殑鏂瑰紡錛岃繖閲屽潎閲囩敤姘鎬箙鍜屼復鏃舵爣鍙風殑鏂瑰紡銆傛敞鎰忚綆楁硶瑕佹眰鍥句腑涓嶅瓨鍦ㄨ礋鏉冭竟銆?/span>

闂鎻忚堪

銆銆鍦ㄦ棤鍚戝浘 G=(V,E) 涓紝鍋囪姣忔潯杈?E[i] 鐨勯暱搴︿負 w[i]錛屾壘鍒扮敱欏剁偣 V0 鍒板叾浣欏悇鐐圭殑鏈鐭礬寰勩傦紙鍗曟簮鏈鐭礬寰?/a>錛?/span> 


榪澃鏂壒鎷?/a>(Dijkstra)綆楁硶鎬濇兂
銆銆
鎸夎礬寰勯暱搴﹂掑嬈″簭浜х敓鏈鐭礬寰勭畻娉曪細
銆銆鎶奦鍒嗘垚涓ょ粍錛?/span>
銆銆錛?錛塖錛氬凡姹傚嚭鏈鐭礬寰勭殑欏剁偣鐨勯泦鍚?/span>
銆銆錛?錛塚-S=T錛氬皻鏈‘瀹氭渶鐭礬寰勭殑欏剁偣闆嗗悎
銆銆灝員涓《鐐規寜鏈鐭礬寰勯掑鐨勬搴忓姞鍏ュ埌S涓紝
銆銆淇濊瘉錛氾紙1錛変粠婧愮偣V0鍒癝涓悇欏剁偣鐨勬渶鐭礬寰勯暱搴﹂兘涓嶅ぇ浜?/span>
銆銆浠嶸0鍒癟涓換浣曢《鐐圭殑鏈鐭礬寰勯暱搴?/span>
銆銆錛?錛夋瘡涓《鐐瑰搴斾竴涓窛紱誨?/span>
銆銆S涓《鐐癸細浠嶸0鍒版欏剁偣鐨勬渶鐭礬寰勯暱搴?/span>
銆銆T涓《鐐癸細浠嶸0鍒版欏剁偣鐨勫彧鍖呮嫭S涓《鐐逛綔涓棿
銆銆欏剁偣鐨勬渶鐭礬寰勯暱搴?/span>
銆銆渚濇嵁錛氬彲浠ヨ瘉鏄嶸0鍒癟涓《鐐筕k鐨勬渶鐭礬寰勶紝鎴栨槸浠嶸0鍒癡k鐨?/span>
銆銆鐩存帴璺緞鐨勬潈鍊鹼紱鎴栨槸浠嶸0緇廠涓《鐐瑰埌Vk鐨勮礬寰勬潈鍊間箣鍜?/span>
銆銆錛堝弽璇佹硶鍙瘉錛?/span>
銆銆姹傛渶鐭礬寰勬楠?/em>
銆銆綆楁硶姝ラ濡備笅錛?/span>
銆銆1. 鍒濅嬌鏃朵護 S={V0},T={鍏朵綑欏剁偣}錛孴涓《鐐瑰搴旂殑璺濈鍊?/span>
銆銆鑻ュ瓨鍦?lt;V0,Vi>錛宒(V0,Vi)涓?lt;V0,Vi>寮т笂鐨勬潈鍊?/span>
銆銆鑻ヤ笉瀛樺湪<V0,Vi>錛宒(V0,Vi)涓?#8733;
銆銆2. 浠嶵涓夊彇涓涓叾璺濈鍊間負鏈灝忕殑欏剁偣W涓斾笉鍦⊿涓紝鍔犲叆S
銆銆3. 瀵筎涓《鐐圭殑璺濈鍊艱繘琛屼慨鏀癸細鑻ュ姞榪沇浣滀腑闂撮《鐐癸紝浠嶸0鍒癡i鐨?/span>
銆銆璺濈鍊兼瘮涓嶅姞W鐨勮礬寰勮鐭紝鍒欎慨鏀規璺濈鍊?/span>
銆銆閲嶅涓婅堪姝ラ2銆?錛岀洿鍒癝涓寘鍚墍鏈夐《鐐癸紝鍗砈=T涓烘 

浠g爜錛?婧愬湴鍧錛?span style="color: #008000; font-size: 13px; background-color: #eeeeee; ">www.cnblogs.com/newwy
 

/*********************************
*   鏈鐭礬寰?--Dijkstra綆楁硶瀹炵幇 
*   銆銆銆HDU錛?544 
*   BLOG:www.cnblogs.com/newwy
*   AUTHOR:Wang Yong
*********************************
*/
#include <iostream>
#define MAX 100
#define INF 1000000000
using namespace std;
 int dijkstra (int mat[][MAX],int n, int s,int f)
 {
     int dis[MAX];
     int mark[MAX];//璁板綍琚変腑鐨勭粨鐐?nbsp;
     int i,j,k = 0;
     for(i = 0 ; i < n ; i++)//鍒濆鍖栨墍鏈夌粨鐐癸紝姣忎釜緇撶偣閮芥病鏈夎閫変腑 
         mark[i] = 0;
    for(i = 0 ; i < n ; i++)//灝嗘瘡涓粨鐐瑰埌start緇撶偣weight璁板綍涓哄綋鍓峝istance 
    {
        dis[i] = mat[s][i];
        //path[i] = s;
    }
    mark[s] = 1;//start緇撶偣琚変腑 
    
//path[s] = 0;
    dis[s] = 0;//灝唖tart緇撶偣鐨勭殑璺濈璁劇疆涓? 
    int min ;//璁劇疆鏈鐭殑璺濈銆?nbsp;
    for(i = 1 ; i < n; i++)
    {
        min = INF;
        for(j = 0 ; j < n;j++)
        {
            if(mark[j] == 0  && dis[j] < min)//鏈閫変腑鐨勭粨鐐逛腑錛岃窛紱繪渶鐭殑琚変腑 
            {
                min = dis[j] ;
                k = j;
            }
        }
        mark[k] = 1;//鏍囪涓鴻閫変腑 
        for(j = 0 ; j < n ; j++)
        {
            if( mark[j] == 0  && (dis[j] > (dis[k] + mat[k][j])))//淇敼鍓╀綑緇撶偣鐨勬渶鐭窛紱?nbsp;
            {
                dis[j] = dis[k] + mat[k][j];
            }
        }
    }
    return dis[f];    
 } 
 int mat[MAX][MAX];
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m))
    {
        int a,b,dis;
        if(n == 0 || m == 0)
            break;
        int i,j;
        for(i = 0 ; i < n;i++)
            for(j = 0 ; j < n; j++)
                mat[i][j] = INF;
        for(i = 0 ; i < m ;i++)
        {
            scanf("%d %d %d",&a,&b,&dis);
            --a,--b;
            if(dis < mat[a][b] || dis < mat[b][a])
            mat[a][b] = mat[b][a] = dis;
        }
        int ans = dijkstra(mat,n,0,n-1);
        printf("%d\n",ans);
    }
 
}

鍙敤 浼樺厛闃熷垪浼樺寲


鍏朵粬瑙i噴錛?br />
http://blog.csdn.net/jiahui524/article/details/6636913 

luis 2012-06-16 03:53 鍙戣〃璇勮
]]>
dijkstra綆楁硶 鏈灝忓爢浼樺寲 <涓嶇敤stl>杞笘http://www.shnenglu.com/luyulaile/archive/2009/08/09/92739.htmlluisluisSun, 09 Aug 2009 13:28:00 GMThttp://www.shnenglu.com/luyulaile/archive/2009/08/09/92739.htmlhttp://www.shnenglu.com/luyulaile/comments/92739.htmlhttp://www.shnenglu.com/luyulaile/archive/2009/08/09/92739.html#Feedback0http://www.shnenglu.com/luyulaile/comments/commentRss/92739.htmlhttp://www.shnenglu.com/luyulaile/services/trackbacks/92739.html------------
妯℃澘鏄疕DOJ 2544
鎴戝啓鐨勬槸璁板綍姣忎釜鐐瑰湪鍫嗕腑鐨勪綅緗甀ncreaseKey錛屼篃鍙互Relax鍚庣洿鎺ュ線閲屾彃錛岀敤涓猙ool鏁扮粍璁板綍涓涓?br>
-----
浼樺寲涔嬪鍦ㄤ簬鏃墮棿澶嶆潅搴︾敱n^2鍙樹負nlgn
-----
#include<cstdio>
#include<cstdlib>

int n,m;
int map[101][101],d[101];

class Heap
{
    public:
        int handle[101];
        void Build(int n)
        {
            for(int i=1;i<=n;i++) a[i]=handle[i]=i;
            size=n;
        }
        void Percup(int p)
        {
            int temp=a[p];
            for(;d[temp]<d[a[p>>1]];p>>=1)
            {
                a[p]=a[p>>1];
                handle[a[p]]=p;
            }
            a[p]=temp;
            handle[a[p]]=p;
        }
        int DeleteMin()
        {
            int val=a[1];
            a[1]=a[size--];
            Percdown(1);
            handle[val]=0;
            return val;
        }
        bool Empty() {return size==0;}
    private:
        int a[101],size;
        void Percdown(int p)
        {
            int temp=a[p],child;
            for(;(p<<1)<=size;p=child)
            {
                child=p<<1;
                if(child+1<=size && d[a[child+1]]<d[a[child]]) child++;
                if(d[temp]<d[a[child]]) break;
                a[p]=a[child];
                handle[a[p]]=p;
            }
            a[p]=temp;
            handle[a[p]]=p;
        }
}h;

void init()
{
    int i,j,c;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++) map[i][j]=0x7fffffff;
        d[i]=0x7fffffff;
    }
    d[1]=0;
    while(m--)
    {
        scanf("%d%d%d",&i,&j,&c);
        map[i][j]=map[j][i]=c;
    }
}

void Relax(int num)
{
    for(int i=1;i<=n;i++)
        if(h.handle[i] && map[num][i]!=0x7fffffff && d[i]>d[num]+map[num][i])
        {
            d[i]=d[num]+map[num][i];
            h.Percup(h.handle[i]);
        }
}

void dijk()
{
    h.Build(n);
    while(!h.Empty()) Relax(h.DeleteMin());
}

int main()
{
    while(scanf("%d%d",&n,&m) && n+m)
    {
        init();
        dijk();
        printf("%d\n",d[n]);
    }
    system("pause");
    return 0;
}
---------------------
鍙︿竴涓増鏈?br>
#include <stdio.h>
const int SIZE= 30005, MAXX=2000000000;
int n,m,tail, dist[SIZE];
bool mark[SIZE];
struct Node
{
int id, val;
Node* next;
}node[SIZE];
struct Heap{
int id, val;
}heap[2*SIZE];
void insert(int a, int b, int val)
{
Node* p = new Node;
p->id = a;
p->val = val;
p->next = node[b].next;
node[b].next = p;
}
void heappush(int id, int val)
{
heap[++tail].id = id;
heap[tail].val = val;
int child, parent, temp;
child = tail;
while((parent = child/2)>=1)
{
if(heap[child].val < heap[parent].val) // child's val < parent's val ; swap ,filter
{
temp = heap[child].id;
heap[child].id = heap[parent].id;
heap[parent].id = temp;
temp = heap[child].val;
heap[child].val = heap[parent].val;
heap[parent].val = temp;
child = parent;
}
else
break;
}
}
int heappop()
{
int child, parent, temp,ret;
ret = heap[1].id;
heap[1].id = heap[tail].id; 
// swap the tail to the root
heap[1].val = heap[tail--].val;
parent = 1;
while((child=2*parent)<=tail)
{
if(child+1<=tail && heap[child+1].val < heap[child].val)
child++;
if(heap[child].val<heap[parent].val)
{
temp = heap[child].id;
heap[child].id = heap[parent].id;
heap[parent].id = temp;
temp = heap[child].val;
heap[child].val = heap[parent].val;
heap[parent].val = temp;
parent = child;
}
else
break;
}
return ret;
}
int dijkstra(int s, int t)
{
int i;
for(i=1; i<=n; i++)
{
dist[i] = MAXX;
mark[i] = false;
}
Node* p;
p = node[s].next;
while(p)
{
dist[p->id] = p->val;
heappush(p->id, p->val);
p = p->next;
}
mark[s] = true; dist[s] = 0;
for(i=1; i<n; i++)
{
int pop = heappop();
while(mark[pop])
pop = heappop();
if(pop ==t || dist[pop]==MAXX)	break;
mark[pop] = true;
p = node[pop].next;
while(p)
{
if(!mark[p->id]&&
(dist[p->id]==MAXX || dist[pop]+p->val< dist[p->id]))
{
dist[p->id] = dist[pop] + p->val;
heappush(p->id, dist[p->id]);
}
p = p->next;
}
}
return dist[t];
}
int main()
{
int i,a,b,c;
scanf("%d%d", &n, &m);
for(i=1; i<=m; i++)
{
scanf("%d%d%d", &a, &b, &c);
insert(a, b, c);
}
printf("%d\n", dijkstra(n,1));
return 0;
}


luis 2009-08-09 21:28 鍙戣〃璇勮
]]>
姹傛渶灝忔敮鎾戞暟 prim綆楁硶 瀹炵幇http://www.shnenglu.com/luyulaile/archive/2009/08/09/92730.htmlluisluisSun, 09 Aug 2009 11:52:00 GMThttp://www.shnenglu.com/luyulaile/archive/2009/08/09/92730.htmlhttp://www.shnenglu.com/luyulaile/comments/92730.htmlhttp://www.shnenglu.com/luyulaile/archive/2009/08/09/92730.html#Feedback0http://www.shnenglu.com/luyulaile/comments/commentRss/92730.htmlhttp://www.shnenglu.com/luyulaile/services/trackbacks/92730.html#define MAX 0xfffffff
#define MaxVertex 21
//prim姹傛渶灝忔敮鎸佹爲錛屽鐢ㄤ簬姹傝竟紼犲瘑緗戠殑鏈灝忔敮鎸佹爲 鏃墮棿澶嶆潅搴(n*n)n涓洪《鐐規暟;
#define  vertextype int
int n;
bool s[MaxVertex];//璇ョ偣鏄惁琚闂?
vertextype cost[MaxVertex];
vertextype dist[MaxVertex][MaxVertex];
void Init()
{
 int i,j,a,b,c;
  scanf("%d",&n);//鍏堣緭鍏ョ偣涓暟
    for(i=1;i<=n;i++)
     for(j=1;j<=n;j++)
      dist[i][j]=MAX;
    while(scanf("%d%d%d",&a,&b,&c),a||b||c)//0 0 0琛ㄧず杈硅緭鍏ョ粨鏉?
     dist[a][b]=dist[b][a]=c;
    s[1]=true;//璇ョ偣宸茬粡琚闂?br>    for(i=2;i<=n;i++)
    {
     cost[i]=dist[1][i];
     s[i]=false;//鍒濆鍖栦負false
    }
}
int main()
{
 freopen("s.txt","r",stdin);
 freopen("key.txt","w",stdout); 
int i,j,k,m,a,b,c,best,min;
    best=0;
 Init();
for(i=1;i<n;i++)//i涓嶈兘絳変簬n錛屽洜涓簄-1鏉¤竟
{
 min=MAX;
 j=1;
 for(k=2;k<=n;k++)
  if(cost[k]<min&&(!s[k]))//  (1)
  {
   min=cost[k];
   j=k;
  }
  s[j]=true;
  best+=min;
  for(k=2;k<=n;k++)
  {
   if(dist[j][k]<cost[k]&&(!s[k]))//鍙兘鍑虹幇宸茬粡璁塊棶榪囩殑鐐筩ost[k]淇濇寔鍘熷鹼紝浣嗚繖娌℃湁鍏崇郴錛屼互涓哄湪涓婇潰鐨勫鐞嗘楠?1)涓笉瀵硅繖浜涜竟澶勭悊
  //dist[j][j]<cost[k]鐨勬瘮杈冨垯鏄負浜嗛噸鍒ら泦鍚圴鍒癡-U闆嗗悎鐨勭偣鐨勮窛紱伙紝娉ㄦ剰鏄暣涓泦鍚圴鍒板悇涓湭綰沖叆V鐨勭偣鐨勮窛紱伙紒
   cost[k]=dist[j][k];
  }
}
 printf("%d\n",best);
 return 0;
}
瀛︿互鑷寸敤 joj 1170

luis 2009-08-09 19:52 鍙戣〃璇勮
]]>
dijkstra鐨勯偦鎺ヨ〃瀹炵幇http://www.shnenglu.com/luyulaile/archive/2009/08/09/92683.htmlluisluisSun, 09 Aug 2009 03:19:00 GMThttp://www.shnenglu.com/luyulaile/archive/2009/08/09/92683.htmlhttp://www.shnenglu.com/luyulaile/comments/92683.htmlhttp://www.shnenglu.com/luyulaile/archive/2009/08/09/92683.html#Feedback1http://www.shnenglu.com/luyulaile/comments/commentRss/92683.htmlhttp://www.shnenglu.com/luyulaile/services/trackbacks/92683.htmlusing namespace std;
//姹傚崟婧愭渶灝忚礬寰勶紝涓嶅Θ璁炬簮鐐逛負1錛岀畻娉曟濇兂鏄?
//浠庢湭璁塊棶鐨勯《鐐逛腑閫夋嫨dv鏈灝忕殑鐐癸紙d涓轟粠璇ョ偣鍒版簮鐐?鐨勮窛紱伙級浠rcs[v][v]=1
//鑰冭檻v鐨勬墍鏈夐偦鎺ラ《鐐箇錛屼笉鏂皾璇曡嫢dv+weight(v,w)<dw錛屽垯鏀瑰彉dw鍊?
//榪欓噷閫夋嫨dv鏈灝忕殑寰堝叧閿紝鍥犱負鏈灝忥紝鎵浠ヨ偗瀹氭槸鏈鐭礬寰勶紝鍏朵粬鐨刣浼氫笉鏂洿鏂般?
 #define  MaxVertx 100//鏈澶氳竟闀垮害
 #define MAX 1e+8
 typedef char vertextype;
 typedef float adjtype;
 typedef struct{
  int n;//欏剁偣涓暟
  vertextype vertex[MaxVertx];
  adjtype arcs[MaxVertx][MaxVertx];//閭繪帴鐭╅樀褰㈠紡
 }Graph;
 typedef struct{
  vertextype vertex;//欏剁偣淇℃伅
  adjtype length;//鏈灝忛暱搴?nbsp;
  int  prev;//鏈鐭礬寰勪笂鍓嶉┍緇撶偣緙栧彿
 }Path;
 Path dist[MaxVertx];
 Graph graph;
 void init(Graph *pgraph,Path dist[])
 {
  int i;
  dist[0].length=0;
  dist[0].prev=0;
  dist[0].vertex=pgraph->vertex[0];//絎竴涓《鐐圭殑鍚嶇О/緙栧彿
  pgraph->arcs[0][0]=1; /* 琛ㄧず欏剁偣v0鍦ㄩ泦鍚圲涓?*/
  
   for(i = 1; i < pgraph->n; i++)              /* 鍒濆鍖栭泦鍚圴-U涓《鐐圭殑璺濈鍊?*/
    {
    dist[i].length=pgraph->arcs[0][i];
    dist[i].vertex=pgraph->vertex[i];
    if(dist[i].length!=MAX)
         dist[i].prev=0;
    else
         dist[i].prev=-1;            
    }
 }
 void dijkstra(Graph graph,Path dist[])
 {
  int i,j,minvex;
  adjtype min;
  init(&graph,dist); /* 鍒濆鍖栵紝姝ゆ椂闆嗗悎U涓彧鏈夐《鐐箆0*/
  for(i=1;i<graph.n;i++)//姣忎釜dist[]閮借姹?涓嶄竴瀹氫細鎵цn嬈★紝鍙兘浼歜reak鍑烘潵
  {
   min=MAX;
   minvex=0;
   for(j=1;j<graph.n;j++)/*鍦╒U涓夊嚭璺濈鍊兼渶灝忛《鐐?/
   if(graph.arcs[j][j]==0&&dist[j].length<min)
   {
    min=dist[j].length;
    minvex=j;
   }
   if(minvex==0)break;/* 浠巚0娌℃湁璺緞鍙互閫氬線闆嗗悎VU涓殑欏剁偣 */
   graph.arcs[minvex][minvex]=1;//琛ㄧず宸茬粡璁塊棶榪囦簡錛屼笉灞炰簬VU闆嗗悎
   for(j=1;j<graph.n;j++)
   {
    if(graph.arcs[j][j]==1)continue;
    if(dist[j].length > dist[minvex].length + graph.arcs[minvex][j])//濡傛灉涓嶇浉閭伙紝鍒檃rcs[minvex][minvex]=+鏃犵┓
    {
                dist[j].length = dist[minvex].length + graph.arcs[minvex][j];
                dist[j].prev = minvex;
        }
   }
  }
 }
 void initgraph()//鍒濆鍖栫煩闃?
 {
 int i,j;
    graph.n=6;
    for (i = 0; i < graph.n; i++)
        for (j = 0; j < graph.n; j++)
            graph.arcs[i][j] = (i == j ? 0 : MAX);
    graph.arcs[0][1] = 50;
    graph.arcs[0][2] = 10;
    graph.arcs[1][2] = 15;
    graph.arcs[1][4] = 5;
    graph.arcs[2][0] = 20;
    graph.arcs[2][3] = 15;
    graph.arcs[3][1] = 20;
    graph.arcs[3][4] = 35;
    graph.arcs[4][3] = 30;
    graph.arcs[5][3] = 3;
    graph.arcs[0][4] = 45; 
 }
  int main()
  {
  //freopen("s.txt","r",stdin);
  //freopen("key.txt","w",stdout);
   int i;
    initgraph();
    dijkstra(graph, dist);
    for (i = 0; i < graph.n; i++)
        printf("(%.0f %d)", dist[i].length,dist[i].prev);
    getchar();
 return 0;
  }


luis 2009-08-09 11:19 鍙戣〃璇勮
]]>
joj 2035 Leaf Nodeshttp://www.shnenglu.com/luyulaile/archive/2009/07/14/90082.htmlluisluisTue, 14 Jul 2009 14:14:00 GMThttp://www.shnenglu.com/luyulaile/archive/2009/07/14/90082.htmlhttp://www.shnenglu.com/luyulaile/comments/90082.htmlhttp://www.shnenglu.com/luyulaile/archive/2009/07/14/90082.html#Feedback0http://www.shnenglu.com/luyulaile/comments/commentRss/90082.htmlhttp://www.shnenglu.com/luyulaile/services/trackbacks/90082.html
Status In/Out TIME Limit MEMORY Limit Submit Times Solved Users JUDGE TYPE
stdin/stdout 1s 10240K 222 102 Standard

Kate is learning about binary tree. She think she know everything you know about binary trees. Wait, you don't know binary tree? Find a book about data structures, and it will just take you about three minutes. Now here is a binary tree:

                                    3
/ \
/   \
2     4
/ \     \
/   \     \
0     1     6
/
/
5

Kate think she also know something that you may not notice. First, for some type of binary trees, only the leaf nodes have the meaning (leaf node is the node which has no sub trees, for the tree above, the leaf nodes are 0 1 5), an example is the Huffman Tree. Second, she guess that if you know the preorder traversal and the postorder traversal of a binary tree, you can ensure the leaf node of the tree, and their order.

For the tree above, the preorder travesal is 3 2 0 1 4 6 5 and the postorder travesal is 0 1 2 5 6 4 3, the leaf nodes in order(from left to right) are 0 1 5.

But now the problem is she just guess it, if you can find a way to print a tree's leaf nodes in order using its preorder traversal and postorder traversal, you can say "she is right!"

Input Specification

The input file will contain one or more test cases. In each test case there is a integer n (0<=n <= 10000), indicating the tree have n nodes, then followed two lists of integers, either contains n integers in the range of 0 and n-1 (both included), the first list is the preorder traversal, and the other is the postorder traversal.

Input is terminated by an interger -1;

Output Specification

For each test case, print the tree's leaf nodes in order, each in a line.

Sample Input

7
3 2 0 1 4 6 5
0 1 2 5 6 4 3
-1

Sample Output

0
1
5
鏍規嵁涓涓噸瑕佺粨璁猴紝鏃犺鏄厛鏍硅繕鏄悗鏍歸亶鍘嗭紝宸﹀瓙鏍戠殑緇撶偣鎬繪槸鍑虹幇鍦ㄥ彸瀛愭爲緇撶偣鐨勫墠闈?br>
                                 G  
                                /   \  
                              F        B  
                            /        /   \  
                          K         C      H  
                        /   \             /        
                      D       E         J  
                        \  
                          A  
                        /  
                      I  
   
  涓嶈鍏堟牴鍚庢牴錛屽乏瀛愭爲鐨勭粨鐐規繪槸鍑虹幇鍦ㄥ彸瀛愭爲緇撶偣鐨勫墠闈€?nbsp; 
  G涓烘牴鏍戯紝鍏堟牴嬈″簭鏃禛鍚庤窡F錛屽悗鏍規搴忔椂F鍓嶆湁DIAEKF錛屾晠DIAEKF涓篏鐨勫乏瀛愭爲鐨勭粨鐐癸紝
  CJHB涓篏鐨勫彸瀛愭爲鐨勭粨鐐廣備笖宸﹀彸瀛愭爲鐨勫厛鏍瑰簭涓猴細FKDIAE錛孊CHJ銆?br>  閫掑綊澶勭悊涓ゅ瓙鏍戝嵆鍙悶瀹?br>
void  find(int preb,int pree,int postb,int poste) 
   {
   int i=s(pre,post[poste-1]);
  int j=s(post,pre[preb+1]);
//娣誨姞澶勭悊鐨勪唬鐮?br> //鍒ゆ柇鏄惁鏈夊乏/鍙蟲敮

    find(preb+1,i-1,postb,j);
  find(i,pree,j+1,poste-1);
   }
浣嗘槸涓婇潰鐨勬濊礬鏄?strong>閿欒鐨勶紒錛侊紒錛侊紒錛侊紒錛侊紒錛侊紒錛侊紒錛?/span>
鍙煡閬撳厛搴忓拰鍚庡簭涓嶈兘鑳芥帹鍑烘爲鏉? 
   
  鍙湁涓簭鍜屽厛搴忔垨鑰呬腑搴忓拰鍚庡簭鎵嶅彲浠? 
   
  涓嶇劧鍙煡閬撴牴鑺傜偣,浣嗘槸鍝簺鏄乏瀛愭爲鍝簺鏄彸瀛愭爲灝變笉鐭ラ亾浜?br>姣斿鍏堝簭鏃?234  
  鍚庡簭鏄?321鐨勪簩鍙夋爲鏈?縐嶆瘮濡傦細  
      1                     1              
      \                   /  
        2               2  
    /                 /  
  3                 3  
    \             /  
      4         4

姝g‘鎬濊礬錛氬厛閬嶅巻鍚庢牴嬈″簭錛岀涓涓竴瀹氫負鍙跺瓙錛岃涓哄綋鍓嶇粨鐐癸紝鐒跺悗渚濇媯嫻嬶紝濡傛灉璇ョ偣鍦ㄥ厛鏍瑰簭鍒椾腑浣嶄簬褰撳墠鑺傜偣鐨勫悗闈紝鍒欎負鍙跺瓙鑺傜偣錛屽悓鏃舵洿鏂板綋鍓嶇粨鐐廣?/div>

luis 2009-07-14 22:14 鍙戣〃璇勮
]]> 精品国产VA久久久久久久冰| 久久av高潮av无码av喷吹| 国产毛片欧美毛片久久久| 久久久久久久久久久| 国产一级持黄大片99久久| 99久久精品国产一区二区| 区久久AAA片69亚洲| AV狠狠色丁香婷婷综合久久| 91麻豆精品国产91久久久久久| 久久综合五月丁香久久激情| 伊人色综合久久天天人手人婷| 欧美伊香蕉久久综合类网站| 久久午夜夜伦鲁鲁片免费无码影视 | 久久人妻少妇嫩草AV无码蜜桃| 久久这里只有精品首页| 久久免费国产精品一区二区| 精品国产乱码久久久久软件| 国产激情久久久久影院小草| 久久ZYZ资源站无码中文动漫| 99久久婷婷国产一区二区| 久久精品国产亚洲77777| 中文精品久久久久人妻| 精品久久久久中文字幕一区| 国产婷婷成人久久Av免费高清 | 久久亚洲中文字幕精品有坂深雪| 久久无码中文字幕东京热| 久久亚洲精品国产精品| 久久久久亚洲AV成人网人人网站 | 精品乱码久久久久久夜夜嗨| 国产国产成人精品久久| 久久丫精品国产亚洲av| 色婷婷噜噜久久国产精品12p| 一级做a爰片久久毛片人呢| 国产91色综合久久免费| 精品蜜臀久久久久99网站| 久久成人国产精品| 2021久久精品国产99国产精品| 无码AV波多野结衣久久| 久久久久久国产精品无码超碰| 亚洲国产精品无码久久一区二区| 欧美日韩久久中文字幕|