POJ - 2377 - Bad Cowtractors (最小生成树)

题目链接:https://vjudge.net/problem/POJ-2377#author=tsacm123

题目大意:就是让你算出n个谷仓之间的最大生成树,然后把各条边的值累加起来

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl ‘\n‘
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) fill(a, a+maxn, INF);
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, int> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == ‘-‘)f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 1e3+10;
int vis[maxn];
vector<P> dis[maxn];
void prim(int n) {
    priority_queue<P> pq;
    zero(vis);
    int sum = 0, s = 0;
    pq.push(make_pair(0, 1));
    while(!pq.empty()) {
        P t = pq.top();
        pq.pop();
        if (vis[t.second])
            continue;
        vis[t.second] = true;
        sum += t.first;
        ++s;
        for (int i = 0; i<(int)dis[t.second].size(); ++i)
            if (!vis[dis[t.second][i].second])
                pq.push(make_pair(dis[t.second][i].first, dis[t.second][i].second));
    }
    if (s != n)
        printf("-1\n");
    else
        printf("%d\n", sum);
}
int main(void) {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0, a, b, d; i<m; ++i) {
        scanf("%d%d%d", &a, &b, &d);
        dis[a].push_back(make_pair(d, b));
        dis[b].push_back(make_pair(d, a));
    }
    prim(n);
    return 0;
}

原文地址:https://www.cnblogs.com/shuitiangong/p/12384759.html

时间: 2024-10-21 16:52:33

POJ - 2377 - Bad Cowtractors (最小生成树)的相关文章

poj 2377 Bad Cowtractors

Bad Cowtractors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10194   Accepted: 4333 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N

POJ 2377 Bad Cowtractors【最大生成树,Prime算法】

Bad Cowtractors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13628   Accepted: 5632 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N

poj - 2377 Bad Cowtractors(最大生成树)

http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她想要花费越多越好,并且任意两个农场都需要连通,并且不能存在环.后面两个条件保证最后的连通图是一棵树. 输出最小花费,如果没办法连通所有节点输出-1. 最大生成树问题,按边的权值从大道小排序即可,kruskal算法可以处理重边的情况,但是在处理的时候,不能仅仅因为两个节点在同一个连通子图就判断图不合法

poj 2377 Bad Cowtractors(最大生成树!)

Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection route

POJ 2377 Bad Cowtractors (Kruskal)

题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-1. 思路:将边权降序再Kruskal,再检查一下是否只有一棵树即可,即根节点只有一个 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; int N, M; // 节点,边的数量 struct edge { int from, to, dist;

poj 2485 Highways(最小生成树)

题目链接:http://poj.org/problem?id=2485 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're plann

POJ 2349 Arctic Network 最小生成树题解

本题也是使用Prime和Kruskal都可以的最小生成树的题解. 本题一点新意就是:需要除去最大的S-1个距离,因为可以使用卫星覆盖这些距离. 技巧:建图建有向图,速度快点,不用计算两边. 这里使用Prime,因为是稠密图. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <al

poj 1258 Agri-Net(最小生成树果题)

题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed

poj 1258 Agri-Net (最小生成树 prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39499   Accepted: 16017 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee