• CODEVS 1638 修复公路


    题目描述 Description

    A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车。政府派人修复这些公路。

    给出A地区的村庄数N,和公路数M,公路是双向的。并告诉你每条公路的连着哪两个村庄,并告诉你什么时候能修完这条公路。问最早什么时候任意两个村庄能够通车,即最早什么时候任意两条村庄都存在至少一条修复完成的道路(可以由多条公路连成一条道路)

    输入描述 Input Description

    第1行两个正整数N,M(N<=1000,M<=100000)

    下面M行,每行3个正整数x, y, t,告诉你这条公路连着x,y两个村庄,在时间t时能修复完成这条公路。(x<=N,y<=N,t<=100000)

    输出描述 Output Description

    如果全部公路修复完毕仍然存在两个村庄无法通车,则输出-1,否则输出最早什么时候任意两个村庄能够通车。

    样例输入 Sample Input

    4 4

    1 2 6

    1 3 4

    1 4 5

    4 2 3

    样例输出 Sample Output

    5

    解题思路

    这是一道灰常灰常裸的最短路,用并查集优化的克鲁斯卡尔就好。。。呼呼呼

     1 program RebuildPath;
     2 type paa=record
     3 l,r,t:longint;
     4 end;
     5 var
     6 ro:array[1..1000] of longint;
     7 pa:array[1..100000] of paa;
     8 m,n,i,j,ans,cheak:Longint;
     9 function root(i:longint):longint;
    10 begin
    11     if ro[i]=i then exit(i);
    12     root:=root(ro[i]);
    13     ro[i]:=root;
    14     exit(root);
    15 end;
    16 
    17 procedure union(x,y:Longint);
    18 begin
    19     ro[root(x)]:=root(y);
    20 end;
    21 
    22 procedure sort(l,r: longint);
    23       var
    24          i,j,x: longint;
    25          y:paa;
    26       begin
    27          i:=l;
    28          j:=r;
    29          x:=pa[(l+r) div 2].t;
    30          repeat
    31            while pa[i].t<x do
    32             inc(i);
    33            while x<pa[j].t do
    34             dec(j);
    35            if not(i>j) then
    36              begin
    37                 y:=pa[i];
    38                 pa[i]:=pa[j];
    39                 pa[j]:=y;
    40                 inc(i);
    41                 j:=j-1;
    42              end;
    43          until i>j;
    44          if l<j then
    45            sort(l,j);
    46          if i<r then
    47            sort(i,r);
    48       end;
    49 begin
    50     read(n,m);
    51     for i:=1 to n do ro[i]:=i;
    52     for i:=1 to m do
    53     begin
    54         read(pa[i].l,pa[i].r,pa[i].t);
    55     end;
    56     sort(1,m);
    57     for i:=1 to m do
    58     if (root(pa[i].l))<>root(pa[i].r) then
    59     begin
    60         union(root(pa[i].l),root(pa[i].r));
    61         ans:=pa[i].t;
    62     end;
    63     cheak:=root(1);
    64     for i:=1 to n do if root(i)=cheak then continue else
    65     begin
    66         writeln(-1);
    67         halt;
    68     end;
    69     writeln(ans);
    70 end.
  • 相关阅读:
    最新的Delphi版本号对照
    SuperObject生成示例
    Why does Delphi XE7 IDE hangs and fails on out of memory exception?
    使用 TRESTClient 与 TRESTRequest 作为 HTTP Client(转)
    Delphi提取PDF文本
    (3)PyCharm中Flask工程逆向生成数据库表
    (2)PyCharm开发Flash项目之蓝图构建
    (1)PyCharm开发工具安装Flask并创建helloworld程序
    使用localStorage写一个简单的备忘录
    一个Redis实例适合存储不同应用程序的数据吗?
  • 原文地址:https://www.cnblogs.com/wuminyan/p/4738256.html
Copyright © 2020-2023  润新知