zoj 1542 Network

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

struct data
{
    int u,v;
    int  w;
} e[15000+5];

bool cmp(data a, data b)
{
    return a.w<b.w;
}

int n,m,bin[5000];

int Find(int x)
{
    int s;
    for(s=x; bin[s]>=0; s=bin[s]);

    while(s!=x)
    {
        int t=bin[x];
        bin[x]=s;
        x=t;
    }
    return s;
}

void Union(int x1,int x2)
{
    int f1=Find(x1),f2=Find(x2);
    int t=bin[f1]+bin[f2];
    if(bin[f1]>bin[f2])
    {
        bin[f1]=f2;
        bin[f2]=t;
    }
    else
    {
        bin[f2]=f1;
        bin[f1]=t;
    }

}

int main()
{
    int i,j,num,u,v,ans[1000+24],maxe;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0) break;
        for(i=0; i<=n; i++)
            bin[i]=-1;

        for(i=0; i<m; i++)
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);

        sort(e,e+m,cmp);
        num=0;
        maxe=0;
        for(i=0; i<m; i++)
        {
            u=e[i].u;
            v=e[i].v;
            if(Find(u)!=Find(v))
            {
                ans[num]=i;
                Union(u,v);
                num++;
                maxe=max(maxe,e[i].w);
            }
            if(num>=n-1) break;
        }

        printf("%d\n",maxe);
        printf("%d\n",num);
        for(i=0; i<num; i++)
        {
            printf("%d %d\n",e[ans[i]].u,e[ans[i]].v);
        }

    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-21 16:38:08

zoj 1542 Network的相关文章

POJ 1861 &amp; ZOJ 1542 Network(最小生成树之Krusal)

题目链接: PKU:http://poj.org/problem?id=1861 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=542 Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c

POJ 1861 &amp;amp; ZOJ 1542 Network(最小生成树之Krusal)

题目链接: PKU:http://poj.org/problem?id=1861 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=542 Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c

ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

题目连接:ZOJ 1542 POJ 1861 Network 网络 Network Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, t

zoj 2676 Network Wars(最小割,01分数规划)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2676 大致题意:给出一个带权无向图,每条边有一个边权wi,求将S和T分开的一个割边集C,使得该割边集的平均边权最小,即最小化∑wi / |C| . 详见amber关于最小割模型的论文 思路:amber论文中详细讲解了如何转化成函数及建图,值得注意的是当边被重新赋权后,对于wi < 0 的边权,该边必然在最小割中,不必再建边,直接加入最大流中即可,因为求最小割时边权都为正值

ZOJ 2676 Network Wars(最优比例最小割)

Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge Network of Byteland consists of n servers, connected by m optical cables. Each cable connects two servers and can transmit data in both directions. Two servers of the n

ZOJ 2676 Network Wars[01分数规划]

ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge Network of Byteland consists of n servers, connected by m optical cables. Each cable connects two servers and can transmit data in both direction

zoj——1311 Network

Network Time Limit: 2 Seconds      Memory Limit: 65536 KB A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines

POJ 1459 &amp;amp;&amp;amp; ZOJ 1734--Power Network【最大流dinic】

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25108   Accepted: 13077 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

ZOJ QS Network (prime_裸题)

In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they need to buy two network adapters (one for each QS) and a segment of network cable.