HDU 5385 The path(构造)
来源:互联网 发布:幼儿早教软件 编辑:程序博客网 时间:2024/05/22 14:45
贪心做法,一开始将1号点作为最短路径树的根,然后左边从2开始,右边从n开始,只要之前加入的点有边连向他们就加入
这样一个点加入的时间就是他的dis值,最短路径树上的父亲也可以确定,于是输出时非树边长度为n,树边长度为两个端点dis之差
#pragma warning(disable:4996)#include <cstdio>#include <cstring>#include <vector>using namespace std;const int N = 100005;struct edge{int u, v;};vector<int>g[N];//has[u]标记是否有从访问过的点到点u的边bool has[N];//标记访问时间int dis[N];edge e[N];int n, m;void add(int u, int v, int i){g[u].push_back(v);e[i].u = u;e[i].v = v;}void tmp(){memset(has, false, sizeof has);memset(dis, 0, sizeof dis);dis[1] = 0;for (int i = 0; i < g[1].size(); i++){int v = g[1][i];has[v] = true;}int cnt = 1, l = 2, r = n;while (l <= r){if (l == r)r = 0;if (has[l]){dis[l] = cnt++;int u = l;for (int i = 0; i < g[u].size(); i++){int v = g[u][i];has[v] = true;}l++;}if (has[r]){dis[r] = cnt++;int u = r;for (int i = 0; i < g[u].size(); i++){int v = g[u][i];has[v] = true;}r--;}}}int main(){//freopen("in.txt", "r", stdin);int t; scanf("%d", &t);while (t--){scanf("%d %d", &n, &m);for (int i = 0; i <= n; i++)g[i].clear();for (int i = 1; i <= m; i++){int u, v; scanf("%d %d", &u, &v);add(u, v, i);}tmp();for (int i = 1; i <= m; i++){int u = e[i].u, v = e[i].v;if (dis[u] < dis[v])printf("%d\n", dis[v] - dis[u]);else printf("%d\n", n);}}return 0;}
0 0
- HDU 5385 The path(构造)
- HDU 5385 The path(贪心+构造)
- hdu 5385 The path 构造
- hdu 5385 The path(最短路+构造)
- hdu 5385 The path 贪心?构造
- HDU 5385(The path-构造最短路树)
- HDOJ 5385 The path 构造
- HDU 5385 The path
- hdu 5385 The path
- hdu 5385 The path(贪心)
- 贪心,Dijkstra(The path,HDU 5385)
- hdu 5385 The path 贪心
- HDU 2807 The Shortest Path(最短路构造+Floyed算法)
- hdu 5385 The path (贪心+最短路径树)
- hdu 2807 The Shortest Path(矩阵)
- The Best Path ----HDU
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
- hdu 5302 Connect the Graph(构造)
- 暖暖环游世界
- View视图绘制流程,View工作原理(二)
- 黑马程序员-----c语言基础与重点第一篇——C知识基础
- 如何修复Linux中出现的“ImportError: No module named wxversion”错误
- 智能家居,一团乱麻
- HDU 5385 The path(构造)
- 【快速幂】【扩展欧几里德】【BSGS】【SDOI 2011】【bzoj 2242】计算器
- 关闭防火墙解决httpclient.execute长时间停滞
- php生成二维码-PHP QR Code
- tips: 监听radio的变化以及获取radio选中值
- hdu1069 Monkey and Banana(dp+贪心)
- Unity3d Assetbundle文件的导出与加载
- Memcached笔记
- freemarker常见语法大全