畅通工程 HDU - 1863 最小生成树模板
两个模板:
kruskal
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = ;
int f[maxn];
int find(int x) {
if (f[x] == x)return x;
else return(f[x] = find(f[x]));
}
bool same(int x, int y) {
return (find(x) == find(y));
}
void un(int x, int y) {
int u = find(x);
int v = find(y);
if (u == v)return;
f[u] = v;
}
struct edge {
int from, to;
long long w;
}e[maxn];
bool cmp(edge a, edge b) {
return a.w < b.w;
}
int main() {
int n, m;
while (cin >> m >> n) {
if (m == )break;
for (int i = ; i < m; i++) {
//int x, y, z;
cin >> e[i].from >> e[i].to >> e[i].w;
}
for (int i = ; i <= n; i++)f[i] = i;
sort(e, e + m, cmp);
int res = ;
for (int i = ; i < m; i++) {
if (same(e[i].from, e[i].to)) continue;
res += e[i].w;
un(e[i].from, e[i].to);
}
for (int i = ; i <= n; i++) {
if (!same(i, ))res = -;
}
if (res == -)cout << '?' << endl;
else cout << res<<endl;
}
}
prim
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = ;
struct edge {
int to;
long long w;
edge(int to = , long long w = ) :to(to), w(w) {}
bool operator<(const edge &a)const {
return w > a.w;
}
};
vector<edge> E[maxn];
bool vis[maxn];
priority_queue<edge> Q; long long prim() {
long long ret = ;
vis[] = ;
int sz = E[].size(); for (int i = ; i < sz; i++) Q.push(E[][i]);
while (!Q.empty()) {
edge t = Q.top(); Q.pop();
if (vis[t.to])continue;
ret += t.w; vis[t.to] = ;
int sz = E[t.to].size(); for (int i = ; i < sz; i++) Q.push(E[t.to][i]);
}
return ret;
} int main() {
int m, n;
while (cin >> m >> n) {
if (m == )break;
for (int i = ; i <= n; i++)E[i].clear(),vis[i]=;
while (!Q.empty()) Q.pop();
for (int i = ; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
E[a].push_back(edge(b, c));
E[b].push_back(edge(a, c));
}
int res = prim();
for (int i = ; i <= n; i++) if (!vis[i]) res = -;
if (res == -)cout << '?' << endl;
else cout << res << endl; }
}
畅通工程 HDU - 1863 最小生成树模板的更多相关文章
- 畅通工程 HDU - 1863
省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道 ...
- HDU 1875:畅通工程再续(最小生成树)
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 1875 畅通工程再续 (最小生成树)
畅通工程再续 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/M Description 相信大家都听说一个"百岛湖&q ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- HDU - 1232 畅通工程-并查集模板
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...
- hdu 1874 畅通工程续(SPFA模板)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU-1232/NYOJ-608畅通工程,并查集模板题,,水过~~~
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) http://acm. ...
- L - 还是畅通工程 - hdu 1233
Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达 ...
- 畅通工程 HDU - 1232
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接 ...
随机推荐
- [Unity官方文档翻译]Primitive and Placeholder Objects Unity原生3D物体教程
Primitive and Placeholder Objects 原始的基础物体 Unity can work with 3D models of any shape that can be cre ...
- RPM常用命令解释
RPM软件包管理器,英文:RPM Package Manager(原Red Hat Package Manager,现在是一个递归缩写) -i安装rpm包 -u升级rpm包 -q查询已安装的软件信息 ...
- SpringBoot application.properties (application.yml)优先级从高到低
SpringBoot application.properties(application.yml) 优先级从高到低 SpringBoot配置文件优先级从高到低 =================== ...
- hbase2.0.0-安装部署
依赖hadoop 环境,我这边的版本是hadoop-2.6.5 选择hbase2.0.0版本的时候,去官网查看支持的hadoop版本 1.伪分布式安装 下载:http://mirror.bit.edu ...
- Django 添加应用
一个项目可以添加多个应用,可以使用以下两种方法来添加应用: [root@localhost web]$ python manage.py startapp blog [root@localhost w ...
- Splash jsfunc() 方法
jsfunc()方法可以直接调用 JavaScript 定义的方法,但是所调用的方法需要用双中括号包围,这相当于实现了 JavaScript 方法到 Lua 脚本的转换 function main(s ...
- Splash 对象属性
args js_enabled resource_timeout images_enabled plugins_enabled scroll_position
- chattr lsattr
chattr命令的用法:chattr [ -RVf ] [ -v version ] [ mode ] files…最关键的是在[mode]部分,[mode]部分是由+-=和[ASacDdIijsTt ...
- EXCEL通俗易懂讲公式(一):sumif,sumifs,countif,countifs
最近公司招了一批新人,excel基本都是小白阶段,以前用过的也就是画个课程表,没做过什么数据统计和文本计算等工作.因此各种问题都来了,什么vlookup,offset,连条件求和的sumif也不会用, ...
- vue-resource使用 (vue仿百度搜索)
1.this.$http.get()方法2.this.$http.post()方法3.this.$http.jsonp()方法 (vue仿百度搜索) 在输入框中输入a, 然后在百度f12 ==> ...