《洛谷P2798 爆彈虐場》
阿新 • • 發佈:2020-09-13
一開始還覺得很難,結果。一發過了。
其實觀察可以發現就是一個最小生成樹。
對於前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 #defineView Codepi 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 namespaceFASTIO; 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\n",mx); system("pause"); }