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 ...
随机推荐
- Asp.Net_抓包解析xml文件为json
protected void Button1_Click(object sender, EventArgs e) { string Phone = this.Txt_Con.Text; string ...
- Mvc4_@Styles.Render提高性能
在页面上可以用@Styles.Render("~/Content/css") 来加载css 首先要在App_Start 里面BundleConfig.cs 文件里面 添加要包含的c ...
- openstack删除僵尸卷
问题描述: 最近在清理openstack环境,在删除cinder云硬盘时,一直发现有两个卷在删除中. 解决方法如下: 首先我们去cinder的数据库中找到这个卷,命令为: MariaDB [(none ...
- webstorm开发微信小程序代码提醒(webstorm开发工具)
使用了微信提供的开发工具是真心难用,卡顿厉害.中英文切写注释换相当不爽.还没办法多开窗口,相信大家也遇到过这种现象. 下边我们介绍下webstorm来开发微信小程序的一些配置: File---sett ...
- LINUX内核分析第六周学习总结——进程的描述和进程的创建
LINUX内核分析第六周学习总结——进程的描述和进程的创建 张忻(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://mooc.study.163.com/cours ...
- 《Linux内核设计与实现》 第三周 读书笔记
第一章 Linux内核简介 1. Unix的历史 Unⅸ虽然已经使用了40年,但计算机科学家仍然认为它是现存操作系统中最强大和最优秀的系统. Unix强大的根本原因: 简洁 在Unix中所有的东西都被 ...
- (第二周)读《我是一只IT小小鸟》有感
读了蒋宇东学长的这篇描述他成长经历和生活感悟的博文,我真的收获了很多,有一种“相见恨晚”的感觉.同为航院的学子,我们有太多太多相同的生活学习经历. 我已经是一名大三的学生了,不知不觉中我大学生活的大部 ...
- iOS-copy与mutableCopy浅析
iOS-copy与mutableCopy浅析 iOS 浅谈:深.浅拷贝与copy.strong 总结:当不可变类型对象调用copy拷贝后,不会产生新的对象,属于浅拷贝,其他类型对象不管调用copy亦或 ...
- VMware虚拟机安装ghost win7系统方法
原本地址:http://www.xitongcheng.com/jiaocheng/xtazjc_article_15314.html
- Luogu1641 SCOI2010生成字符串(组合数学)
NOI2018冒泡排序的一个子问题. #include<iostream> #include<cstdio> #include<cmath> #include< ...