漂洋过海来看你 【dfs or 最短路】
来源:互联网 发布:有钱人的三观 知乎 编辑:程序博客网 时间:2024/05/01 17:11
漂洋过海来看你
Description
BMan和hyx住在一个神奇的国度,这个国度有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来。 现在BMan在第S号城市,他经常想起那个一起AC过的队友hyx,记忆它总是慢慢的累积,在他心中无法抹去,可是他并不知道hyx 在哪个城市,所以他决定用尽半年的积蓄漂洋过海去找hyx,现在BMan很想知道如果他想去hyx所在的第X号城市,必须经过的前 一个城市是第几号城市(放心,由于心系队友,BMan是不会选择走重复的路的~)
Input
第一行输入一个整数T表示测试数据共有T(1<=T<=10)组 每组测试数据的第一行输入一个正整数N(1<=N<=1000)和一个正整数S(1<=S<=1000),N表示城市的总数,S是BMan所在城市的编号 随后的N-1行,每行有两个正整数a,b(1<=a,b<=N),表示第a号城市和第b号城市之间有一条路连通。
Output
每组测试数据输N个正整数,其中,第i个数表示从S走到i号城市,必须要经过的上一个城市的编号(其中i=S时,请输出-1)
Sample Input
1
10 1
1 9
1 8
8 10
10 3
8 6
1 2
10 4
9 5
3 7
Sample Output
-1 1 10 10 9 8 3 1 1 8
最短路或dfs均可做
最短路代码:
#include <stdio.h> #include <iostream> #include <math.h> #include <stdlib.h> #include <ctype.h> #include <algorithm> #include <vector> #include <string.h> #include <queue> #include <stack> #include <set> #include <sstream> #include <time.h> #include <utility> #include <malloc.h> #include <stdexcept> #include <iomanip> #include <iterator> using namespace std;const int MAXV = 4010;const int inf = 10000000;int map[MAXV][MAXV];int d[MAXV];bool vis[MAXV];int n, m;int pre[MAXV];void dijkstra(int s){ for (int i = 1; i <= n; i++) { vis[i] = 0; d[i] = map[s][i]; } while (1) { int min = inf, v = -1; for (int i = 1; i <= n; i++) if (!vis[i] && d[i]<min) { v = i; min = d[i]; } if (v == -1) break; vis[v] = 1; for (int i = 1; i <= n; i++) if (!vis[i] && d[i] >= d[v] + map[v][i]) { d[i] = map[v][i] + d[v]; pre[i] = v; } }}int t, a, b;int main(){ scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (i == j) map[i][i] = 0; else map[i][j] = map[j][i] = inf; } memset(pre, 0, sizeof(pre)); pre[m] = -1; for (int i = 1; i < n; i++) { scanf("%d%d", &a, &b); map[a][b] = map[b][a] = 1; } dijkstra(m); for (int i = 1; i < n; i++) printf("%d ", pre[i]); printf("%d\n", pre[n]); } return 0;}
dfs代码:
#include <stdio.h> #include <iostream> #include <math.h> #include <stdlib.h> #include <ctype.h> #include <algorithm> #include <vector> #include <string.h> #include <queue> #include <stack> #include <set> #include <sstream> #include <time.h> #include <utility> #include <malloc.h> #include <stdexcept> #include <iomanip> #include <iterator> using namespace std;const int MAXV = 4010;const int inf = 10000000;int map[MAXV][MAXV];int d[MAXV];bool vis[MAXV];int n, m;int pre[MAXV];int t, a, b;void dfs(int s){ vis[s] = 1; for (int i = 1; i <= n; i++) { if (!vis[i] && map[s][i] == 1) { pre[i] = s; vis[i] = 1; dfs(i); } }}int main(){ scanf("%d",&t); while (t--) { memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&m); memset(map,0,sizeof(map)); memset(pre,0,sizeof(pre)); pre[m] = -1; for (int i = 1; i < n; i++) { scanf("%d%d",&a,&b); map[a][b] = map[b][a] = 1; } dfs(m); for (int i = 1; i < n; i++) printf("%d ",pre[i]); printf("%d\n",pre[n]); } return 0;}
0 0
- 漂洋过海来看你 【dfs or 最短路】
- zzulioj--1711--漂洋过海来看你(dfs+vector)
- 漂洋过海来看你
- 漂洋过海来看你 老歌
- 【dfs or 最短路】【HDU1224】【Free DIY Tour】
- 2015广工网络赛初赛-Problem G: 漂洋过海来看你-迪杰斯特拉
- hdu1142(最短路+DFS)
- 1142 hdu (最短路 + DFS)
- uva1600(最短路 bfs & dfs)
- 1018 最短路 Dijkstra+DFS
- hdu 3411 (DFS+最短路)
- poj 1724 最短路变形 DFS+剪枝
- HDU 1242 dFS 找目标最短路
- hdu 1572(dfs+最短路)
- poj 3009 dfs暴力解决最短路
- CodeForces 522A(DFS||floyd最短路)
- poj 3083 dfs+bfs最短路
- hdu 3768 Shopping(DFS+最短路)
- android---surfaceview小结--(1)
- 三月随笔
- iOS UICollectionView 入门 06 添加标题view
- 1.2 系统平台——CentOS 6
- 【HDU 3642 求长方体的体积并
- 漂洋过海来看你 【dfs or 最短路】
- POJ3150---Cellular Automaton(矩阵)
- JAVA IO (一) 基础深入理解
- ios开发-OC-Fondation框架的学习
- MongoDB Sharding分片管理
- list.size()>0 && !list.isEmpty() 性能比较疑问
- 1.3 Bochs——一个可调试的虚拟机
- 接口和抽象类的区别
- ubutun server配置图形桌面