【无标题】问题 I: 繁忙的都市

题目描述城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造。城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条

#include<bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define sf(a) scanf("%d\n",&a)
#define sft(a,b) scanf("%d%d",&a,&b)
#define go(a) while(cin>>a&&a)
#define ms(a,b) memset(a,b,sizeof a)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pf(a) printf("%.8lf",a)
#define valu first
#define num second
#define E 1e-8
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int idata = 10000 + 5;
int n, m, t;
int maxx, minn;
int i, j, k;
int flag, sum;
int fa[idata];
struct point
{
    int x, y, fee;
    const bool operator<(const point& b)
        const {
        return fee < b.fee;
    }
}a[idata];
int Find(int son)
{
    return son == fa[son] ? son : fa[son] = Find(fa[son]);
}
void Union(int x, int y)
{
    x = Find(x), y = Find(y);
    if (x != y)
        fa[y] = x;
}

int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    while (cin >> n >> m)
    {
        int tot = 0;
        for (i = 0; i < m; i++)
        {
            int x, y, z;
            cin >> x >> y >> z;
            a[i].x = x, a[i].y = y, a[i].fee = z;
            tot += z;
        }
        for (i = 1; i <= n; i++) fa[i] = i;
        sort(a, a + m);
        for (i = 0; i < m; i++)
        {
            if (Find(a[i].x) != Find(a[i].y))
            {
                Union(a[i].x, a[i].y);
                flag++;
                maxx = max(a[i].fee, maxx);
            }
            if (flag == n - 1) break;
        }
        cout << flag << " " << maxx << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值