【刷题】BZOJ 2125 最短路
Description
給一個(gè)N個(gè)點(diǎn)M條邊的連通無向圖,滿足每條邊最多屬于一個(gè)環(huán),有Q組詢問,每次詢問兩點(diǎn)之間的最短路徑。
Input
輸入的第一行包含三個(gè)整數(shù),分別表示N和M和Q 下接M行,每行三個(gè)整數(shù)v,u,w表示一條無向邊v-u,長(zhǎng)度為w 最后Q行,每行兩個(gè)整數(shù)v,u表示一組詢問
Output
輸出Q行,每行一個(gè)整數(shù)表示詢問的答案
Sample Input
9 10 2 1 2 1 1 4 1 3 4 1 2 3 1 3 7 1 7 8 2 7 9 2 1 5 3 1 6 4 5 6 1 1 9 5 7Sample Output
5 6HINT
對(duì)于100%的數(shù)據(jù),N<=10000,Q<=10000
Solution
仙人掌上的最短路
建圓方樹,將原圖變成樹,求出每個(gè)點(diǎn)到根的最短距離,詢問的話差分一下就好了,這是個(gè)經(jīng)典差分
但是求LCA的時(shí)候要分情況
首先,如果LCA是圓點(diǎn),即不在環(huán)上走,那么直接差分就好了
如果LCA是方點(diǎn),那么就會(huì)要在環(huán)上走,所以要找LCA下面的兩個(gè)點(diǎn),就是進(jìn)入環(huán)的兩個(gè)點(diǎn),先求出詢問的兩個(gè)點(diǎn)到入環(huán)的兩個(gè)點(diǎn)的距離,然后要找入環(huán)的兩個(gè)點(diǎn)在環(huán)上短側(cè)的距離。所以對(duì)于每個(gè)環(huán)即點(diǎn)雙,要保存這個(gè)環(huán)的總長(zhǎng),以及每個(gè)點(diǎn)的前綴長(zhǎng),以便快速求環(huán)上兩點(diǎn)的最短距離
然后就做完了
#include<bits/stdc++.h> #define ui unsigned int #define ll long long #define db double #define ld long double #define ull unsigned long long const int MAXN=10000+10,MAXM=1000000+10,inf=0x3f3f3f3f; int n,m,Q,e,to[MAXM<<1],nex[MAXM<<1],beg[MAXN],DFN[MAXN],LOW[MAXN],d[MAXN],dep[MAXN],Jie[20][MAXN],Visit_Num,sum[MAXN],len[MAXN],cnt,out[MAXM<<1],Be[MAXN],was[MAXM<<1],p[MAXN]; std::stack<int> s; std::queue<int> q; template<typename T> inline void read(T &x) {T data=0,w=1;char ch=0;while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();if(ch=='-')w=-1,ch=getchar();while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();x=data*w; } template<typename T> inline void write(T x,char ch='\0') {if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+'0');if(ch!='\0')putchar(ch); } template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);} template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);} template<typename T> inline T min(T x,T y){return x<y?x:y;} template<typename T> inline T max(T x,T y){return x>y?x:y;} inline void insert(int x,int y,int z=0) {to[++e]=y;nex[e]=beg[x];out[e]=x;beg[x]=e;was[e]=z; } inline void SPFA(int s) {for(register int i=1;i<=n;++i)d[i]=inf;d[s]=0;p[s]=1;q.push(s);while(!q.empty()){int x=q.front();q.pop();p[x]=0;for(register int i=beg[x];i;i=nex[i])if(d[to[i]]>d[x]+was[i]){d[to[i]]=d[x]+was[i];if(!p[to[i]])p[to[i]]=1,q.push(to[i]);}} } inline void Tarjan(int x,int f) {DFN[x]=LOW[x]=++Visit_Num;for(register int i=beg[x];i;i=nex[i])if(to[i]==f)continue;else if(!DFN[to[i]]){s.push(i);Tarjan(to[i],x);chkmin(LOW[x],LOW[to[i]]);if(LOW[to[i]]>=DFN[x]){int temp;++cnt;do{temp=s.top();s.pop();len[cnt]+=was[temp];if(out[temp]!=x||to[temp]!=to[i])sum[out[temp]]=0;sum[out[temp]]+=sum[to[temp]]+was[temp];if(out[temp]!=x){Jie[0][out[temp]]=x;Be[out[temp]]=cnt;}if(to[temp]!=x){Jie[0][to[temp]]=x;if(to[temp]!=to[i])Be[to[temp]]=cnt;}}while(out[temp]!=x||to[temp]!=to[i]);}}else if(DFN[to[i]]<DFN[x])s.push(i),chkmin(LOW[x],DFN[to[i]]); } inline void dfs(int x,int f) {dep[x]=dep[f]+1;for(register int i=beg[x];i;i=nex[i])if(to[i]!=f)dfs(to[i],x); } inline int LCA(int u,int v,int &iu,int &iv) {if(dep[u]<dep[v])std::swap(u,v);iu=iv=v;int tmp=dep[u]-dep[v];if(dep[u]>dep[v])for(register int i=19;i>=0;--i)if(tmp>>i&1)u=Jie[i][u];if(u==v)return u;for(register int i=19;i>=0;--i)if(Jie[i][u]^Jie[i][v])u=Jie[i][u],v=Jie[i][v];iu=u,iv=v;return Jie[0][u]; } int main() {read(n);read(m);read(Q);for(register int i=1;i<=m;++i){int u,v,w;read(u);read(v);read(w);insert(u,v,w);insert(v,u,w);}SPFA(1);Tarjan(1,0);e=0;memset(beg,0,sizeof(beg));for(register int i=2;i<=n;++i)insert(i,Jie[0][i]),insert(Jie[0][i],i);dfs(1,0);for(register int j=1;j<=19;++j)for(register int i=1;i<=n;++i)Jie[j][i]=Jie[j-1][Jie[j-1][i]];while(Q--){int u,v,iu,iv,lca,res=0;read(u);read(v);lca=LCA(u,v,iu,iv);if(Be[iu]&&Be[iu]==Be[iv]){int l=std::abs(sum[iu]-sum[iv]),r=len[Be[iu]]-l;res=d[u]+d[v]-d[iu]-d[iv]+min(l,r);}else res=d[u]+d[v]-(d[lca]<<1);printf("%d\n",res);}return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/hongyj/p/9561437.html
總結(jié)
以上是生活随笔為你收集整理的【刷题】BZOJ 2125 最短路的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 中的 os 模块常见方法?
- 下一篇: BZOJ1079 [SCOI2008]着