hdu 5385 The path
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 100001
- int 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=; char c=getchar();
- while(!isdigit(c)) c=getchar();
- while(isdigit(c)) { x=x*+c-''; 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> && s<=n)
- {
- if(!vis[s]) return;
- if(dis[s]!=-) 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=;
- memset(front,,sizeof(front));
- read(n); read(m);
- for(int i=;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,-,sizeof(dis));
- vis[]=true;
- L=; R=n;
- id=;
- while(L<R)
- {
- bfs(L,);
- bfs(R,-);
- }
- for(int i=;i<=m;++i)
- {
- x=abs(dis[e[i].u]-dis[e[i].v]);
- if(!x) x=n;
- printf("%d\n",x);
- }
- }
- }
hdu 5385 The path的更多相关文章
- 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 ...
- HDU.5385.The path(构造)
题目链接 最短路构造题三连:这道题,HDU4903,SRM590 Fox And City. \(Description\) 给定一张\(n\)个点\(m\)条边的有向图,每条边的边权在\([1,n] ...
- 贪心 HDOJ 5385 The Path
题目传送门 /* 题意:给一张图和一些有向边,问如何给边赋值使得d1 < d2 < .. < dx > ,,,< ddn 贪心:左边从2开始,右边从n开始,每次选择未标记 ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- HDU 5636 Shortest Path 暴力
Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...
- hdu 3631 Shortest Path
floyd算法好像很奇妙的样子.可以做到每次加入一个点再以这个点为中间点去更新最短路,效率是n*n. #include<cstdio> #include<cstring> #i ...
- HDU 5636 Shortest Path(Floyed,枚举)
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...
随机推荐
- 【ORACLE】oracle11g RAC搭建
--安装好操作系统(rhel-server-6.7 on vmware) 注意事项: 1.磁盘配置lvm 2.账号密码 root/oracle ---------------------------- ...
- CSS快速入门-基本选择器
1.标签选择器 通过标签进行元素选择. <style> a { font-size:10px; color:red; } </style> 2.* *代表通配符,匹配任意标签, ...
- setBit testBit权限管理
1.jdk7文档解释 public boolean testBit(int n) Returns true if and only if the designated bit is set. (Com ...
- ELK安装部署
一.ELK简介 ELK是Elasticsearch.Logstash.Kibana的简称,这三者是核心套件,但并非全部.Elasticsearch是实时全文搜索和分析引擎,提供搜集.分析.存储数据三大 ...
- Vigenere加密
Vigenere加密法原理很简单,实现起来也不难.与普通的单码加密法不同,明文经过加密之后,每个字母出现的频率就不会有高峰和低峰. 密钥中字母代表行和明文中的字母代表行.在vigenere表中找到对应 ...
- Python进阶量化交易场外篇3——最大回撤评价策略风险
新年伊始,很荣幸笔者的<教你用 Python 进阶量化交易>专栏在慕课专栏板块上线了,欢迎大家订阅!为了能够提供给大家更轻松的学习过程,笔者在专栏内容之外会陆续推出一些手记来辅助同学们学习 ...
- PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs
统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...
- Alpha答辩总结
[Alpha展示评审表格] 小组序号 小组名称 格式(20%) 内容(20%) PPT(20%) 演讲(20%) 答辩(20%) 总分 1 天机组 15 15 15 15 16 76 2 PMS 16 ...
- [Cyan之旅]使用NPOI实现Excel的导入导出,踩坑若干.
Cyan是博主[Soar360]自2014年以来开始编写整理的工具组件,用于解决现实工作中常用且与业务逻辑无关的问题. 什么是NPOI? NPOI 是 POI 项目的 .NET 版本.POI是一个开源 ...
- CMS垃圾回收过程
1.总体介绍: CMS(Concurrent Mark-Sweep)是以牺牲吞吐量为代价来获得最短回收停顿时间的垃圾回收器.对于要求服务器响应速度的应用上,这种垃圾回收器非常适合.在启动JVM参数加上 ...