

VIJOS-P1045 Kerry的电缆网络(并查集)
source link: https://arminli.com/vijos-p1045/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

VIJOS-P1045 Kerry的电缆网络(并查集)
February 24, 2016
按照长度排序,然后并查集即可。
#include<cstring> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; int fa[100005]; int n; double ans, S; struct edge{ int x, y; double l; }E[100005]; int findfa(int x){ if(fa[x] != x){ fa[x] = findfa(fa[x]); } return fa[x]; } void add(int x, int y, double k){ if(x > y) swap(x, y); x = findfa(x); y = findfa(y); if(x != y){ fa[y] = x; ans += k; } } bool cmp(edge a, edge b){ return a.l < b.l; } int main(){ //freopen("a.txt", "r", stdin); int m = 1; scanf("%lf %d", &S, &n); while(scanf("%d %d %lf", &E[m].x, &E[m].y, &E[m].l) != EOF){ m++; }m--; for(int i = 1; i <= n; i++) fa[i] = i; sort(E+1, E+m+1, cmp); ans = 0; for(int i = 1; i <= m; i++){ add(E[i].x, E[i].y, E[i].l); } bool flag = 0; int x = findfa(1); for(int i = 2; i <= n; i++){ if(findfa(i) != x){ flag = 1; break; } } if(ans-S > 1e-6 || flag) cout << "Impossible" << endl; else printf("Need %.2f miles of cablen", ans); return 0; }
Recommend
-
5
...
-
7
Rest-Framework源码解析和自定义组件 | Kerry Rest-Framework源码解析和自定义组件 ...
-
6
Python零散的小知识点 | Kerry Python零散的小知识点 ...
-
3
异步非阻塞IO模块 | Kerry 异步非阻塞IO模块 ...
-
5
最近又看了看tornado框架,写了一个自定义的组件。一直没时间上传,今天终于可以和大家见面了,对了,大家在和本文亲密之前需要先预热一下哦~:stuck_out_tongue::stuck_out_tongue::stuck_out_tongue:
-
3
进程和线程分析下面就两者的区别和联系分析如下: 进程 线程 定义 资源(CPU时间、内存等)分配的最小单位 程序执行的最小单位 区别 1.独立的地址空间...
-
7
你真的了解WebSocket吗? WebSocket协议是基于TCP的一种新的协议。WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符。它实现了浏览器与服务器全双工(full-duplex)通信。其本质是保持TCP连接,在浏览器和服务端通过Socket...
-
9
Climate PolicyJohn Kerry says clean energy transition is the largest market the...
-
9
Google TV rolling out new ‘Watch With Me’ collection from Scandal’s Kerry Washington August 18, 2022
-
16
Raspberry Pi 5:PoE HAT板提供一根用于网络和电源的电缆,并提供12伏的附件 admin 2024年02月12日 09:40 Raspberry Pi 5 是一款多...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK