zoukankan      html  css  js  c++  java
  • CCF/CSP-201812-4-数据中心

     

    题目的意思说白了就是找到一个最小值K,使得用小于等于K的边组成的图是联通的即可。二分这个K判定下就好了。

    第二种做法就是求MST里面的最大边就是答案。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define pii pair<int,int>
     4 #define mp make_pair
     5 const int maxn=500005;
     6 vector<pii>g[maxn];
     7 int n,m,root,tot;
     8 bool vis[maxn];
     9 bool ok(int mid){
    10     memset(vis,0,sizeof(vis));
    11     tot=0;
    12     queue<int>q;
    13     q.push(root);
    14     while(!q.empty()){
    15         int u=q.front();q.pop();
    16         if(vis[u])continue;
    17         vis[u]=1;tot++;
    18         for(pii x:g[u]){
    19             int v=x.first,w=x.second;
    20             if(w>mid||vis[v])continue;
    21             q.push(v);
    22         }
    23     }
    24     return tot==n;
    25 }
    26 int main(){
    27  
    28     int u,v,w;
    29     ios::sync_with_stdio(false);
    30     cin>>n>>m>>root;
    31     while(m--){
    32         cin>>u>>v>>w;
    33         g[u].push_back(mp(v,w));
    34         g[v].push_back(mp(u,w));
    35     }
    36     int l=0,r=1000005;
    37     while(l<r){
    38         int mid=l+(r-l)/2;
    39         if(ok(mid)){
    40             r=mid;
    41         }else{
    42             l=mid+1;
    43         }
    44     }cout<<l<<endl;
    45     return 0;
    46 }
    47 /*
    48 4 5 1
    49 1 2 3
    50 1 3 4
    51 1 4 5
    52 2 3 8
    53 3 4 2
    54 */
  • 相关阅读:
    Django路由控制
    Django杂录
    JQuery
    php-高级计算器
    单项链表
    php解决约瑟夫环
    开灯算法问题
    PHP经典算法之背包问题
    归并排序
    php踩过的那些坑(5)浮点数计算
  • 原文地址:https://www.cnblogs.com/zzqc/p/12442510.html
Copyright © 2011-2022 走看看