标签:
Description
Input
Output
Sample Input
input | output |
---|---|
7 7 1 2 2 4 2 5 3 4 4 6 5 6 6 7 1 7 3 |
2 |
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> Node; #define x first #define y second const int N = 111111; vector<int> nx[N]; int dist[N], n, m; Node answer[N]; void BFS(const int x) { queue<int> q; memset(dist, 0x11, sizeof dist); dist[x] = 0; q.push(x); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < nx[u].size(); ++i) { int v = nx[u][i]; if (dist[v] > dist[u] + 1) { dist[v] = dist[u] + 1; q.push(v); } } } } int BFS(int x, int y) { queue<int> q; for (int i = 0; i < N; ++i) { answer[i].x = INT_MAX; answer[i].y = 0; } answer[x].x = 0; answer[x].y = -dist[x]; q.push(x); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < nx[u].size(); ++i) { int v = nx[u][i]; if (answer[v] > Node(answer[u].x + 1, max(answer[u].y, -dist[v]))) { answer[v] = Node(answer[u].x + 1, max(answer[u].y, -dist[v])); q.push(v); } } } //cout << answer[y].x << ‘ ‘ << answer[y].y << endl; return -answer[y].y; } int Run() { int x, y, z; int a, b; while (cin >> n >> m) { for (int i = 1; i <= n; ++i) { nx[i].clear(); } for (int i = 0; i < m; ++i) { cin >> a >> b; nx[a].push_back(b); nx[b].push_back(a); } cin >> x >> y >> z; BFS(z); cout << BFS(x, y) << endl; } return 0; } int main() { ios::sync_with_stdio(0); return Run(); }
标签:
原文地址:http://www.cnblogs.com/BugClearlove/p/4412959.html