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 ...
- Js_增删改Cookie的值
//获得cookie 的值function cookie(name) { var cookieArray = document.cookie.split("; "); //得到分割 ...
- STM32下载Bin文件的几种方式
一.STM32 ST-LINK Utility 1.下载安装软件 官网下载地址:http://www.st.com/zh/development-tools/stsw-link004.html 百度网 ...
- Unity程序协同问题,传送时屏幕变黑变亮的解决,常规操作的行为集合
在unity中运行某段程序时往往需要运行另外一段不相干但是却对功能上有需求的程序,比如进行场景传送,在传送点处,点击I键,屏幕慢慢变黑,场景传送到另外一个场景,场景又慢慢变亮.这里首先涉及两个物体,一 ...
- python基础面试题
函数1def foo(arg,li=[]): li.append(arg) return li list1 = foo(21) list2 = foo(11,[2]) list3 = foo(28) ...
- keycode值对照表
转载自:https://segmentfault.com/a/1190000005828048 字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 7 ...
- PAT-1004 Counting Leaves
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- WebShell代码分析溯源(第1题)墨者学院
一.访问链接 二.下载系统源码后直接放到D盾里扫描,扫到后门文件 三.查看该木马文件 <?php error_reporting(0); $_GET['POST']($_POST['GET']) ...
- [T-ARA][결혼 하지마][不要结婚]
歌词来源:http://music.163.com/#/song?id=27808773 作曲 : 二段横踢 [作曲 : 二段横踢] 作词 : 二段横踢 [作词 : 二段横踢] Hey anybody ...
- 《Linux内核设计与实现》第5章读书整理
<第五章 系统调用>笔记 5.1 与内核通信 系统调用在用户空间和硬件设备之间提供了一个中间层. 中间层的作用: 为用户空间提供一 ...