bzoj 1196

http://www.lydsy.com/JudgeOnline/problem.php?id=1196

二分+并查集

一共有2*M条路径,我们首先将这2*M条路径按费用排序。

然后二分最大费用的公路mid

变成判断性问题:能否只用第1到第mid条公路,使得生成树至少包含K条一级公路。

因为这时候已经跟费用无关了,我们优先选取一级公路即可。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

inline int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
inline LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }

const int maxN=10000;
const int maxM=2*20000;

int N,K,M;
struct Troad
  {
      int x,y,id,p,cost;
        inline Troad(int _id=0,int _p=0,int _x=0,int _y=0,int _cost=0){id=_id;p=_p;x=_x;y=_y;cost=_cost;}    
  }road[maxM+100];

inline bool cmpcost(Troad a,Troad b){return a.cost<b.cost;}

int pa[maxN+100];
inline int findroot(int a){return pa[a]<0?a:pa[a]=findroot(pa[a]);}
inline void uni(int a,int b)
  {
      int f1=findroot(a),f2=findroot(b);
      if(f1==f2)return;
      if(pa[f1]>pa[f2])swap(f1,f2);
      pa[f1]+=pa[f2];
      pa[f2]=f1;
  }

inline int check(int mid)
  {
      int i,cnt=0;
      mmst(pa,-1);
      re(i,1,mid)
          if(road[i].p==1)
            if(findroot(road[i].x)!=findroot(road[i].y))
              {
                  uni(road[i].x,road[i].y);
                  cnt++;
              }
        if(cnt<K)return 0;
        re(i,1,mid)uni(road[i].x,road[i].y);
        re(i,1,N)if(findroot(i)!=findroot(1))return 0;
        return 1;
    }

struct Tout
  {
      int id,p;
      inline Tout(int _id=0,int _p=0){id=_id;p=_p;}
    }out[maxN+10];
int ge;
inline bool cmpid(Tout a,Tout b){return a.id<b.id;}

int main()
  {
      freopen("road.in","r",stdin);
      freopen("road.out","w",stdout);
      int i;
      N=gint();K=gint();M=gint();
      re(i,1,M)
        {
            int x=gint(),y=gint(),c1=gint(),c2=gint();
            road[2*i-1]=Troad(i,1,x,y,c1);
            road[2*i]=Troad(i,2,x,y,c2);
        }
      M*=2;
      sort(road+1,road+M+1,cmpcost);
      int l=1,r=M,mid;
      while(l<=r)
          {
              mid=(l+r)>>1;
                if(check(mid))r=mid-1;else l=mid+1;
            }
      cout<<road[l].cost<<endl;
      mmst(pa,-1);
      re(i,1,l)
          if(road[i].p==1)
            if(findroot(road[i].x)!=findroot(road[i].y))
              {
                  uni(road[i].x,road[i].y);
                  out[++ge]=Tout(road[i].id,road[i].p);
              }
        re(i,1,l)if(findroot(road[i].x)!=findroot(road[i].y))
          {
              uni(road[i].x,road[i].y);
                out[++ge]=Tout(road[i].id,road[i].p);
            }
        sort(out+1,out+ge+1,cmpid);
        re(i,1,ge)cout<<out[i].id<<" "<<out[i].p<<endl;
        return 0;
  }
View Code

 

posted @ 2015-08-21 14:09  maijing  阅读(215)  评论(0编辑  收藏  举报