poj 1861 Network MST

简介:

    期末后第一次写题,结果就是这么灵异的一题……

    样例是错的,这题实际上就是求个最小生成树,spj


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define INF 1E9
using namespace std;
struct edge
{
    int f,t,v;
};
bool cmp(edge a,edge b)
{
    return a.v<b.v;
}
edge e[15005];
int fa[1005];
int far(int nn)
{
    if(fa[nn]<0)return nn;
    return fa[nn]=far(fa[nn]);
}
int main()
{
    memset(fa,-1,sizeof(fa));
    int n,m,f,t,v;
    scanf("%d%d",&n,&m);
    int i;
    for(i=0;i<m;i++)
        scanf("%d%d%d",&e[i].f,&e[i].t,&e[i].v);
    sort(e,e+m,cmp);
    int now,k,Max;
    int ans[1000];
    for(k=Max=0,now=1;now<n;k++)
    {
        f=far(e[k].f);t=far(e[k].t);
        if(f==t)continue;
        Max=max(Max,e[k].v);
        if(fa[f]<fa[t])//加权法则,避免退化
        {
            fa[f]+=fa[t];
            fa[t]=f;
        }
        else
        {
            fa[t]+=fa[f];
            fa[f]=t;
        }
        ans[now]=k;
        now++;
    }
    printf("%d\n",Max);
    printf("%d\n",now-1);
    for(i=1;i<now;i++)
     printf("%d %d\n",e[ans[i]].f,e[ans[i]].t);

}


目录
相关文章
[POJ 1236] Network of Schools | Tarjan缩点
Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”).
115 0
[POJ 1236] Network of Schools | Tarjan缩点
|
算法 数据建模
【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点
题目链接:http://poj.org/problem?id=1236 题意:给定一个表示n所学校网络连通关系的有向图。现要通过网络分发软件,规则是:若顶点u,v存在通路,发给u,则v可以通过网络从u接收到。
1150 0
uva 1329 Corporative Network
点击打开链接uva 1329 思路: 带权并查集 分析: 1 看懂题目就是切菜了 代码: #include #include #include #include using namespace std; const int MAXN...
862 0
uva 1267 Network
点击打开链接uva 1267 思路:先把无根树转化为有根树然后找深度最大的点进行dfs 分析: 1 首先我们应该先把这个无根树转化为有根树,然后我们就可以知道每一个叶子节点相对与根节点的距离 2 接下来我们考虑一下深度最大的节点,假设当前的节点u是深度最大的节点,那么我们可以知道u的k级祖先(父亲是1级,父亲的父亲是2级)处放置服务器肯定比1~k-1任何的一级都优。
887 0