ZOJ 3204: Connect Them

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367

#include <iostream>
#include <algorithm>
#include <limits>
#include <queue>
using namespace std;
const int MAXN = 105, INF = numeric_limits<int>::max();
typedef pair<int, int> PII;
typedef vector<PII> VPII;
int parent[MAXN];
int T, n, tot;
int Find(int x)//finds the subset of the x-th element and
{
    //updates its predecessors for furture efficiency
    return (parent[x] == x) ? x : parent[x] = Find(parent[x]);
}
void Union(int x, int y)//units two subsets in to a single subset,
{
    //called when they should share the same parent
    parent[Find(x)] = Find(y);
}
struct Edge
{
    int from, to, cost;
    Edge() {};
    Edge(int f, int t, int c) :from(f), to(t), cost(c)
    {

    };
    friend bool operator<(const Edge &e, const Edge &f)
    {
        if (e.cost != f.cost)return e.cost<f.cost;
        if (e.from != f.from)return e.from<f.from;
        return e.to<f.to;
    };
} edge[MAXN*MAXN / 2];
struct cmp
{
    bool operator()(const PII & x, const PII & y)
    {
        if (x.first != y.first)return x.first>y.first;
        return x.second>y.second;
    }
};

priority_queue<PII, VPII, cmp> Q;
bool Kruskal()
{
    /**initialization*/
    for (int i = 1; i <= n; i++) //the index of an element represents the subset it belongs to,
        parent[i] = i;              //so initially itself
    while (!Q.empty())Q.pop();
    sort(edge, edge + tot);
    int counter = 0;
    for (int i = 0; i<tot; i++)
    {
        int u = Find(edge[i].from), v = Find(edge[i].to);
        if (u != v)
        {
            Q.push(make_pair(edge[i].from, edge[i].to));
            parent[u] = v;
            counter++;
        }
    }
    if (counter == n - 1)
        return true;
    return false;
}
int main()
{
    cin >> T;
    while (T--)
    {
        tot = 0;
        cin >> n;
        /**add edges*/
        int t;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
            {
                scanf("%d", &t);
                if (i<j&&t)edge[tot++] = Edge(i, j, t);
            }

        /**Kruskal*/
        if (Kruskal())
        {
            bool f = true;
            while (!Q.empty())
            {
                auto tmp = Q.top();
                if (f)f = false;
                else cout << ‘ ‘;
                printf("%d %d", tmp.first, tmp.second);
                Q.pop();
            }
            printf("\n");
        }
        else printf("-1\n");
    }
    return 0;
}
时间: 2024-11-10 08:20:22

ZOJ 3204: Connect Them的相关文章

ZOJ 3204 Connect them (C) 最小生成树kruskal

Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is the sa

ZOJ 3204 Connect them(最小生成树:kruscal算法)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3204 Connect them Time Limit: 1 Second     Memory Limit:32768 KB You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN).All connecti

ZOJ 3204 Connect them(最小生成树之Krusal 输出字典序最小的)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is

ZOJ 3204 Connect them(最小生成树+最小字典序)

Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers iand j is the sam

zoj 3204 Connect them(最小生成树)

题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define INF 0x7fffffff #define MAXN 128 bool vis[MAXN]; int lowu[MAXN];//记录起始边(已加入集合中的边) int lowc[MAX

ZOJ 3204 Connect them MST-Kruscal

这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的 因为是裸的 Kruscal ,所以就直接上代码了- Source Code : //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring

浙江省第6届程序设计竞赛结题报告汇总 zoj3202-3212

zoj 3202 Second-price Auction 水题,不解释了,直接贴代码 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; struct node{ int x; int y; }; struct node number[105]; int cmp(struct node a,struct node b){

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio