当前位置:网站首页>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.
没想好,就知道无脑敲代码
边栏推荐
- openresty 高可用部署
- 你想知道的 Watch App 开发
- Is OnePlus Ace worth buying?Use strength to interpret the power of performance
- EasyNTS上云网关断电重启后设备离线是什么原因?
- 荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide
- 借助Web3盘活日本优质IP:UneMeta 与 OpenSea 的差异化竞争
- Postgresql 备份大小情况!
- 【美丽天天秒】链动2+1模式开发
- 借助kubekey极速安装Kubernetes
- 技术干货|如何将 Pulsar 数据快速且无缝接入 Apache Doris
猜你喜欢
随机推荐
PHP基础笔记-NO.1
使用安全浏览器将网页保存为pdf的方法步骤
WEB 渗透之CSRF
[Azure Event Hub] Create Event Hub Consume Client + Custom Event Position with Azure AD Authentication
荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide
WPF 实现柱形统计图
es6新增-Promise详解(异步编程的解决方案1)
WEB 渗透之SSRF
程序员如何分分钟搞垮一个项目?
H.265网页播放器EasyPlayer获取视频流正常,但是播放出现黑屏是什么原因?
【Deliberately practice the view of the back tube】deliberately practice
Cyanine5.5 alkyne|Cy5.5 alkyne|1628790-37-3|Cy5.5-ALK
细胞不可渗透的荧光探针 锌离子荧光探针Zinquin 151606-29-0
云图说丨初识华为云微服务引擎CSE
87.(cesium之家)cesium热力图(贴地形)
ImportError: /lib/libgdal.so.26: undefined symbol: sqlite3_column_table_name
Atomic Wallet已支持TRC20-USDT
理想L9旗舰级的安全性有多强?守护一家人安全出行“底线”
动态打印菱形
InnoDB 中不同SQL语句设置的锁