hdu-2485-bfs+dfs
来源:互联网 发布:淘宝浴衣与羽织的区别 编辑:程序博客网 时间:2024/05/29 13:10
#include<stdio.h>#include<string.h>#include<queue>#include<vector>using namespace std;const int INF = 0x3f3f3f3f;struct Node{ int id; int step; Node(){} Node(int a,int b):id(a),step(b){}};int n,m,k;int ans;int pre[55];int G[55][55];int vis[55];int del[55];int bfs(int s){ memset(vis,0,sizeof(vis)); memset(pre,0,sizeof(pre)); queue<Node> q; q.push(Node(s,0)); while(!q.empty()){ Node ct = q.front(); q.pop(); if(ct.id == n){ return ct.step; } for(int i = 1;i <= n;i++){ if(!del[i] && !vis[i] && G[ct.id][i]){ vis[i] = 1; pre[i] = ct.id; q.push(Node(i,ct.step+1)); } } } return -1;}void dfs(int num){ if(num >= ans) return; int now = bfs(1); if(now == -1 || now > k){ ans = num < ans ? num : ans; return; } int x = pre[n]; int road[55];//注意这里需要提前记录改路径上的点,因为当你深搜下去之后pre数组就变了 int cnt = 0; while(x != 1){ road[cnt++] = x; x = pre[x]; } for(int i = 0;i < cnt;i++){ del[road[i]] = 1; dfs(num+1); del[road[i]] = 0; }}int main(){ while(scanf("%d%d%d",&n,&m,&k) && (n && m && k)){ memset(G,0,sizeof(G)); memset(del,0,sizeof(del)); ans = INF; for(int i = 0;i < m;i++){ int u,v; scanf("%d%d",&u,&v); G[u][v] = 1; } dfs(0); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- hdu-2485-bfs+dfs
- HDU 1983 BFS + DFS
- HDU 1044 BFS + DFS
- hdu (4414)(BFS+DFS)
- hdu 1044 bfs dfs
- hdu 1044 DFS + BFS
- hdu 4101 DFS+BFS
- HDU 1983 DFS+BFS
- HDU 1044 BFS+DFS
- HDU 1044(bfs+dfs)
- hdu 1983(bfs+dfs)
- hdu 1044 (bfs+dfs)
- HDU 1983 BFS&&DFS
- HDU 1044 bfs+dfs
- hdu 1074 dfs or bfs
- hdu 1241 bfs or dfs
- [HDU 5004 KAMI] dfs+bfs
- BFS+DFS(hdu 1044)
- 指针运算
- CHAPTER 11-Training Deep Neural Nets-part2
- 腾讯荣获《哈佛商业评论》拉姆·查兰管理实践奖
- C Primer Plus 第五版11.13编程题参考答案
- C++读入优化和输出优化
- hdu-2485-bfs+dfs
- 深度学习炼丹师的养成之路之——Batch size/Epoch/Learning Rate的设置和学习策略
- Windows下将现有的项目上传到GitHub上
- Unity技术支持团队性能优化经验分享
- Windows下安装express遇到的问题及解决办法
- 充电是电动车革命的大难题 看看欧洲有什么高招? | 行业
- 机器学习领域顶级会议
- Java面向对象(OOP)
- 对话Jeff Dean:未来不可预测 实现AGI的时间是未来5到500年 | AI英雄