2. Obviously, the col value of each step should be selected greedily. In order to ensure that each step is still the shortest path when greedily selecting, we should make a bfs from the end point to dis at each point.
3. The process of getting the minimum value is still the process of bfs, but we need to access the next layer of BFS from multiple points, which is implemented by vector, and the number of access layers is the shortest distance.
#include <iostream> #include <string> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <algorithm> #include <functional> #include <utility> #include <cstring> #include <cstdio> #include <cstdlib> #include <ctime> #include <cmath> #include <cctype> #define CLEAR(a, b) memset(a, b, sizeof(a)) #define IN() freopen("in.txt", "r", stdin) #define OUT() freopen("out.txt", "w", stdout) #define LL long long #define maxn 100005 #define maxm 200005 #define mod 1000000007 #define INF 1000000007 #define eps 1e-5 #define PI 3.1415926535898 #define N 26 using namespace std; //-------------------------CHC------------------------------// struct Edge { int to, col; Edge(int to = 0, int col = 0) : to(to), col(col) { } }; vector<Edge> edges[maxn]; int d[maxn]; bool vis[maxn]; vector<int> col; void get_dis(int n) { CLEAR(vis, 0); queue<int> q; q.push(n); vis[n] = true; d[n] = 0; while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < edges[u].size(); ++i) { int v = edges[u][i].to; if (!vis[v]) { vis[v] = true; d[v] = d[u] + 1; q.push(v); } } } } void bfs(int n) { CLEAR(vis, 0); col.clear(); vector<int> s; s.push_back(1); vis[1] = true; for (int i = 0; i < d[1]; ++i) { int mincol = INF; for (int j = 0; j < s.size(); ++j) { //find the minimum col int u = s[j]; for (int k = 0; k < edges[u].size(); ++k) { int v = edges[u][k].to, c = edges[u][k].col; if (!vis[v] && d[v] == d[u] - 1) mincol = min(mincol, c); } } col.push_back(mincol); vector<int> temp; for (int j = 0; j < s.size(); ++j) { //find out the next vertices of the next phase int u = s[j]; for (int k = 0; k < edges[u].size(); ++k) { int v = edges[u][k].to, c = edges[u][k].col; if (!vis[v] && d[v] == d[u] - 1 && c == mincol) { temp.push_back(v); vis[v] = true; } } } s = temp; } } int main() { int n, m; while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= n; ++i) edges[i].clear(); CLEAR(d, -1); int u, v, w; for (int i = 0; i < m; ++i) { scanf("%d%d%d", &u, &v, &w); edges[u].push_back(Edge(v, w)); edges[v].push_back(Edge(u, w)); } get_dis(n); bfs(n); printf("%d\n", d[1]); bool first = true; for (int i = 0; i < col.size(); ++i) { if (first) first = false; else putchar(' '); printf("%d", col[i]); } putchar('\n'); } return 0; }