一开始还觉得很难,结果。一发过了。
其实观察可以发现就是一个最小生成树。
对于前k条边,我们去连最小生成树,显然这时比较的是自己想的花费。
然后剩下的就是老师讲的花费比较,再去连最小生成树。
// Author: levil #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int,int> pii; const int N = 1e4+5; const int M = 2e4+5; const LL Mod = 1e9+7; #define rg register #define pi acos(-1) #define INF 1e9 #define CT0 cin.tie(0),cout.tie(0) #define IO ios::sync_with_stdio(false) #define dbg(ax) cout << "now this num is " << ax << endl; namespace FASTIO{ inline LL read(){ LL x = 0,f = 1;char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();} return x*f; } void print(int x){ if(x < 0){x = -x;putchar('-');} if(x > 9) print(x/10); putchar(x%10+'0'); } } using namespace FASTIO; void FRE(){/*freopen("data1.in","r",stdin); freopen("data1.out","w",stdout);*/} struct Node{int st,to,dis1,dis2;}e[M]; bool cmp1(Node a,Node b){return a.dis1 < b.dis1;} bool cmp2(Node a,Node b){return a.dis2 < b.dis2;} int fa[N]; int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); } int main() { int n,k,m;n = read(),k = read(),m = read(); for(int i = 1;i <= n;++i) fa[i] = i; for(int i = 1;i <= m;++i) e[i].st = read(),e[i].to = read(),e[i].dis1 = read(),e[i].dis2 = read(); sort(e+1,e+m+1,cmp1); int num = k,mx = -1; for(int i = 1;i <= m && num > 0;++i) { int x = e[i].st,y = e[i].to,dis = e[i].dis1; int xx = Find(x),yy = Find(y); if(xx != yy) { fa[xx] = yy; mx = max(mx,dis); num--; } } sort(e+1,e+m+1,cmp2); for(int i = 1;i <= m;++i) { int x = e[i].st,y = e[i].to,dis = e[i].dis2; int xx = Find(x),yy = Find(y); if(xx != yy) { fa[xx] = yy; mx = max(mx,dis); } } printf("%d ",mx); system("pause"); }