每日一题 UVA - 1599 Ideal Path 字典序最短路
Posted suut
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每日一题 UVA - 1599 Ideal Path 字典序最短路相关的知识,希望对你有一定的参考价值。
题解:给一个1e5个点2e5条边,每个边有一个值,让你输出一条从1到n边的路径使得:条数最短的前提下字典序最小。
题解:bfs一次找最短路(因为权值都是1,不用dijkstra),再bfs一次存一下路径,4个月前的代码(忘了为什么搞得那么麻烦),wa了两天,今天看了一下题目,看了一下代码,改了一下初始化数组,直接过了orz
#define _CRT_SECURE_NO_WARNINGS #include<cstring> #include<cctype> #include<cmath> #include<cstdio> #include<string> #include<stack> #include<list> #include<set> #include<map> #include<queue> #include<vector> #include<sstream> #include<iostream> #include<algorithm> //std::ios::sync_with_stdio(false); using namespace std; const int maxn = 2e5 + 5; vector<pair<int, int> >E[maxn]; int d[maxn]; int n, m; void bfs(int n) { queue<int> Q; Q.push(n); d[n] = 1; while (!Q.empty()) { int now = Q.front(); Q.pop(); for (int i = 0; i < E[now].size(); i++) { int v = E[now][i].first; if (d[v])continue; d[v] = d[now] + 1; Q.push(v); } } } int ans[maxn]; int vis[maxn]; void bfs1() { for (int i = 0; i <= n; i++)ans[i] = 1e9+5; queue<int>Q; Q.push(1); while (!Q.empty()) { int now = Q.front(); Q.pop(); if (vis[now])continue; vis[now] = 1; int mn = 1e9 + 5; for (int i = 0; i < E[now].size(); i++) { int v = E[now][i].first; if (d[v] != d[now] - 1)continue; int w = E[now][i].second; mn = min(mn, w); } int diff = d[1] - d[now]; ans[diff] = min(ans[diff], mn); for (int i = 0; i < E[now].size(); i++) { int v = E[now][i].first; if (d[v] != d[now] - 1)continue; int w = E[now][i].second; if (w == mn) Q.push(v); } } } void init() { memset(ans, 0, sizeof(ans)); memset(vis, 0, sizeof(vis)); memset(d, 0, sizeof(d)); for (int i = 0; i <= n; i++)E[i].clear(); } int main() { while (cin >> n >> m) { init(); for (int i = 1; i <= m; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); E[x].push_back(make_pair(y, z)); E[y].push_back(make_pair(x, z)); } bfs(n); bfs1(); cout << d[1] - 1 << endl; for (int i = 0; i < d[1] - 1; i++)printf("%d%c", ans[i], i + 1 == d[1] - 1 ? ‘ ‘ : ‘ ‘);// << ‘ ‘;| } //system("pause"); return 0; } /* 4 6 1 2 1 1 3 2 3 4 3 2 3 1 2 4 4 3 1 1 */
以上是关于每日一题 UVA - 1599 Ideal Path 字典序最短路的主要内容,如果未能解决你的问题,请参考以下文章