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

UVA 208 (DFS)

时间:2015-05-21 22:09:35      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

题意:找出1到T的所有路径;

坑点:一开始以为是到终点,读错了题意,没测试第二个样例,结果WA了4遍,坑大了;

技术分享
 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <sstream>
 7 #include <algorithm>
 8 #define Max 2147483647
 9 #define INF 0x7fffffff
10 #define N 90000+2
11 #define M 40000
12 #define ll long long
13 #define mem(a,b) memset(a,b,sizeof(a))
14 #define repu(i, a, b) for(int i = (a); i < (b); i++)
15 #define repd(i, a, b) for(int i = (a-1); i >= (b); i--)
16 const double PI=-acos(-1.0);
17 using namespace std;
18 const int maxn = 999999;
19 int t = 0, v, sum, T;
20 int rec[30],vis[30];
21 int g[30][30] ,floyd[30][30];
22 void dfs(int x,int n)
23 {
24     if(x==T)///到终点输出
25     {
26         printf("%d" ,1);
27         repu(i,1,n-1)
28         printf(" %d",rec[i]);
29         printf(" %d\n",T);
30         sum++;
31         return ;
32     }
33     repu(i,1,v+1)
34     {
35         if(!vis[i]&&g[x][i]==1&&floyd[T][i]!=maxn)
36         {///i点能到达终点+x,i通路+未访问过
37             rec[n]=i;
38             vis[i]=1;
39             dfs(i,n+1);
40             vis[i]=0;
41         }
42     }
43 }
44 int main()
45 {
46     int x, y, cas = 1;
47     while (~scanf("%d", &T))
48     {
49         v = 0;
50         repu(i,1,22)
51         repu(j,1,22)
52         g[i][j] = floyd[i][j] = maxn;
53         while(scanf("%d%d", &x, &y) && (x || y))
54         {
55             g[x][y] = g[y][x] = 1;///无向图
56             floyd[x][y] = floyd[y][x] = 1;
57             v = max(v,max(x,y));///找最大点
58         }
59         for(int k = 1; k <= v; k++)///弗洛伊德算法找最短路,判断两点之间是否是通路
60             for (int i = 1; i <= v; i++)
61                 for (int j = 1; j <= v; j++)
62                     floyd[i][j] = min(floyd[i][j],floyd[i][k] + floyd[k][j]);
63         vis[1] = 1;
64         sum = 0;
65         printf("CASE %d:\n", cas++);
66         dfs(1, 1);///从第一组数找起
67         printf("There are %d routes from the firestation to streetcorner %d.\n", sum, T);
68     }
69     return 0;
70 }
DFS

 

UVA 208 (DFS)

标签:

原文地址:http://www.cnblogs.com/ACMERY/p/4520598.html

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