zoukankan      html  css  js  c++  java
  • zoj 1542 Network

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1542

    解题思路:最小生成树(kruskal 算法 + 优先队列 + 并查集)

      1 /**************************************************************************
      2 user_id: SCNU20102200088
      3 problem_id: zoj 1542
      4 problem_name: Network
      5 **************************************************************************/
      6 
      7 #include <algorithm>
      8 #include <iostream>
      9 #include <iterator>
     10 #include <iomanip>
     11 #include <sstream>
     12 #include <fstream>
     13 #include <cstring>
     14 #include <cstdlib>
     15 #include <climits>
     16 #include <bitset>
     17 #include <string>
     18 #include <vector>
     19 #include <cstdio>
     20 #include <cctype>
     21 #include <ctime>
     22 #include <cmath>
     23 #include <queue>
     24 #include <stack>
     25 #include <list>
     26 #include <set>
     27 #include <map>
     28 using namespace std;
     29 
     30 //线段树
     31 #define lson l,m,rt<<1
     32 #define rson m+1,r,rt<<1|1
     33 
     34 //手工扩展栈
     35 #pragma comment(linker,"/STACK:102400000,102400000")
     36 
     37 const double EPS=1e-9;
     38 const double PI=acos(-1.0);
     39 const double E=2.7182818284590452353602874713526;  //自然对数底数
     40 const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)
     41 
     42 const int x4[]={-1,0,1,0};
     43 const int y4[]={0,1,0,-1};
     44 const int x8[]={-1,-1,0,1,1,1,0,-1};
     45 const int y8[]={0,1,1,1,0,-1,-1,-1};
     46 
     47 typedef long long LL;
     48 
     49 typedef int T;
     50 T max(T a,T b){ return a>b? a:b; }
     51 T min(T a,T b){ return a<b? a:b; }
     52 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
     53 T lcm(T a,T b){ return a/gcd(a,b)*b; }
     54 
     55 ///////////////////////////////////////////////////////////////////////////
     56 //Add Code:
     57 int n,s[1005];
     58 
     59 struct Node{
     60     int u,v,w;
     61     bool operator <(const Node &a) const{
     62         return w>a.w;
     63     }
     64 }node;
     65 
     66 priority_queue<Node> pq;
     67 
     68 void Union(int x,int y){
     69     s[y]=x;
     70 }
     71 
     72 int Find(int x){
     73     if(s[x]<0) return x;
     74     return s[x]=Find(s[x]);
     75 }
     76 
     77 void Kruskal(){
     78     int num=0,ret=0,i,x[1005],y[1005];
     79     memset(s,-1,sizeof(s));
     80     while(!pq.empty() && num<n-1){
     81         node=pq.top();
     82         pq.pop();
     83         if(Find(node.u)!=Find(node.v)){
     84             Union(Find(node.u),Find(node.v));
     85             ret+=node.w;
     86             num++;
     87             x[num]=node.u;
     88             y[num]=node.v;
     89         }
     90     }
     91     printf("%d
    ",node.w);
     92     printf("%d
    ",num);
     93     for(i=1;i<=num;i++) printf("%d %d
    ",x[i],y[i]);
     94     while(!pq.empty()) pq.pop();
     95 }
     96 ///////////////////////////////////////////////////////////////////////////
     97 
     98 int main(){
     99     std::ios::sync_with_stdio(false);
    100     //freopen("in.txt","r",stdin);
    101     //freopen("out.txt","w",stdout);
    102     ///////////////////////////////////////////////////////////////////////
    103     //Add Code:
    104     int m,i;
    105     while(scanf("%d%d",&n,&m)!=EOF){
    106         for(i=1;i<=m;i++){
    107             scanf("%d%d%d",&node.u,&node.v,&node.w);
    108             pq.push(node);
    109         }
    110         Kruskal();
    111     }
    112     ///////////////////////////////////////////////////////////////////////
    113     return 0;
    114 }
    115 
    116 /**************************************************************************
    117 Testcase:
    118 Input:
    119 4 6
    120 1 2 1
    121 1 3 1
    122 1 4 2
    123 2 3 1
    124 3 4 1
    125 2 4 1
    126 Output:
    127 1
    128 3
    129 1 2
    130 2 4
    131 1 3
    132 **************************************************************************/
  • 相关阅读:
    【WF2017】Mission Improbable
    【Codeforces 837D】Round Subset
    【Codeforces 788C】The Great Mixing
    【JSOI2008】最大数
    2.1图像的数字化
    MATLAB生成随机数
    四六级准考证号忘记了如何快速查询四六级成绩?
    加密与水印结合
    如何在 PyPI安装python的软件包?
    matlab中如何定义函数
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3358538.html
Copyright © 2011-2022 走看看