poj1797Heavy Transportation

按说是个水题,不过我用dijkstra一直wa,不知道那错了,然后迫不得已换上kruskal,一发ac,心情复杂。 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <queue>
#include <vector>
#include <algorithm>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define ll long long
#define ld long double
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lcm(a,b) ((a)*(b)/(__gcd((a),(b))))
#define mod 1000000007
#define MP make_pair
#define PI pair<int,int>
using namespace std;
const int N = 1e6 + 10;
int ans, n, m, d[N], t, k = 1;
struct node {
    int x, y, w;
};
vector<node> v;
int cmp(node a, node b) {
    return a.w > b.w;
}
int F(int x) {
    return d[x] == x ? x : d[x] = F(d[x]);
}
int main() {
    scanf("%d", &t);
    while(t--) {
        v.clear(), ans = INF;
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= 1010; i++)
            d[i] = i;
        for(int a, b, c, i = 0; i < m; i++) {
            scanf("%d%d%d", &a, &b, &c);
            v.push_back({a, b, c});
        }
        sort(v.begin(), v.end(), cmp);
        for(int i = 0; i < v.size(); i++) {
            int x = F(v[i].x), y = F(v[i].y);
            if(x != y) {
                ans = min(ans, v[i].w);
                d[x] = y;
            }
            if(F(n) == F(1))
                break;
        }
        printf("Scenario #%d:\n", k++);
        printf("%d\n\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Endeavor_G/article/details/88820989