P1111 修复公路
生活随笔
收集整理的這篇文章主要介紹了
P1111 修复公路
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目背景
A地區在地震過后,連接所有村莊的公路都造成了損壞而無法通車。
政府派人修復這些公路。
題目描述
給出A地區的村莊數N,和公路數M,公路是雙向的。
并告訴你每條公路的連著哪兩個村莊,并告訴你什么時候能修完這條公路。
問最早什么時候任意兩個村莊能夠通車,即最早什么時候任意兩條村莊都存在至少一條修復完成的道路(可以由多條公路連成一條道路)
輸入格式
第1行兩個正整數N,M
下面M行,每行3個正整數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位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的P1111 修复公路的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P1305 新二叉树
- 下一篇: 阿里云服务器(ECS)CentOS修改y