邻接矩阵存储简单路径
2015-06-16 14:47
246 查看
Description
假设无向图G采用邻接矩阵存储,设计一个算法,输出图G中从顶点u到v的所有简单路径。
Input
简单路径是指路径上的顶点不重复。第一行为一个整数n,表示顶点的个数(顶点编号为0到n-1),第二行表示顶点u和v的编号,接下来是为一个n*n大小的矩阵,表示图的邻接关系。数字为0表示不邻接,1表示不邻接。
Output
输出图G中从顶点u到v的所有简单路径。
Sample Input
Sample Output
View Code
假设无向图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 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
相关文章推荐
- 陈怡暖:美元及股指走跌,金银受益小幅收涨
- JPA实体继承实体的映射策略
- project.pbxproj,最熟悉的”陌生人”
- WinSock三种选择I/O模型
- Vmware vSphere 5.0实战教程之一 vSphere高级应用测试环境搭建
- ExtJs中多个form情况下指定某个form使能
- 【LeetCode 207】Course Schedule
- ExtJs中多个form情况下指定某个form使能
- Http中的Get与Post
- 自己的五子棋版本1.1
- Mac OS X Yosemite安装Hadoop 2.6记录
- Android文件图片上传的详细讲解(四)---服务端编码
- IOS开发之深拷贝与浅拷贝(mutableCopy与Copy)详解
- 解决两个div之间出现空白条问题
- malloc()与 alloc()区别
- 天气数据API接口记录
- NV12转化为BMP函数
- 图像目标区域质心计算
- java.lang.IllegalAccessError: tried to access method oracle.jdbc.pool.OracleDataSource.getPassword()
- 乱七八糟的笔记