日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

hdu 5385 The path

發(fā)布時(shí)間:2025/7/25 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 5385 The path 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

http://acm.hdu.edu.cn/showproblem.php?pid=5385

?

題意:

給定一張n個(gè)點(diǎn)m條有向邊的圖,構(gòu)造每條邊的邊權(quán)(邊權(quán)為正整數(shù)),令d(x)表示1到x的最短路,使得存在點(diǎn)i(1<=i<=n)滿足d(1)<d(2)<…<d(i)>d(i+1)>…>d(n)。

?

從兩邊向中間構(gòu)造。

開始L=1,R=n

從L開始bfs,順次構(gòu)造L,L+1,L+2……

構(gòu)造不動(dòng)了再從R開始bfs,順次構(gòu)造R,R-1,R-2……

然后在從L開始……

直到L>=R

第j個(gè)bfs到的點(diǎn),就令它的dis[i]=j

邊u-->v的邊權(quán)即為 dis[v]-dis[u] 的絕對(duì)值

?

?

#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);}} }

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/TheRoadToTheGold/p/8443161.html

總結(jié)

以上是生活随笔為你收集整理的hdu 5385 The path的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。