样例: |
| 0 2 3 1 -1 -1 |

根据题意,数据范围也小,也可以用朴素版的Dijsktra来做,朴素版的Dijsktra我做过了一遍了,可以看以一下我之前写的。
这次用堆优化,有时候数据范围大那么一点点的时候比如数据范围是![]()
的时候,最坏情况下,朴素版的Dijsktra的时间复杂度是(1.5 * 10^5)^2,就会超时。
如果我们通过提前排序知道哪个路径是最短路的点,即去掉一层循环,时间复杂度就是1.5 * 10^5,这样不会超时,就需要用到 堆来排序我们每个点最短距离,并且该点如果到达过,就寻找下一个最短路径的,由于数据范围较大,用不了了邻接矩阵的方式,我们只能用邻接表来实现了。
- #include
- #include
- #include
- #include
- #include
- #define endl '\n'
- #define mk make_pair
- #define x first
- #define y second
- #define int long long
- #define YES puts("YES")
- #define NO puts("NO")
- #define umap unordered_map
- #define INF 0x3f3f3f3f3f3f3f
- #define All(x) (x).begin(),(x).end()
- #pragma GCC optimize(3,"Ofast","inline")
- #define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
- using namespace std;
- const int N = 2e6 + 10;
-
- // 存储 点与路径长度
- using PII = pair<int,int>;
-
- int n,m,s;
-
- int dist[N]; // 记录对应点的最短路
- bool st[N]; // 标记该点是否走到过
-
- // 数组模拟邻接表,更有效率
- int h[N],e[N],w[N],ne[N],idx;
- inline void Add(int a,int b,int c)
- {
- e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx++;
- }
-
- inline void Dijkstra()
- {
- // 初始化 最短路
- memset(dist,INF,sizeof dist);
- // 初始化起点最短路距离是 0
- dist[s] = 0;
-
- // 建立存储的 堆,根据小根堆的 小到大排序
- priority_queue
,greater>q; -
- // 这里小根堆的小到大排序规则,
- // 所以我们需要距离小的排前面,点放在后面
- q.push(mk(0,s));
-
- // 这里有一点点类似 BFS 做法
- while(q.size())
- {
- // 取出我们对应最短距离需要更新的堆组合
- auto now = q.top();
- q.pop();
-
- int a = now.y; // 取出对应的点
- int distence = now.x; // 取出对应的最短距离
-
- if(st[a]) continue; // 如果我们点走动过,就不用更新走动了
-
- st[a] = true; // 标记当前走动更新的点
-
- // 更新该点的 dist
- for(int i = h[a];i != -1;i = ne[i])
- {
- int j = e[i]; // 取出对应点的关系
-
- // 如果该点j的距离 比 a 点到 j 点的距离还要大,那么更新最短路径距离
- if(dist[j] > distence + w[i]) dist[j] = distence + w[i];
-
- // 存储对应距离和对应点,方便下一次更新
- q.push(mk(dist[j],j));
- }
- }
- return ;
- }
-
- inline void solve()
- {
- // 链表初始化
- memset(h,-1,sizeof h);
-
- cin >> n >> m >> s;
- while(m--)
- {
- int a,b,c;
- cin >> a >> b >> c;
-
- // 添加链表,记录两点之间的距离
- Add(a,b,c);
- Add(b,a,c);
- }
-
- // 求最短路
- Dijkstra();
-
- // 输出各点的所得最短距离
- for(int i = 0;i < n;++i)
- {
- if(i)cout << ' ';
- if(dist[i] >= INF) cout << -1;
- else cout << dist[i];
- }
- }
-
- signed main()
- {
- // freopen("a.txt", "r", stdin);
- ___G;
- int _t = 1;
- // cin >> _t;
- while (_t--)
- {
- solve();
- }
-
- return 0;
- }
