hdu 5385 The path(最短路+构造)
来源:互联网 发布:finale mac 汉化 编辑:程序博客网 时间:2024/05/19 13:42
题目链接:hdu 5385 The path
维护一个l,r,l从2开始递增,r从N开始递减,每个距离值l,r至多走一步,并且每次将可以到达的点标记,注意最后最大值只能有一个。
#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;typedef pair<int,int> pii;const int maxn = 1e5 + 5;int N, M, D[maxn], R[maxn];int X[maxn], Y[maxn], W[maxn];vector<pii> G[maxn];void init () {scanf("%d%d", &N, &M);memset(R, 0, sizeof(R));for (int i = 1; i <= N; i++) {D[i] = -1;G[i].clear();}for (int i = 1; i <= M; i++) {W[i] = -1;scanf("%d%d", &X[i], &Y[i]);G[X[i]].push_back(make_pair(Y[i], i));//G[Y[i]].push_back(make_pair(X[i], i));}}void solve () {D[1] = 0;queue<int> Q;Q.push(1);int l = 2, r = N;for (int d = 1; d <= N; d++) {if (r < l)break;while (!Q.empty()) {int u = Q.front();Q.pop();for (int i = 0; i < G[u].size(); i++) {int v = G[u][i].first;if (R[v])continue;R[v] = G[u][i].second;}}if (R[l]) {D[l] = d;int u = X[R[l]] + Y[R[l]] - l;W[R[l]] = D[l] - D[u];Q.push(l++);}if (r > l && R[r]) {D[r] = d;int u = X[R[r]] + Y[R[r]] - r;W[R[r]] = D[r] - D[u];Q.push(r--);}}/*for (int i = 1; i <= N; i++)printf("%d ", D[i]);printf("\n");*/}int main () {int cas;scanf("%d", &cas);while (cas--) {init ();solve ();for (int i = 1; i <= M; i++)printf("%d\n", W[i] == -1 ? N : W[i]);}return 0;}
0 0
- hdu 5385 The path(最短路+构造)
- HDU 5385(The path-构造最短路树)
- HDU 2807 The Shortest Path(最短路构造+Floyed算法)
- HDU5385 The path (构造最短路 BFS)
- hdu 2807 The Shortest Path(最短路)
- HDU-2807 The Shortest Path 最短路
- 【HDU】2807 The Shortest Path 最短路
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
- HDU 5385 The path(构造)
- HDU 5385 The path(贪心+构造)
- hdu 5385 The path 构造
- hdu 3631 Shortest Path(最短路)
- HDU 4479 Shortest path(最短路)
- HDU 5636 Shortest Path(最短路)
- HDU The Shortest Path 矩阵的最短路
- 【HDU】4725 The Shortest Path in Nya Graph 最短路
- 【最短路】 HDU 4725 The Shortest Path in Nya Graph
- hdu 4725 The Shortest Path in Nya Graph(最短路)
- 我的ROS入门(五):总算搞通ROS的服务节点订阅发布消息话题了
- Thread的创建方式以及稍微深入的分析:
- 如何new与delete各种二维数组
- 程序封装_随记(1)
- struts2学习笔记(九)struts2内置校验
- hdu 5385 The path(最短路+构造)
- 当修改一些代码时,使用什么编译命令可以最有效率
- 程序封装_随记(2)
- win10机器
- 程序员什么时候该考虑辞职
- Eclipse搭建最简单的Servlet及配置过程
- java语句
- 光栅化方法优化
- 解决自定义seekbar和listview显示冲突的问题