其他分享
首页 > 其他分享> > jzyz 1225 调查干草

jzyz 1225 调查干草

作者:互联网

描述 Description
奶牛用完了干草,这是一件必须马上补救的可怕事情. Bessie打算看N (2 <= N <=2,000)个农场(标号为1…N)去调查干草的情况.她将走过一些或全部M (1 <= M <=10,000)条连接着农场长度都不大于1,000,000,000的双向通路.一些农场可能被多条不同通路连接.
Bessie尝试决定她需要的水瓶的大小.她知道她在路上每走一个单位长度需要1个单位的水.因为她可以在每个农场得到更多的水,她只关心最长那条路的长度.她打算把她必须带的水减到最少.
帮Bessie计算她必须带的最多的水:即Bessie 走过最长的路最短,输出这个最小的数.
输入格式 Input Format
*第1行:两个整数用空格隔开:N和M
*第2…M+1行:每一行包括3个数Ai,Bi,Li,表示从Ai农场到Bi农场有条路径长度为Li.
输出格式 Output Format
*第一行:一个整数表示必须经过的最长的路.

题解

emm
跑一遍最小生成树就行了

code

//愉快的放代码 我又水了一篇博客

/*
o              o     ooooo      o            o                  ooooo            oooooooo           ooooooo
oo            oo       o        o            o                 o     o           o      o           o
o o          o o       o        o            o                o       o          o      o           o
o  o        o  o       o        o            o               o         o         oooooooo           o
o   o      o   o       o        o            o               o         o         o                  ooooooo
o    o    o    o       o        o            o               o         o         o                  o
o     o  o     o       o        o            o                o       o          o                  o     
o      oo      o       o        o            o                 o     o           o                  o
o       o      o     ooooo      oooooooooo   oooooooooo         ooooo            o                  ooooooo
*/

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <iomanip>    
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define up(i, a, b) for (i = a; i <= b; ++i)
#define down(i, a, b) for (i = a; i >= b; --i)
typedef long long ull;
const int maxn = 1e6 + 1000;
const int inf = 0x3f3f3f3f;

inline int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    return s * w;
}

int n, m;
int ans, num_edge = 0;
int f[maxn];
struct Tree {
    int from, to, dis;
    bool operator<(const Tree &a) const {
        return dis < a.dis;
    }
} t[maxn];

inline int find(int k) {
    return f[k] == k ? k : f[k] = find(f[k]);
} 

int main() {
    n = read(), m = read();
    for (int i = 1; i <= m; ++i) {
        t[i].from = read();
        t[i].to = read();
        t[i].dis = read();
    }
    std::sort(t + 1, t + m + 1);
    for (int i = 1; i <= n; ++i) f[i] = i;
    for (int i = 1; i <= m; ++i) {
        int u = find(t[i].from), v = find(t[i].to);
        if (u == v) continue;
        else f[u] = v;
        if (ans < t[i].dis) ans = t[i].dis;
    }
    printf("%d\n", ans);
    // system("PAUSE");
    return 0;
}

标签:oo,jzyz,1225,ch,int,ooooo,Bessie,include,干草
来源: https://blog.csdn.net/qq_34493840/article/details/88025119