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>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-8         ;
const int N = 210               ;
const int M = 1100011*2         ;
const ll P = 10000000097ll      ;
const double MINN = -999999999.9;
const int INF = 0x3f3f3f3f      ;
const int MAXN = 110            ;

int root[MAXN];
struct Edge {
    int from,to;
    int w;
} edge[MAXN * MAXN];

int tol;
Edge ans[MAXN * MAXN];
int cnt;

void addedge (int u, int v, int w) {
    edge[tol].from = u;
    edge[tol].to = v;
    edge[tol].w = w;
    ++tol;
}

bool cmp1 (Edge a, Edge b) {
    if(a.w != b.w)    return a.w < b.w;
    else if (a.from != b.from)  return a.from < b.from;
    else return a.to < b.to;
}

bool cmp2 (Edge a, Edge b) {
    if (a.from != b.from)   return a.from < b.from;
    else return a.to < b.to;
}

int find (int x) {
    if (root[x] == -1)  return x;
    return root[x] = find (root[x]);
}

void kruscal () {
    memset (root,-1,sizeof(root));
    cnt = 0;    //加入最小生成树的边
    for (int k = 0; k < tol; ++k) {
        int u = edge[k].from;
        int v = edge[k].to;
        int t1 = find(u);
        int t2 = find(v);
        if(t1 != t2) {
            ans[cnt++] = edge[k];
            root[t1] = t2;
        }
    }
}

int main () {
    int T;
    scanf("%d",&T);
    int n;
    while (T--) {
        scanf ("%d",&n);
        tol = 0;
        int w;
        for (int i = 1; i <= n; ++i) {
          for (int j = 1; j <= n; ++j) {
              scanf("%d",&w);
              if(j <= i || w == 0)    continue;
              addedge(i, j, w);
          }
        }
        sort (edge, edge + tol, cmp1);
        kruscal ();
        if (cnt != n - 1) {
            printf("-1\n");
            continue;
        } else {
            sort (ans, ans + cnt, cmp2);
            for (int i = 0; i < cnt - 1; ++i) {
              printf("%d %d ", ans[i].from, ans[i].to);
            }
            printf("%d %d\n", ans[cnt - 1].from, ans[cnt - 1].to);
        }
    }
    return 0;
}
时间: 2024-08-28 07:05:44

ZOJ 3204 Connect them MST-Kruscal的相关文章

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

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

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 1203 Swordfish (经典MST ~ Kruscal)Boruvka算法

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=203 Description: We all remember that in the movie Swordfish, Gabriel broke into the World Bank Investors Group in West Los Angeles, to rob $9.5 billion. And he needed Stanley, the best ha

浙江省第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){

HDU 4081Qin Shi Huang&amp;#39;s National Road System(最小生成树+最小瓶颈路)

 题意:秦始皇要修路.把N个城市用N-1条边连通.且他希望花费最小,可是这时候有一个多管闲事的道士出来说他有魔法能够帮助秦始皇变成一条路.可是仅仅能变出一条. 可是.两个人对修路的法案存在歧义,道士希望修路能够给很多其它的百姓带来福利.而秦始皇希望修路要尽量使花费小.最后,秦始皇拿出了一个公式A/B.A表示两个城市的人数,B表示出了用魔法变出来的路外.最短的总距离. 如今要你求出A/B的最大值. 思路:枚举连接哪两个城市.由于这条边是免费的.我们要求算上这条边的最小生成树.假设每次都求一次最