http://acm.hdu.edu.cn/showproblem.php?pid=5385

题意:

给定一张n个点m条有向边的图,构造每条边的边权(边权为正整数),令d(x)表示1到x的最短路,使得存在点i(1<=i<=n)满足d(1)<d(2)<…<d(i)>d(i+1)>…>d(n)。

从两边向中间构造。

开始L=1,R=n

从L开始bfs,顺次构造L,L+1,L+2……

构造不动了再从R开始bfs,顺次构造R,R-1,R-2……

然后在从L开始……

直到L>=R

第j个bfs到的点,就令它的dis[i]=j

边u-->v的边权即为 dis[v]-dis[u] 的绝对值

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>using namespace std;#define N 100001int n,m;struct node
{int u,v;
}e[N];int front[N],nxt[N],to[N],tot;bool vis[N];
int dis[N];int L,R;
int id;void read(int &x)
{x=0; char c=getchar();while(!isdigit(c)) c=getchar();while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
}void add(int u,int v)
{to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
}void bfs(int &s,int k)
{while(s>0 && s<=n){if(!vis[s]) return;if(dis[s]!=-1) return;dis[s]=id++;for(int i=front[s];i;i=nxt[i]) vis[to[i]]=true;s+=k;}
}int main()
{int T,x;read(T);while(T--){tot=0;memset(front,0,sizeof(front));read(n); read(m);for(int i=1;i<=m;++i){read(e[i].u);read(e[i].v);add(e[i].u,e[i].v);}memset(vis,false,sizeof(vis));memset(dis,-1,sizeof(dis));vis[1]=true;L=1; R=n;id=0;while(L<R){bfs(L,1);bfs(R,-1);}for(int i=1;i<=m;++i) {x=abs(dis[e[i].u]-dis[e[i].v]);if(!x) x=n;printf("%d\n",x);}}
}

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/8443161.html

hdu 5385 The path相关推荐

  1. HDU 5385 The path(贪心、构造、最短路径树)

    HDU 5385 题目大意:给定一个图,dis表示第i个点到1点的最短路,dis1=0,给有向图上的边赋权值(1~n)满足dis1<dis2<dis3<--<disk>d ...

  2. hdu 3631 Shortest Path(Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3631 Shortest Path Time Limit: 3000/1000 MS (Java/Oth ...

  3. hdu 5636 Shortest Path(Floyd最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5636 解题思路: 这道题可以用Floyd解决,不过需要特殊处理一下: 实际上我们只需要利用添加的那三条 ...

  4. HDU 4338 Simple Path 点双连通+lca

    [题目大意] 给你一个无向图.问,从点u到点v,若是只走简单路径,有多少个点不能到达? [思路] 这题肯定是要双连通缩点的,以前老是觉得点双连通不会用来缩点,因为割点可以属于多个连通集合,现在立马打脸 ...

  5. 杭电OJ分类题目(4)-Graph

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...

  6. P - The Shortest Path in Nya Graph HDU - 4725

    P - The Shortest Path in Nya Graph HDU - 4725 最短路 不是 每两个点之间按层数设置边权 + 额外边权 TLE 是 相邻两层之间设置边权 + 额外边权 需注 ...

  7. HDU 6223 Infinite Fraction Path

    链接 http://acm.hdu.edu.cn/showproblem.php?pid=6223 Problem Description The ant Welly now dedicates hi ...

  8. hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有n个点和n层,m条边,每一层的任意一个点都可以花费固定的值到下一层或者上一层的任意点 然 ...

  9. Hdu 4916 Count on the path

    意甲冠军:鉴于一棵树的顶点标签为连续1~n,不是每个网上查询a-b最小的圆点标签路径 这题想了好久,如果1为根节点. 首先如果a-b只是根节点1.答案一定是1. 否则我们用fa[i]表示i节点的父亲, ...

最新文章

  1. Educational Round 66 题解
  2. 田忌赛马贪心算法_田忌赛马 贪心算法
  3. PyTorch 1.7发布,支持CUDA 11、Windows分布式训练
  4. 部署自建CA颁发证书实现https加密
  5. 程序员的中场职业规划
  6. [New Portal]Windows Azure Web Site (4) Web Site Gallery
  7. Linux下离线安装python项目的依赖包
  8. win8.1 64位安装oracle10g客户端心得
  9. Struts1表单验证方式
  10. 搭建 zookeeper 和搭建dubbo监控中心
  11. 从TIN获取任意坐标点高程(原创)
  12. paip.没有源码的情况下更改ASP.net ASPX页
  13. protel 99se 负片打印
  14. AI人工智能服务器安装说明书,AI人工智能电脑配置及服务器双显卡的安装总结...
  15. 联想微型计算机怎么连接电源,终于认识联想电脑主板与机箱电源安装方法
  16. 模板消息php40008,企业微信发送模板消息 40008 Warning: wrong json format. ?
  17. 在postgre中进行搜索关键字查询的方法
  18. 不支持虚拟化的cpu如何开VM虚拟机(不支持,即“主机不支持Intel VT-x,不是支持Intel VT-x,但Intel VT-x禁处于禁用状态!!!!!!)
  19. 禁止复制服务器文件夹,远程桌面服务器 禁止复制文件夹
  20. python布尔型变量错误的赋值_Python中布尔变量的值为( )

热门文章

  1. A Guide to Python's Magic Methods
  2. 使用selenium+phantomJS实现网页爬取
  3. 安装ArchLinux BIOS
  4. Windows Azure 新上线网络相关服务
  5. python基本语法积累~
  6. 超级数学计算机,超级计算器+
  7. 字符集问题的初步探讨-乱码的产生
  8. centos vscode安装到指定目录_win10 WSL构建vscode+centos开发环境
  9. leetcode算法题--字符串的排列
  10. linux虚拟网络设备之bridge(桥)(三)