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的更多相关文章

  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. Error:Could not find common.jar (android.arch.core:common:1.0.0)

    Error:Could not find common.jar (android.arch.core:common:1.0.0). Searched in the following location ...

  2. BUGKU--刷题

    刷题 一.BUGKU WEB 1. 变量1 知识点php两个$$是 可变变量,就是一个变量的变量名可以动态的设置和使用 $GLOBALS一个包含了全部变量的全局组合数组.变量的名字就是数组的键 < ...

  3. JAVA笔试准备

    建立时间:2019.4.19 修改时间: 腾讯:选择题(30个,一小时内),简答(2道)和编程题(2道) 涉及内容:(也有可能全是算法)C++,JAVA,数据结构,网络,Linux,计算题 1. 磁盘 ...

  4. BugPhobia开发篇章:Beta阶段第I次Scrum Meeting

    0x01 :Scrum Meeting基本摘要 Beta阶段第一次Scrum Meeting 敏捷开发起始时间 2015/12/10 00:00 A.M. 敏捷开发终止时间 2015/12/12 23 ...

  5. Linux内核的启动过程分析

    秦鼎涛 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验目的及要求: 使用gdb跟踪调试内核从s ...

  6. rethinking virtual network embedding..substrate support for path splitting and migration阅读笔记

    1.引言 网络虚拟化, 1.支持同一个底层网络有多种网络架构,每种架构定制一个应用或用户社区. 2.也可以让多个服务提供者在共同的物理基础设施上定制端到端的服务.如Voice over IP(VoIP ...

  7. 我与git“美妙”的一天

    今天是第一天使用git,苦不堪言,感觉服务器和自己都要爆炸了,弄了半天才马马虎虎会了一点,基本流程如下 1.在mukever.online注册用户 2.下载git for windows(一个客户端) ...

  8. 【Leetcode】209. Minimum Size Subarray Sum

    Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...

  9. delphi XE的字符串处理

    最近用delphi xe做了个东西,因为以前一直使用Delphi 7做开发,delphi 7 到delphi XE有了很大的变化,最大的变化就是对Unicode的支持,所以刚开始使用DELPHI XE ...

  10. golang yaml配置文件解析

    yaml文件语法 此模块内容转自:http://www.ruanyifeng.com/blog/2016/07/yaml.html 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使 ...