标签:lan bsp 输出 stream 开发 isl names end node
题目描述
输入
输出
一个数据,表示花费最大的公路的花费。
样例输入
4 2 5 1 2 6 5 1 3 3 1 2 3 9 4 2 4 6 1
样例输出
6
题解
这是一道大水题
第一眼二分答案,然而并不用。先按c1排序,求出前k个,然后按c2排序,求出剩下的即可。
#include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define ll long long const int maxn=10000+50; const int maxm=20000*2+50; int n,m,k,C,x,y,maxx,fat[maxn]; struct node{int x,y,c1,c2;}a[maxm]; int cmp1(const node &a,const node &b){return a.c1<b.c1;} int cmp2(const node &a,const node &b){return a.c2<b.c2;} template<typename T>void read(T& aa){ char cc; ll ff;aa=0;cc=getchar();ff=1; while((cc<‘0‘||cc>‘9‘)&&cc!=‘-‘) cc=getchar(); if(cc==‘-‘) ff=-1,cc=getchar(); while(cc>=‘0‘&&cc<=‘9‘) aa=aa*10+cc-‘0‘,cc=getchar(); aa*=ff; } int father(int x){ if(x!=fat[x]) fat[x]=father(fat[x]); return fat[x]; } void un(int x,int y){ int fa=father(x),fb=father(y); fat[fa]=fb; } int main(){ read(n),read(k),read(m); for(int i=1;i<=n;i++) fat[i]=i; for(int i=1;i<=m-1;i++) read(a[i].x),read(a[i].y),read(a[i].c1),read(a[i].c2); sort(a+1,a+1+m,cmp1); int t=0; for(int i=1;i<=m;i++){ int fa=father(a[i].x),fb=father(a[i].y); if(fa!=fb){ t++; un(a[i].x,a[i].y); maxx=max(maxx,a[i].c1); } if(t==k) break; } sort(a+1,a+1+m,cmp2); t=0; for(int i=1;i<=m;i++){ int fa=father(a[i].x),fb=father(a[i].y); if(fa!=fb){ t++; un(a[i].x,a[i].y); maxx=max(maxx,a[i].c2); } if(t==n-k-1) break; } cout<<maxx<<endl; return 0; }
标签:lan bsp 输出 stream 开发 isl names end node
原文地址:https://www.cnblogs.com/rlddd/p/9667989.html