当前位置:网站首页>POJ 2377 Bad Cowtractors(最大生成树)
POJ 2377 Bad Cowtractors(最大生成树)
2022-08-03 18:23:00 【51CTO】
题目地址: 点击打开链接
思路:就是求最大生成树,即把n个点都连起来且不会出现回路,并且花费最大,刚开是思维定式了,把边赋初值为一个最大值,结果调了半天,比赛完了5分钟就A了,把初始边都赋值为0,每次就最大边加入,还有就是图类问题要注意多重边的可能,这道题要选最大的边
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
typedef
long
long
ll;
using
namespace
std;
const
int
maxn
=
1e3
+
10;
int
map1[
maxn][
maxn];
int
lowdist[
maxn];
int
visit[
maxn];
int
n,
m,
sum;
bool
flag;
void
Kruskal()
{
memset(
visit,
0,
sizeof(
visit));
int
i,
j,
k;
for(
i
=
1;
i
<=
n;
i
++)
{
lowdist[
i]
=
map1[
1][
i];
}
visit[
1]
=
1;
for(
i
=
1;
i
<
n;
i
++)
//要并入n个点所以要循环n次
{
int
max2
=
0;
k
=
0;
for(
j
=
1;
j
<=
n;
j
++)
{
if(
!
visit[
j]
&&
lowdist[
j]
>
max2)
{
max2
=
lowdist[
j];
k
=
j;
}
}
if(
k
==
0)
{
flag
=
false;
return;
}
visit[
k]
=
1;
sum
+=
lowdist[
k];
for(
j
=
1;
j
<=
n;
j
++)
{
if(
!
visit[
j]
&&
map1[
k][
j]
>
lowdist[
j])
{
lowdist[
j]
=
map1[
k][
j];
}
}
}
}
int
main()
{
int
i;
while(
scanf(
"%d%d",
&
n,
&
m)
!=
EOF)
{
memset(
map1,
0,
sizeof(
map1));
int
a,
b,
c;
for(
i
=
1;
i
<=
m;
i
++)
{
scanf(
"%d%d%d",
&
a,
&
b,
&
c);
if(
c
>
map1[
a][
b])
{
map1[
a][
b]
=
map1[
b][
a]
=
c;
}
}
flag
=
true;
sum
=
0;
Kruskal();
if(
flag)
{
printf(
"%d\n",
sum);
}
else
{
printf(
"-1\n");
}
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
错误代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
typedef
long
long
ll;
using
namespace
std;
const
int
zui
=
1e6
+
10;
const
int
maxn
=
1e3
+
10;
int
map1[
maxn][
maxn];
int
lowdist[
maxn];
int
visit[
maxn];
int
n,
m,
sum;
bool
flag;
void
Kruskal()
{
memset(
visit,
0,
sizeof(
visit));
int
i,
j,
k;
for(
i
=
1;
i
<=
n;
i
++)
{
lowdist[
i]
=
map1[
1][
i];
}
visit[
1]
=
1;
for(
i
=
1;
i
<
n;
i
++)
//要并入n个点所以要循环n次
{
int
max2
=
0;
k
=
0;
for(
j
=
1;
j
<=
n;
j
++)
{
if(
!
visit[
j]
&&
lowdist[
j]
>
max2
&&
lowdist[
j]
<=
100000)
{
max2
=
lowdist[
j];
k
=
j;
}
}
if(
k
==
0)
{
flag
=
false;
return;
}
visit[
k]
=
1;
sum
+=
lowdist[
k];
for(
j
=
1;
j
<=
n;
j
++)
{
if(
!
visit[
j]
&&
map1[
k][
j]
>
lowdist[
j]
&&
map1[
k][
j]
<=
100000)
{
lowdist[
j]
=
map1[
k][
j];
}
}
}
}
int
main()
{
int
i,
j;
while(
scanf(
"%d%d",
&
n,
&
m)
!=
EOF)
{
for(
i
=
1;
i
<=
n;
i
++)
{
for(
j
=
i
+
1;
j
<=
n;
j
++)
{
map1[
i][
j]
=
map1[
j][
i]
=
zui;
}
map1[
i][
i]
=
0;
}
int
a,
b,
c;
for(
i
=
1;
i
<=
m;
i
++)
{
scanf(
"%d%d%d",
&
a,
&
b,
&
c);
map1[
a][
b]
=
map1[
b][
a]
=
c;
}
flag
=
true;
sum
=
0;
Kruskal();
if(
flag)
{
printf(
"%d\n",
sum);
}
else
{
printf(
"-1\n");
}
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
没想好,就知道无脑敲代码
边栏推荐
猜你喜欢

Crack: WebKitX ActiveX and WebKitX VHX

yaml数据格式

技术开发人员常用的安全浏览器

Jenkins CI平台(二)

多商户商城系统功能拆解21讲-平台端分销订单

每周推荐短视频:为了填补学习资源的空缺,作者专门写了本书?

Online monitoring of UPS power supply and operating environment in the computer room, the solution is here

架构基本概念和架构本质

在线监控机房内的UPS电源及运行环境,解决方案来了

87. (Home of cesium) cesium heat map (topography)
随机推荐
【Deliberately practice the view of the back tube】deliberately practice
Blender script 删除所有幽灵对象
【HCIP】MPLS实验
Arduino实验三:继电器实验
细胞不可渗透的荧光探针 锌离子荧光探针Zinquin 151606-29-0
ImportError: /lib/libgdal.so.26: undefined symbol: sqlite3_column_table_name
2022/08/02------Ugly number
STM32——LCD—FSMC原理简介
你想知道的 Watch App 开发
Atomic Wallet已支持TRC20-USDT
图像传感第一章学习心得
Intelligent security contract - delegatecall (2)
Redis:哨兵
pydev debugger: warning: trying to add breakpoint to file that does not exist: /tmp/xxx
每周推荐短视频:为了填补学习资源的空缺,作者专门写了本书?
B628芯片电路图,B628升压IC的PCB布局PCB
ASA归因:如何评估关键词的投放价值
剑指Offer 56.数组中数字出现的次数
域名抢注“卷”到了表情包?ENS逆势上涨的新推力
Share 14 JS functions you must know