Metal Steak

Hard to eat

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 79 Stories :: 0 Comments :: 0 Trackbacks

公告

aaaaaaaaaaaa

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

#include <iostream>
using namespace std;

int    n, m, x, y;
int    map[101][101];
int    prev[101];
int    vst[101];
int    f = 1, r;

struct
node
{
    
int    c;
    
void
    add( node
* q )
    {
        q[
++r] = *this;
        prev[r] 
= f;
    }
    
bool
    
operator == ( node x )
    {
        
if( x.c == this->c )
            
return true;
        
return false;
    }
}que[
10001];

void
__read__()
{
    cin 
>> n >> m >> x >> y;
    
forint i = 1; i <= m; i++ )
    {
        
int p, q;
        cin 
>> p >> q;
        map[p][q] 
=
        map[q][p] 
= true;
    }
}

void
__outp__( 
int t )
{
    
if( que[t].c != x )
        __outp__( prev[t] );
    
if( que[t].c == x )
        cout 
<< que[t].c;
    
else
        cout 
<< "->" << que[t].c;
}

void
__bfs__()
{
    node start;
    start.c 
= x;
    start.add( que );
    vst[x] 
= true;
    
if( map[x][y] )
    {
        cout 
<< x << "->" << y << endl;
        exit( 
0 );
    }
    
while1 )
    {
        
forint i = 1; i <= n; i++ )
            
if!vst[i] && map[que[f].c][i] )
            {
                vst[i] 
= true;
                node waypoint;
                waypoint.c 
= i;
                waypoint.add( que );
            }
        f
++;
        
if( que[r].c == y )
        {
            __outp__( r ); cout 
<< endl;
            exit( 
0 );
        }
        
if( f == r + 1 )
        {
            cout 
<< "no solution" << endl;
            exit( 
0 );
        }
    }
}

int
main()
{
    __read__();
    __bfs__();

    
return 0;
}

posted on 2009-09-16 18:48 mad4alcohol 阅读(191) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理