Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhhf96
C-Plus-Plus-TheAlgorithms
提交
91ce393c
C
C-Plus-Plus-TheAlgorithms
项目概览
zhhf96
/
C-Plus-Plus-TheAlgorithms
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
C-Plus-Plus-TheAlgorithms
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
91ce393c
编写于
8月 16, 2020
作者:
F
Filip Hlasek
提交者:
GitHub
8月 17, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: linter for kruskal (#1036)
* fix: linter for kruskal * using instead of typedef.
上级
d8b4da68
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
31 addition
and
82 deletion
+31
-82
graph/kruskal.cpp
graph/kruskal.cpp
+31
-82
未找到文件。
graph/kruskal.cpp
浏览文件 @
91ce393c
#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
//#include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
const
int
mx
=
1e6
+
5
;
const
long
int
inf
=
2e9
;
typedef
long
long
ll
;
#define rep(i, n) for (i = 0; i < n; i++)
#define repp(i, a, b) for (i = a; i <= b; i++)
#define pii pair<int, int>
#define vpii vector<pii>
#define vi vector<int>
#define vll vector<ll>
#define r(x) scanf("%d", &x)
#define rs(s) scanf("%s", s)
#define gc getchar_unlocked
#define pc putchar_unlocked
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
using
namespace
std
;
void
in
(
int
&
x
)
{
register
int
c
=
gc
();
x
=
0
;
int
neg
=
0
;
for
(;
((
c
<
48
||
c
>
57
)
&&
c
!=
'-'
);
c
=
gc
())
;
if
(
c
==
'-'
)
{
neg
=
1
;
c
=
gc
();
}
for
(;
c
>
47
&&
c
<
58
;
c
=
gc
())
{
x
=
(
x
<<
1
)
+
(
x
<<
3
)
+
c
-
48
;
}
if
(
neg
)
x
=
-
x
;
}
void
out
(
int
n
)
{
int
N
=
n
,
rev
,
count
=
0
;
rev
=
N
;
if
(
N
==
0
)
{
pc
(
'0'
);
return
;
}
while
((
rev
%
10
)
==
0
)
{
count
++
;
rev
/=
10
;
}
rev
=
0
;
while
(
N
!=
0
)
{
rev
=
(
rev
<<
3
)
+
(
rev
<<
1
)
+
N
%
10
;
N
/=
10
;
}
while
(
rev
!=
0
)
{
pc
(
rev
%
10
+
'0'
);
rev
/=
10
;
}
while
(
count
--
)
pc
(
'0'
);
}
ll
parent
[
mx
],
arr
[
mx
],
node
,
edge
;
vector
<
pair
<
ll
,
pair
<
ll
,
ll
>>>
v
;
using
ll
=
int64_t
;
std
::
array
<
ll
,
mx
>
parent
;
ll
node
,
edge
;
std
::
vector
<
std
::
pair
<
ll
,
std
::
pair
<
ll
,
ll
>>>
edges
;
void
initial
()
{
int
i
;
rep
(
i
,
node
+
edge
)
parent
[
i
]
=
i
;
for
(
int
i
=
0
;
i
<
node
+
edge
;
++
i
)
{
parent
[
i
]
=
i
;
}
}
int
root
(
int
i
)
{
while
(
parent
[
i
]
!=
i
)
{
parent
[
i
]
=
parent
[
parent
[
i
]];
...
...
@@ -75,41 +23,42 @@ int root(int i) {
}
return
i
;
}
void
join
(
int
x
,
int
y
)
{
int
root_x
=
root
(
x
);
// Disjoint set union by rank
int
root_y
=
root
(
y
);
parent
[
root_x
]
=
root_y
;
}
ll
kruskal
()
{
ll
mincost
=
0
,
i
,
x
,
y
;
rep
(
i
,
edge
)
{
x
=
v
[
i
].
second
.
first
;
y
=
v
[
i
].
second
.
second
;
ll
mincost
=
0
;
for
(
int
i
=
0
;
i
<
edge
;
++
i
)
{
ll
x
=
edges
[
i
].
second
.
first
;
ll
y
=
edges
[
i
].
second
.
second
;
if
(
root
(
x
)
!=
root
(
y
))
{
mincost
+=
v
[
i
].
first
;
mincost
+=
edges
[
i
].
first
;
join
(
x
,
y
);
}
}
return
mincost
;
}
int
main
()
{
fast
;
while
(
1
)
{
int
i
,
j
,
from
,
to
,
cost
,
totalcost
=
0
;
cin
>>
node
>>
edge
;
// Enter the nodes and edges
if
(
node
==
0
&&
edge
==
0
)
while
(
true
)
{
int
from
=
0
,
to
=
0
,
cost
=
0
,
totalcost
=
0
;
std
::
cin
>>
node
>>
edge
;
// Enter the nodes and edges
if
(
node
==
0
&&
edge
==
0
)
{
break
;
// Enter 0 0 to break out
}
initial
();
// Initialise the parent array
rep
(
i
,
edge
)
{
cin
>>
from
>>
to
>>
cost
;
v
.
pb
(
mp
(
cost
,
mp
(
from
,
to
)));
for
(
int
i
=
0
;
i
<
edge
;
++
i
)
{
std
::
cin
>>
from
>>
to
>>
cost
;
edges
.
emplace_back
(
make_pair
(
cost
,
std
::
make_pair
(
from
,
to
)));
totalcost
+=
cost
;
}
sort
(
v
.
begin
(),
v
.
end
());
// rep(i,v.size())
// cout<<v[i].first<<" ";
cout
<<
kruskal
()
<<
endl
;
v
.
clear
();
sort
(
edges
.
begin
(),
edges
.
end
());
std
::
cout
<<
kruskal
()
<<
std
::
endl
;
edges
.
clear
();
}
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录