hdu 5385 The path
生活随笔
收集整理的這篇文章主要介紹了
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 100001int 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=0; char c=getchar();while(!isdigit(c)) c=getchar();while(isdigit(c)) { x=x*10+c-'0'; 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>0 && s<=n){if(!vis[s]) return;if(dis[s]!=-1) 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=0;memset(front,0,sizeof(front));read(n); read(m);for(int i=1;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,-1,sizeof(dis));vis[1]=true;L=1; R=n;id=0;while(L<R){bfs(L,1);bfs(R,-1);}for(int i=1;i<=m;++i) {x=abs(dis[e[i].u]-dis[e[i].v]);if(!x) x=n;printf("%d\n",x);}} }?
?
轉載于:https://www.cnblogs.com/TheRoadToTheGold/p/8443161.html
總結
以上是生活随笔為你收集整理的hdu 5385 The path的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP Misc. 函数
- 下一篇: 关于bitnami redmine 的一