标签:
[BZOJ2654]tree
试题描述
输入
输出
输入示例
2 2 1 0 1 1 1 0 1 2 0
输出示例
2
数据规模及约定
见“输入”
题解
记得这道题是 WJMZBMR 神题。。。然而被我用类似“[BZOJ3624][Apio2008]免费道路”这道题的方法 rank 1 捉了。
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack> #include <vector> #include <queue> #include <cstring> #include <string> #include <map> #include <set> using namespace std; const int BufferSize = 1 << 16; char buffer[BufferSize], *Head, *Tail; inline char Getchar() { if(Head == Tail) { int l = fread(buffer, 1, BufferSize, stdin); Tail = (Head = buffer) + l; } return *Head++; } int read() { int x = 0, f = 1; char c = Getchar(); while(!isdigit(c)){ if(c == ‘-‘) f = -1; c = Getchar(); } while(isdigit(c)){ x = x * 10 + c - ‘0‘; c = Getchar(); } return x * f; } #define maxn 50010 #define maxm 100010 int n, m; struct Edge { int u, v, w; bool tp, has; bool operator < (const Edge& t) const { return w < t.w; } } es[maxm]; int fa[maxn]; int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); } void init() { for(int i = 1; i <= n; i++) fa[i] = i; return ; } int main() { n = read(); m = read(); int need = read(); for(int i = 1; i <= m; i++) { int u = read() + 1, v = read() + 1, w = read(), tp = read(); es[i] = (Edge){ u, v, w, tp, 0 }; } sort(es + 1, es + m + 1); init(); for(int i = 1; i <= m; i++) if(es[i].tp) { int u = findset(es[i].u), v = findset(es[i].v); if(u != v) fa[v] = u; } for(int i = 1; i <= m; i++) if(!es[i].tp) { int u = findset(es[i].u), v = findset(es[i].v); if(u != v) fa[v] = u, es[i].has = 1; } init(); int cnt = 0, ans = 0; for(int i = 1; i <= m; i++) if(es[i].has) fa[findset(es[i].v)] = findset(es[i].u), ans += es[i].w, cnt++; for(int i = 1; i <= m && cnt < need; i++) if(!es[i].tp) { int u = findset(es[i].u), v = findset(es[i].v); if(u != v) fa[v] = u, ans += es[i].w, cnt++; } for(int i = 1; i <= m; i++) if(es[i].tp) { int u = findset(es[i].u), v = findset(es[i].v); if(u != v) fa[v] = u, ans += es[i].w; } printf("%d\n", ans); return 0; }
然而最后又被 wzj 神犇用我的方法超过了。。。
标签:
原文地址:http://www.cnblogs.com/xiao-ju-ruo-xjr/p/5688918.html