日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

bzoj#2125. 最短路

發布時間:2023/12/3 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bzoj#2125. 最短路 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

bzoj#2125. 最短路

題目描述

Description
給一個N個點M條邊的連通無向圖,滿足每條邊最多屬于一個環,有Q組詢問,每次詢問兩點之間的最短路徑。
Input
輸入的第一行包含三個整數,分別表示N和M和Q 下接M行,每行三個整數v,u,w表示一條無向邊v-u,長度為w 最后Q行,每行兩個整數v,u表示一組詢問
Output
輸出Q行,每行一個整數表示詢問的答案

Solution

題意就是求仙人掌中兩點最短路。

就是仙人掌圓方樹的板子題。

對于每一個環用一個方點ttt代替環,從方點ttt向這個環的根節點xxx連一條代價為000的邊,再從ttt向環上其他所有點yyy連一條代價為mindis(x,y)mindis(x,y)mindis(x,y)的邊,并在ttt上記錄整個環的總長度sumtsum_tsumt?

每一次詢問(x,y)(x,y)(x,y)最短路時,分類討論LCA(x,y)LCA(x,y)LCA(x,y)是方點還是圓點。
若是圓點答案即為dist(x,y)dist(x,y)dist(x,y)
若是方點,則答案為xxx到環的最短路+yyy到環的最短路+xxx接入環的節點與yyy接入環的節點之間的最短路。

因此只要倍增或樹剖預處理出LCA與路徑和,每次跳LCA的時候順路求解答案即可。

時間復雜度O(nlg?n)O(n\lg n)O(nlgn)

#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <cassert> #include <string.h> //#include <unordered_set> //#include <unordered_map> //#include <bits/stdc++.h>#define MP(A,B) make_pair(A,B) #define PB(A) push_back(A) #define SIZE(A) ((int)A.size()) #define LEN(A) ((int)A.length()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define fi first #define se secondusing namespace std;template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; } template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }typedef long long ll; typedef unsigned long long ull; typedef long double lod; typedef pair<int,int> PR; typedef vector<int> VI;const lod eps=1e-11; const lod pi=acos(-1); const int oo=1<<30; const ll loo=1ll<<62; const int mods=1e9+7; const int MAXN=30005; const int INF=0x3f3f3f3f;//1061109567 /*--------------------------------------------------------------------*/ inline int read() {int f=1,x=0; char c=getchar();while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }return x*f; } struct enode{int to,c; }; vector<enode> e[MAXN],E[MAXN]; int DFN=0,n,m,Case,num,dist[MAXN],sum[MAXN],Fa[MAXN],flag[MAXN]; int dfn[MAXN],low[MAXN],fa[MAXN][15],sm[MAXN][15],dep[MAXN],Log[MAXN]; void add_edge(int x,int y,int c) { // cout<<"Edge:"<<x<<" "<<y<<" "<<c<<endl;E[x].PB(((enode){y,c}));E[y].PB(((enode){x,c})); } void solve(int x,int y,int c) {sum[++num]=dist[y]-dist[x]+c;add_edge(x,num,0);for (int p=y;p!=x;p=Fa[p]){int Dis=min(dist[p]-dist[x],sum[num]-(dist[p]-dist[x]));add_edge(p,num,Dis);flag[p]=(Dis==(dist[p]-dist[x]));} } void tarjan(int x,int father) {dfn[x]=low[x]=++DFN,Fa[x]=father;for (auto V:e[x]){int v=V.to,c=V.c;if (!dfn[v]) dist[v]=dist[x]+c,tarjan(v,x),upmin(low[x],low[v]);else if (v!=father) upmin(low[x],dfn[v]);if (dfn[x]<low[v]) add_edge(x,v,c);}for (auto V:e[x]){int v=V.to,c=V.c;if (Fa[v]!=x&&dfn[v]>dfn[x]) solve(x,v,c);} } void dfs(int x,int father) {dep[x]=dep[father]+1;for (int i=1;i<=Log[dep[x]];i++) fa[x][i]=fa[fa[x][i-1]][i-1],sm[x][i]=sm[fa[x][i-1]][i-1]+sm[x][i-1];for (auto v:E[x]){if (v.to==father) continue;fa[v.to][0]=x;sm[v.to][0]=v.c;dfs(v.to,x);} } int getans(int x,int y) {int s=0;if (dep[x]<dep[y]) swap(x,y);for (int i=Log[dep[x]];i>=0;i--)if (dep[fa[x][i]]>=dep[y]) s+=sm[x][i],x=fa[x][i]; // cout<<x<<" "<<y<<" "<<fa[x][0]<<" "<<s<<endl;if (x==y) return s;for (int i=Log[dep[x]];i>=0;i--)if (fa[x][i]!=fa[y][i]) s+=sm[x][i]+sm[y][i],x=fa[x][i],y=fa[y][i];// cout<<x<<" "<<y<<" "<<fa[x][0]<<" "<<s<<endl;if (fa[x][0]<=n) return s+sm[x][0]+sm[y][0];int Dis;if (flag[x]^flag[y]) Dis=sm[x][0]+sm[y][0];else Dis=abs(sm[x][0]-sm[y][0]);return s+min(Dis,sum[fa[x][0]]-Dis); } int main() {n=read(),m=read(),Case=read(),num=n;for (int i=1;i<=m;i++){int u=read(),v=read(),c=read();e[u].PB(((enode){v,c}));e[v].PB(((enode){u,c}));}tarjan(1,0);dep[0]=-1,Log[1]=0;for (int i=2;i<=num;i++) Log[i]=Log[i>>1]+1;dfs(1,0); // for (int i=1;i<=num;i++) cout<<fa[i][0]<<" "<<sm[i][0]<<" "<<fa[i][1]<<" "<<sm[i][1]<<endl;while (Case--){int x=read(),y=read();printf("%d\n",getans(x,y));}return 0; } /* 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 75 6 */

總結

以上是生活随笔為你收集整理的bzoj#2125. 最短路的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。