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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

P1111 修复公路

發(fā)布時間:2024/5/6 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 P1111 修复公路 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目背景

A地區(qū)在地震過后,連接所有村莊的公路都造成了損壞而無法通車。

政府派人修復這些公路。

題目描述

給出A地區(qū)的村莊數(shù)N,和公路數(shù)M,公路是雙向的。

并告訴你每條公路的連著哪兩個村莊,并告訴你什么時候能修完這條公路。

問最早什么時候任意兩個村莊能夠通車,即最早什么時候任意兩條村莊都存在至少一條修復完成的道路(可以由多條公路連成一條道路)

輸入格式

第1行兩個正整數(shù)N,M

下面M行,每行3個正整數(shù)x, y, t,告訴你這條公路連著x,y兩個村莊,在時間t時能修復完成這條公路。

輸出格式

如果全部公路修復完畢仍然存在兩個村莊無法通車,則輸出?1,否則輸出最早什么時候任意兩個村莊能夠通車。

輸入輸出樣例

輸入 #1

4 4 1 2 6 1 3 4 1 4 5 4 2 3

輸出 #1

5

說明/提示

N≤1000,M≤100000

x≤N,y≤N,t≤100000

Code

/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^0. ^ 0^.0 1 .^.1 ^0 .........001^.1 1. .111100....01^00 11^ ^1. .1^1.^ ^0 0^.^ ^0..1.1 1..^1 .0 ^ ^00. ^^0.^^ 0 ^^110.^0 0 ^ ^^^10.01^^ 10 1 1 ^^^1110.101 10 1.1 ^^^1111110010 01 ^^ ^^^1111^1.^ ^^^10 10^ 0^ 1 ^^111^^^0.1^ 1....^11 0 ^^11^^^ 0.. ....1^ ^ ^1. 0^ ^11^^^ ^ 1 111^ ^ 0.10 00 11 ^^^^^ 1 0 1.0^ ^0 ^0 ^^^^ 0 0.0^ 1.0 .^ ^^^^ 1 1 .0^.^ ^^ 0^ ^1 ^^^^ 0. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 1001 ^^ ^^^ . 1^. ^ ^. 11 0. 1 ^ ^^ 0.0 ^. 0 ^0 1 ^^^ 0.0.^ 1. 0^ 0 .1 ^^^ ...1 1. 00 . .1 ^^^ ..1 1. ^. 0 .^ ^^ ..0. 1. .^ . 0 ..1 1. 01 . . ^ 0^.^ 00 ^0 1. ^ 1 1.0 00 . ^^^^^^ ..^ 00 01 ..1. 00 10 1 ^^.1 00 ^. ^^^ .1.. 00 .1 1..01 ..1.1 00 1. ..^ 10^ 1^ 00 ^.1 0 1 1.1 00 00 ^ 1 ^. 00 ^.^ 10^ ^^1.1 00 00 10^..^ 1. ^. 1.0 1 ^. 00 00 .^^ ^. ^ 1 00 ^0000^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 1. .^1. 1 1. ^0^ . ^.1 00 01^.0 001. .^*/ // train —— 1111.cpp created by VB_KoKing on 2019-08-15. /* Procedural objectives:Variables required by the program:Procedural thinking:Functions required by the program:Determination algorithm:Determining data structure:*/ /* My dear Max said: "I like you, So the first bunch of sunshine I saw in the morning is you, The first gentle breeze that passed through my ear is you, The first star I see is also you. The world I see is all your shadow."FIGHTING FOR OUR FUTURE!!! */#include <cstdio> #include <algorithm>using namespace std;struct Road{int x, y, t; }roads[100007]; int villages[100007];int find(int x){int res = x;while (villages[res])res = villages[res];while (x != res){int temp = villages[x];villages[x] = res;x = temp;}return res; }bool join(int x, int y){int res_x = find(x), res_y = find(y);if (res_x != res_y){villages[res_x] = res_y;return true;}return false; }bool cmp(const Road &a, const Road &b){return a.t < b.t; }void solve() {int n, m;scanf("%d %d",&n, &m);for (int i = 0; i < m; i++)scanf("%d %d %d", &roads[i].x, &roads[i].y, &roads[i].t);sort(roads, roads + m, cmp);for (int i = 0; i < m; i++) {n -= join(roads[i].x, roads[i].y);if (n == 1){printf("%d\n",roads[i].t);return;}}if (n > 1) printf("-1\n"); }int main(){solve();return 0; } 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的P1111 修复公路的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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