1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=10005; int n,m,k,father[maxn]; struct Edge { int from,to,dist; Edge(int x=0,int y=0,int v=0):from(x),to(y),dist(v) {} bool operator < (const Edge& b) const { return dist<b.dist; } } edges1[maxn*2],edges2[maxn*2]; int Get_Father(int x) { if(father[x]==x)return x; return father[x]=Get_Father(father[x]); } bool Check(int Limit) { int cnt=0; for(int i=1; i<=n; i++)father[i]=i; for(int i=1; i<=m; i++) { if(edges1[i].dist>Limit)break; int fx=Get_Father(edges1[i].from),fy=Get_Father(edges1[i].to); if(fx!=fy) { father[fx]=fy; cnt++; } } if(cnt<k)return false; for(int i=1; i<=m; i++) { if(edges2[i].dist>Limit)break; int fx=Get_Father(edges2[i].from),fy=Get_Father(edges2[i].to); if(fx!=fy) { father[fx]=fy; cnt++; } } return cnt==n-1; } int main() { n=Get_Int(); k=Get_Int(); m=Get_Int(); for(int i=1; i<=m; i++) { int x=Get_Int(),y=Get_Int(),c1=Get_Int(),c2=Get_Int(); edges1[i]=Edge(x,y,c1); edges2[i]=Edge(x,y,c2); } sort(edges1+1,edges1+m+1); sort(edges2+1,edges2+m+1); int Left=0,Right=300000000; while(Left<=Right) { int mid=(Left+Right)>>1; if(Check(mid))Right=mid-1; else Left=mid+1; } printf("%d\n",Left); return 0; }
|