CODEVS——T1519 過路費

http://codevs.cn/problem/1519/?

時間限制: 1 s
?空間限制: 256000 KB
?題目等級 : 大師 Master
題解
?查看運行結果
題目描述?Description

? ? 在某個遙遠的國家里,有 n個城市。編號為 1,2,3,…,n。這個國家的政府修建了m 條雙向道路,每條道路連接著兩個城市。政府規定從城市 S 到城市T需要收取的過路費為所經過城市之間道路長度的最大值。如:A到B長度為 2,B到C 長度為3,那么開車從 A經過 B到C 需要上交的過路費為 3。
? ? 佳佳是個做生意的人,需要經常開車從任意一個城市到另外一個城市,因此他需要頻繁地上交過路費,由于忙于做生意,所以他無時間來尋找交過路費最低的行駛路線。然而, 當他交的過路費越多他的心情就變得越糟糕。 作為秘書的你,需要每次根據老板的起止城市,提供給他從開始城市到達目的城市,最少需要上交多少過路費。

輸入描述?Input Description

? ? 第一行是兩個整數 n 和m,分別表示城市的個數以及道路的條數。?
? ? 接下來 m 行,每行包含三個整數 a,b,w(1≤a,b≤n,0≤w≤10^9),表示a與b之間有一條長度為 w的道路。
? ? 接著有一行為一個整數 q,表示佳佳發出的詢問個數。?
? ? 再接下來 q行,每一行包含兩個整數 S,T(1≤S,T≤n,S≠T), 表示開始城市S 和目的城市T。

輸出描述?Output Description

? ? 輸出共q行,每行一個整數,分別表示每個詢問需要上交的最少過路費用。輸入數據保證所有的城市都是連通的。

樣例輸入?Sample Input

4 5?
1 2 10?
1 3 20?
1 4 100?
2 4 30?
3 4 10?
2?
1 4?
4 1

樣例輸出?Sample Output

20?
20

數據范圍及提示?Data Size & Hint

對于 30%的數據,滿足 1≤ n≤1000,1≤m≤10000,1≤q≤100;?
對于 50%的數據,滿足 1≤ n≤10000,1≤m≤10000,1≤q≤10000;?
對于 100%的數據,滿足 1≤ n≤10000,1≤m≤100000,1≤q≤10000;

?

(和貨車運輸很像)

先求出最小生成樹(使圖簡化),在此樹上做倍增,維護最大代價

 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 const int N(10000+15);
 8 const int M(100000+5);
 9 int n,m,q,u,v,ans;
10 struct Edge
11 {
12     int u,v,w;
13 }road[M];
14 bool cmp(Edge a,Edge b)
15 {
16     return a.w<b.w;
17 }
18 
19 int head[N],sumedge;
20 struct E
21 {
22     int v,next,w;
23     E(int v=0,int next=0,int w=0):
24         v(v),next(next),w(w){}
25 }edge[N<<1];
26 void ins(int u,int v,int w)
27 {
28     edge[++sumedge]=E(v,head[u],w);
29     head[u]=sumedge;
30 }
31 
32 int fa[N];
33 int find(int x)
34 {
35     return x==fa[x]?x:fa[x]=find(fa[x]);
36 }
37 void K()
38 {
39     int cnt=0;
40     sort(road+1,road+m+1,cmp);
41     for(int i=1;i<=n;i++) fa[i]=i;
42     for(int i=1;i<=m;i++)
43     {
44         int x=road[i].u,y=road[i].v;
45         int fx=find(x),fy=find(y);
46         if(fx==fy) continue;
47         fa[fx]=fy;
48         ins(x,y,road[i].w);
49         ins(y,x,road[i].w);
50         if(++cnt==n-1) return ;
51     }
52 }
53 
54 int dad[N],val[N],deep[N];
55 void DFS(int x)
56 {
57     deep[x]=deep[dad[x]]+1;
58     for(int i=head[x];i;i=edge[i].next)
59     {
60         int v=edge[i].v;
61         if(dad[v]) continue;
62         val[v]=edge[i].w;
63         dad[v]=x;  DFS(v);
64     }
65 }
66 int LCA(int x,int y)
67 {
68     int maxx=0,maxn=0;
69     if(deep[x]>deep[y]) swap(x,y);
70     for(;deep[y]>deep[x];y=dad[y]) maxx=max(maxx,val[y]);
71     for(;x!=y;x=dad[x],y=dad[y]) maxn=max(maxn,max(val[x],val[y]));
72     return max(maxn,maxx);
73 }
74 
75 int main()
76 {
77     scanf("%d%d",&n,&m);
78     for(int i=1;i<=m;i++)
79         scanf("%d%d%d",&road[i].u,&road[i].v,&road[i].w);
80     K(); DFS(1);
81     scanf("%d",&q);
82     for(;q--;)
83     {
84         scanf("%d%d",&u,&v);
85         printf("%d\n",LCA(u,v));
86     }
87     return 0;
88 }

?

轉載于:https://www.cnblogs.com/Shy-key/p/7308057.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391742.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391742.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391742.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

pca數學推導_PCA背后的統計和數學概念

pca數學推導As I promised in the previous article, Principal Component Analysis (PCA) with Scikit-learn, today, I’ll discuss the mathematics behind the principal component analysis by manually executing the algorithm using the powerful numpy and pandas lib…

pandas之cut

cut( )用來把一組數據分割成離散的區間。 cut(x, bins, rightTrue, labelsNone, retbinsFalse, precision3, include_lowestFalse, duplicatesraise) # x&#xff1a;被切分的數據&#xff0c;必須是一維的 # bins&#xff1a;①int型整數&#xff1a;將x按照數值大小平均分成分…

為Tueri.io構建React圖像優化組件

Let’s face it, image optimization is hard. We want to make it effortless.面對現實吧&#xff0c;圖像優化非常困難。 我們希望毫不費力。 When we set out to build our React Component there were a few problems we wanted to solve:當我們開始構建React組件時&#…

紅黑樹分析

紅黑樹的性質&#xff1a; 性質1&#xff1a;每個節點要么是黑色&#xff0c;要么是紅色。 性質2&#xff1a;根節點是黑色。性質3&#xff1a;每個葉子節點&#xff08;NIL&#xff09;是黑色。性質4&#xff1a;每個紅色節點的兩個子節點一定都是黑色。不能有兩個紅色節點相…

overlay 如何實現跨主機通信?- 每天5分鐘玩轉 Docker 容器技術(52)

上一節我們在 host1 中運行了容器 bbox1&#xff0c;今天將詳細討論 overlay 網絡跨主機通信的原理。 在 host2 中運行容器 bbox2&#xff1a; bbox2 IP 為 10.0.0.3&#xff0c;可以直接 ping bbox1&#xff1a; 可見 overlay 網絡中的容器可以直接通信&#xff0c;同時 docke…

第 132 章 Example

這里介紹一個負載均衡放置問題&#xff0c;我們可以把它擺放在任何位置&#xff0c;每種方案都各有優缺點&#xff0c;需要根據你的實際情況選擇使用 適用于HAProxy / Nginx / LVS 等等 這里用web,db為例子&#xff0c;講述負載均衡之間的關系 132.1. 雙負載均衡的用法 User --…

Python:實現圖片裁剪的兩種方式——Pillow和OpenCV

原文&#xff1a;https://blog.csdn.net/hfutdog/article/details/82351549 在這篇文章里我們聊一下Python實現圖片裁剪的兩種方式&#xff0c;一種利用了Pillow&#xff0c;還有一種利用了OpenCV。兩種方式都需要簡單的幾行代碼&#xff0c;這可能也就是現在Python那么流行的原…

第一個應在JavaScript數組的最后

by Thomas Barrasso由Thomas Barrasso 第一個應在JavaScript數組的最后 (The first shall be last with JavaScript arrays) So the last shall be [0], and the first [length — 1].所以最后一個應該是[0] &#xff0c;第一個[length_1]。 – Adapted from Matthew 20:16–根…

鼠標移動到ul圖片會擺動_我們可以從擺動時序分析中學到的三件事

鼠標移動到ul圖片會擺動An opportunity for a new kind of analysis of Major League Baseball data may be upon us soon. Here’s how we can prepare.不久之后&#xff0c;我們將有機會對美國職棒大聯盟數據進行新的分析。 這是我們準備的方法。 It is tempting to think t…

leetcode 1052. 愛生氣的書店老板(滑動窗口)

今天&#xff0c;書店老板有一家店打算試營業 customers.length 分鐘。每分鐘都有一些顧客&#xff08;customers[i]&#xff09;會進入書店&#xff0c;所有這些顧客都會在那一分鐘結束后離開。 在某些時候&#xff0c;書店老板會生氣。 如果書店老板在第 i 分鐘生氣&#xf…

回到網易后開源APM技術選型與實戰

篇幅一&#xff1a;APM基礎篇\\1、什么是APM?\\APM&#xff0c;全稱&#xff1a;Application Performance Management &#xff0c;目前市面的系統基本都是參考Google的Dapper&#xff08;大規模分布式系統的跟蹤系統&#xff09;來做的&#xff0c;翻譯傳送門《google的Dappe…

持續集成持續部署持續交付_如何開始進行持續集成

持續集成持續部署持續交付Everything you need to know to get started with continuous integration: branching strategies, tests automation, tools and best practices.開始進行持續集成所需的一切&#xff1a;分支策略&#xff0c;測試自動化&#xff0c;工具和最佳實踐。…

51nod 1073約瑟夫環

思路傳送門 &#xff1a;http://blog.csdn.net/kk303/article/details/9629329 n里面挑選m個 可以遞推從n-1里面挑m個 然后n-1里面的x 可以轉換成 n里面的x 的公式 x &#xff08;xm&#xff09;%n; #include <bits/stdc.h> using namespace std;int main () {int n,m;s…

如何選擇優化算法遺傳算法_用遺傳算法優化垃圾收集策略

如何選擇優化算法遺傳算法Genetic Algorithms are a family of optimisation techniques that loosely resemble evolutionary processes in nature. It may be a crude analogy, but if you squint your eyes, Darwin’s Natural Selection does roughly resemble an optimisa…

robot:截圖關鍵字

參考&#xff1a; https://www.cnblogs.com/hong-fithing/p/9656221.html--python https://blog.csdn.net/weixin_43156282/article/details/87350309--robot https://blog.csdn.net/xiongzaiabc/article/details/82912280--截圖指定區域 轉載于:https://www.cnblogs.com/gcgc/…

leetcode 832. 翻轉圖像

給定一個二進制矩陣 A&#xff0c;我們想先水平翻轉圖像&#xff0c;然后反轉圖像并返回結果。 水平翻轉圖片就是將圖片的每一行都進行翻轉&#xff0c;即逆序。例如&#xff0c;水平翻轉 [1, 1, 0] 的結果是 [0, 1, 1]。 反轉圖片的意思是圖片中的 0 全部被 1 替換&#xff…

SVN服務備份操作步驟

SVN服務備份操作步驟1、準備源服務器和目標服務器源服務器&#xff1a;192.168.1.250目標服務器&#xff1a;192.168.1.251 root/rootroot 2、對目標服務器&#xff08;251&#xff09;裝SVN服務器&#xff0c; 腳本如下&#xff1a;yum install subversion 3、創建一個新的倉庫…

SpringCloud入門(一)

1. 系統架構演變概述 #mermaid-svg-F8dvnEDl6rEgSP97 .label{font-family:trebuchet ms, verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-F8dvnEDl6rEgSP97 .label text{fill:#333}#mermaid-svg-F8dvnEDl6rEgSP97 .node rect,#merm…

PullToRefreshListView中嵌套ViewPager滑動沖突的解決

PullToRefreshListView中嵌套ViewPager滑動沖突的解決 最近恰好遇到PullToRefreshListView中需要嵌套ViewPager的情況,ViewPager 作為頭部添加到ListView中&#xff0c;發先ViewPager在滑動過程中流暢性太差幾乎很難左右滑動。在網上也看了很多大神的介紹&#xff0c;看了ViewP…

神經網絡 卷積神經網絡_如何愚弄神經網絡?

神經網絡 卷積神經網絡Imagine you’re in the year 2050 and you’re on your way to work in a self-driving car (probably). Suddenly, you realize your car is cruising at 100KMPH on a busy road after passing through a cross lane and you don’t know why.想象一下…