码迷,mamicode.com
首页 > 其他好文 > 详细

邻接矩阵存储简单路径

时间:2015-06-16 16:07:00      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

Description

     假设无向图G采用邻接矩阵存储,设计一个算法,输出图G中从顶点u到v的所有简单路径。

Input

    简单路径是指路径上的顶点不重复。第一行为一个整数n,表示顶点的个数(顶点编号为0到n-1),第二行表示顶点u和v的编号,接下来是为一个n*n大小的矩阵,表示图的邻接关系。数字为0表示不邻接,1表示不邻接。

Output

     输出图G中从顶点u到v的所有简单路径。

 

Sample Input
 
1
2
3
4
5
6
7
5
0 3
0 1 0 1 1
1 0 1 1 0
0 1 0 1 1
1 1 1 0 1
1 0 1 1 0
Sample Output
1
2
3
4
5
6
7
8
0123
01243
013
03
04213
0423
043
 
 
技术分享
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct
{
    int edge[100][100];
}MGraph;
MGraph g;
int  i, j, n, vis[31],path[31];
char V[31];
void bfs(int x,int y,int cnt)
{
    int i;
    vis[x] = 1;
    path[cnt] = x;
    if (x == y)
    {
        for (i = 0; i <= cnt; i++)
            cout << path[i];
        cout << endl;
    }
    for (i = 0; i < n; i++)
    {
        if (g.edge[x][i] && !vis[i])
        {
            //vis[i] = 1;
            bfs(i, y, cnt + 1);
        }
    }
    vis[x] = 0;
}
int main()
{
    int x, y;
    cin >> n >> x >> y;
    for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
        cin >> g.edge[i][j];
    bfs(x,y,0);
    return 0;
}
View Code

 

 
 
 
 
 
 

邻接矩阵存储简单路径

标签:

原文地址:http://www.cnblogs.com/traini13/p/4580787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!