搜索所有路径

Posted Jason-Cow

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搜索所有路径相关的知识,希望对你有一定的参考价值。

 

 

 

  1 #include <cmath>
  2 #include <queue>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <iostream>
  7 #include <algorithm>
  8 using namespace std;
  9 struct edge {
 10     int next,v,w;
 11     edge(int next=0,int v=0,int w=0):next(next),v(v),w(w) {}
 12 };
 13 const int N=30,M=105;
 14 int head[N],cnt;
 15 edge e[M];
 16 
 17 void add(int u,int v,int w) {
 18     e[++cnt]=edge(head[u],v,w),head[u]=cnt;
 19     //e[++cnt]=edge(head[v],u,w),head[v]=cnt;
 20 }
 21 
 22 void init_G() {
 23     add(1,2,11);
 24 
 25     add(2,1,11);
 26     add(2,3,3);
 27     add(2,4,6);
 28 
 29     add(3,2,3);
 30     add(3,6,5);
 31     add(3,7,3);
 32     add(3,9,3);
 33     add(3,11,3);
 34 
 35     add(4,2,6);
 36     add(4,5,1);
 37     add(4,6,2);
 38 
 39     add(5,4,1);
 40     add(5,18,2);
 41     add(5,16,3);
 42 
 43     add(6,3,5);
 44     add(6,4,2);
 45     add(6,7,1);
 46     add(6,17,2);
 47 
 48     add(7,3,3);
 49     add(7,6,1);
 50     add(7,8,1);
 51     add(7,16,2);
 52 
 53     add(8,7,1);
 54     add(8,9,2);
 55     add(8,14,2);
 56     add(8,15,1);
 57 
 58     add(9,3,3);
 59     add(9,8,2);
 60     add(9,10,1);
 61     add(9,14,1);
 62 
 63     add(10,9,1);
 64     add(10,11,3);
 65     add(10,12,2);
 66     add(10,13,1);
 67 
 68     add(11,3,3);
 69     add(11,10,3);
 70     add(11,12,2);
 71 
 72     add(12,10,2);
 73     add(12,11,2);
 74     add(12,28,2);
 75 
 76     add(13,10,1);
 77     add(13,14,1);
 78     add(13,26,1);
 79 
 80     add(14,8,2);
 81     add(14,9,1);
 82     add(14,13,1);
 83     add(14,23,2);
 84     add(14,24,1);
 85     add(14,26,2);
 86 
 87     add(15,8,1);
 88     add(15,16,1);
 89     add(15,24,2);
 90 
 91     add(16,5,3);
 92     add(16,7,1);
 93     add(16,15,1);
 94     add(16,21,2);
 95 
 96     add(17,6,2);
 97     add(17,18,1);
 98     add(17,19,1);
 99     add(17,21,1);
100 
101     add(18,5,2);
102     add(18,17,1);
103     add(18,19,1);
104 
105     add(19,17,1);
106     add(19,18,1);
107     add(19,20,1);
108 
109     add(20,19,1);
110     add(20,21,1);
111     add(20,22,1);
112 
113     add(21,16,2);
114     add(21,17,1);
115     add(21,20,1);
116     add(21,23,1);
117 
118     add(22,20,1);
119     add(22,23,1);
120     add(22,25,1);
121 
122     add(23,14,2);
123     add(23,21,1);
124     add(23,22,1);
125     add(23,24,1);
126 
127     add(24,14,1);
128     add(24,15,1);
129     add(24,23,1);
130     add(24,25,1);
131     add(24,26,1);
132 
133     add(25,22,1);
134     add(25,24,1);
135     add(25,27,1);
136 
137     add(26,13,1);
138     add(26,14,2);
139     add(26,24,1);
140     add(26,28,1);
141 
142     add(27,25,3);
143     add(27,28,2);
144 
145     add(28,12,2);
146     add(28,26,1);
147     add(28,27,2);
148 }
149 
150 struct node {
151     int dis,u;
152     bool operator<(const node NEXT)const {
153         return dis<NEXT.dis;
154     }
155     node(int dis=0,int u=0):dis(dis),u(u) {}
156 };
157 
158 int d[N];void init_d(int n) {for(int i=1; i<=n; i++)d[i]=1e9;}
159 
160 int dijkstra(int s,int t) {
161     init_d(t);
162     priority_queue<node>q;
163     d[s]=0,q.push(node(0,s));
164     for(int u,v; !q.empty();) {
165         u=q.top().u,q.pop();
166         for(int i=head[u]; i; i=e[i].next) {
167             v=e[i].v;
168             if(d[v]>d[u]+e[i].w) {
169                 d[v]=d[u]+e[i].w;
170                 q.push(node(d[v],v));
171             }
172         }
173 
174     }
175     return d[t];
176 }
177 
178 int vis[N],que[N],tot;
179 
180 void dfs(int u,int sum,int dep) {
181     if(u==28 && sum==0) {
182         for(int i=1;i<=dep;i++) printf("%d ",que[i]);puts("");
183         return;
184     }
185     for(int i=head[u]; i; i=e[i].next) {
186         int v=e[i].v,w=e[i].w;
187         if(!vis[v]) {
188             vis[v]=1;
189             que[dep+1]=v;
190             dfs(v,sum-w,dep+1);
191             que[dep+1]=0;
192             vis[v]=0;
193         }
194     }
195 }
196 
197 int main() {
198     freopen("allResult.txt","w",stdout);
199     
200     init_G();
201     int MIN=dijkstra(1,28);
202     
203     int sum_all_edge=0;
204     for(int i=1;i<=cnt;i++)sum_all_edge+=e[i].w;
205     int MAX=sum_all_edge/2;
206     
207     
208     for(int i=MIN;i<=MAX;i++){
209         printf("For passing %d stations\\n",i);
210         vis[1]=1;
211         que[1]=1;
212         dfs(1,i,1);
213         puts("");
214     }    
215     return 0;
216 }

 

以上是关于搜索所有路径的主要内容,如果未能解决你的问题,请参考以下文章

从搜索文档中查找最小片段的算法?

怎么用cmd搜索出指定后缀的文件路径并将所有路径返回到一个文本中?

iOS xcode 代码片段

如何在 BottomNavigationView 的片段上打开搜索界面?

C++ 代码片段(积累)

片段中的Android webView显示空白页面