Open yooocen opened 6 years ago
这题有一点难度,首先要想到既然是图就可以用深度搜索,至于是最少的站的情况下还要最少的中转站就需要维护两个变量,记得visit这个标志,用完以后要恢复
#include <cstdio> #include <vector> using namespace std; vector<vector<int>> v(10000); int line[10000][10000], visit[10000]; vector<int> path, tempPath; int transferCnt(vector<int> a) { int cnt = -1, preLine = 0; for (int i = 1; i < a.size(); i++) { if (line[a[i-1]][a[i]] != preLine) cnt++; preLine = line[a[i-1]][a[i]]; } return cnt; } void dfs(int node, int end, int cnt, int &minCnt, int &minTransfer) { if (node == end && (cnt < minCnt || (cnt == minCnt && transferCnt(tempPath) < minTransfer))) { minCnt = cnt; minTransfer = transferCnt(tempPath); path = tempPath; } if (node == end) return; for (int i = 0; i < v[node].size(); i++) { if (visit[v[node][i]] == 0) { visit[v[node][i]] = 1; tempPath.push_back(v[node][i]); dfs(v[node][i], end, cnt + 1, minCnt, minTransfer); visit[v[node][i]] = 0; tempPath.pop_back(); } } } int main() { int n, m, k, pre, temp, a, b; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &m, &pre); for (int j = 1; j < m; j++) { scanf("%d", &temp); v[pre].push_back(temp); v[temp].push_back(pre); line[pre][temp] = line[temp][pre] = i + 1; pre = temp; } } scanf("%d", &k); for (int i = 0; i < k; i++) { scanf("%d%d", &a, &b); int minCnt = 99999, minTransfer = 99999; tempPath.clear(); tempPath.push_back(a); visit[a] = 1; dfs(a, b, 0, minCnt, minTransfer); visit[a] = 0; printf("%d\n", minCnt); int preLine = 0, preTransfer = a; for (int j = 1; j < path.size(); j++) { if (line[path[j-1]][path[j]] != preLine) { if (preLine != 0) printf("Take Line#%d from %04d to %04d.\n", preLine, preTransfer, path[j-1]); preLine = line[path[j-1]][path[j]]; preTransfer = path[j-1]; } } printf("Take Line#%d from %04d to %04d.\n", preLine, preTransfer, b); } return 0; }
这题有一点难度,首先要想到既然是图就可以用深度搜索,至于是最少的站的情况下还要最少的中转站就需要维护两个变量,记得visit这个标志,用完以后要恢复