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] 的绝对值

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5.  
  6. using namespace std;
  7.  
  8. #define N 100001
  9.  
  10. int n,m;
  11.  
  12. struct node
  13. {
  14. int u,v;
  15. }e[N];
  16.  
  17. int front[N],nxt[N],to[N],tot;
  18.  
  19. bool vis[N];
  20. int dis[N];
  21.  
  22. int L,R;
  23. int id;
  24.  
  25. void read(int &x)
  26. {
  27. x=; char c=getchar();
  28. while(!isdigit(c)) c=getchar();
  29. while(isdigit(c)) { x=x*+c-''; c=getchar(); }
  30. }
  31.  
  32. void add(int u,int v)
  33. {
  34. to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
  35. }
  36.  
  37. void bfs(int &s,int k)
  38. {
  39. while(s> && s<=n)
  40. {
  41. if(!vis[s]) return;
  42. if(dis[s]!=-) return;
  43. dis[s]=id++;
  44. for(int i=front[s];i;i=nxt[i]) vis[to[i]]=true;
  45. s+=k;
  46. }
  47. }
  48.  
  49. int main()
  50. {
  51. int T,x;
  52. read(T);
  53. while(T--)
  54. {
  55. tot=;
  56. memset(front,,sizeof(front));
  57. read(n); read(m);
  58. for(int i=;i<=m;++i)
  59. {
  60. read(e[i].u);
  61. read(e[i].v);
  62. add(e[i].u,e[i].v);
  63. }
  64. memset(vis,false,sizeof(vis));
  65. memset(dis,-,sizeof(dis));
  66. vis[]=true;
  67. L=; R=n;
  68. id=;
  69. while(L<R)
  70. {
  71. bfs(L,);
  72. bfs(R,-);
  73. }
  74. for(int i=;i<=m;++i)
  75. {
  76. x=abs(dis[e[i].u]-dis[e[i].v]);
  77. if(!x) x=n;
  78. printf("%d\n",x);
  79. }
  80. }
  81. }

hdu 5385 The path的更多相关文章

  1. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  2. HDU.5385.The path(构造)

    题目链接 最短路构造题三连:这道题,HDU4903,SRM590 Fox And City. \(Description\) 给定一张\(n\)个点\(m\)条边的有向图,每条边的边权在\([1,n] ...

  3. 贪心 HDOJ 5385 The Path

    题目传送门 /* 题意:给一张图和一些有向边,问如何给边赋值使得d1 < d2 < .. < dx > ,,,< ddn 贪心:左边从2开始,右边从n开始,每次选择未标记 ...

  4. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  5. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  6. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  7. HDU 5636 Shortest Path 暴力

    Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...

  8. hdu 3631 Shortest Path

    floyd算法好像很奇妙的样子.可以做到每次加入一个点再以这个点为中间点去更新最短路,效率是n*n. #include<cstdio> #include<cstring> #i ...

  9. HDU 5636 Shortest Path(Floyed,枚举)

    Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...

随机推荐

  1. 【ORACLE】oracle11g RAC搭建

    --安装好操作系统(rhel-server-6.7 on vmware) 注意事项: 1.磁盘配置lvm 2.账号密码 root/oracle ---------------------------- ...

  2. CSS快速入门-基本选择器

    1.标签选择器 通过标签进行元素选择. <style> a { font-size:10px; color:red; } </style> 2.* *代表通配符,匹配任意标签, ...

  3. setBit testBit权限管理

    1.jdk7文档解释 public boolean testBit(int n) Returns true if and only if the designated bit is set. (Com ...

  4. ELK安装部署

    一.ELK简介 ELK是Elasticsearch.Logstash.Kibana的简称,这三者是核心套件,但并非全部.Elasticsearch是实时全文搜索和分析引擎,提供搜集.分析.存储数据三大 ...

  5. Vigenere加密

    Vigenere加密法原理很简单,实现起来也不难.与普通的单码加密法不同,明文经过加密之后,每个字母出现的频率就不会有高峰和低峰. 密钥中字母代表行和明文中的字母代表行.在vigenere表中找到对应 ...

  6. Python进阶量化交易场外篇3——最大回撤评价策略风险

    新年伊始,很荣幸笔者的<教你用 Python 进阶量化交易>专栏在慕课专栏板块上线了,欢迎大家订阅!为了能够提供给大家更轻松的学习过程,笔者在专栏内容之外会陆续推出一些手记来辅助同学们学习 ...

  7. PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

    统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...

  8. Alpha答辩总结

    [Alpha展示评审表格] 小组序号 小组名称 格式(20%) 内容(20%) PPT(20%) 演讲(20%) 答辩(20%) 总分 1 天机组 15 15 15 15 16 76 2 PMS 16 ...

  9. [Cyan之旅]使用NPOI实现Excel的导入导出,踩坑若干.

    Cyan是博主[Soar360]自2014年以来开始编写整理的工具组件,用于解决现实工作中常用且与业务逻辑无关的问题. 什么是NPOI? NPOI 是 POI 项目的 .NET 版本.POI是一个开源 ...

  10. CMS垃圾回收过程

    1.总体介绍: CMS(Concurrent Mark-Sweep)是以牺牲吞吐量为代价来获得最短回收停顿时间的垃圾回收器.对于要求服务器响应速度的应用上,这种垃圾回收器非常适合.在启动JVM参数加上 ...