标签:
今天老师不在,不用考试,开森,去刷题,然而扁桃体炎那就是我自己作死了
题目简述:在某个城市中,有这样一坨罪犯,他们可能在彼此间有怨恨值,这个城市里有两个监狱,可供你放置他们,一旦有怨恨值的两个人相遇,就会造成相应的影响,要你计算所造成的最小影响值
今天咱家身体不好,不易太多废话,我们直接开始,我们对于所存入的关系可看作两点一权值边,那我们根据w排序一边后,类似kruskal的贪心的连边即可。
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> using namespace std; typedef long long ll; struct edge{ int a,b,w; }; const int Size = 2e4 + 4; const int size = 1e5 + 5; int father[Size << 1]; edge data[size]; bool cmp(edge,edge); int getfather(int); int main(){ int n,m,ans = 0; scanf("%d%d",&n,&m); for (int i = 0;i < m; ++i) scanf("%d%d%d",&data[i].a,&data[i].b,&data[i].w); sort(data,data + m,cmp); for (int i = n << 1;i; --i) father[i] = i; for (int i = 0;i < m; ++i){ int x = getfather(data[i].a); int y = getfather(data[i].b); if (x == y){ ans = data[i].w; break; } father[x] = getfather(data[i].b + n); father[y] = getfather(data[i].a + n); } printf("%d\n",ans); return 0; } bool cmp(edge a,edge b){ return a.w > b.w; } int getfather(int x){ if (father[x] == x) return x; return father[x] = getfather(father[x]); }
标签:
原文地址:http://www.cnblogs.com/GENEVE/p/4819846.html