首页 技术 正文
技术 2022年11月18日
0 收藏 650 点赞 3,099 浏览 1199 个字

【题目链接】http://poj.org/problem?id=2395【解题思路】找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够深厚且模板有错误导致提交错误 

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define INF 1000000002
#define MAXN 2002
#define SIZE 20200 using namespace std; int eh[MAXN], tot, dist[MAXN];
bool visit[MAXN];
int n, m;
struct Edge{
int v, u, cost, next;
Edge(){}
Edge(int a, int b, int c, int d){
v = a, u = b, cost = c, next = d;
}
Edge(int a, int b){v = a, cost = b;}
bool operator < (const Edge &x) const{
return cost > x.cost;
}
};
struct Edge edge[SIZE]; int prim(int s)
{
for(int i = ; i <= n; ++i) visit[i] = false, dist[i] = INF;
dist[s] = ;
priority_queue<Edge> que;
que.push(Edge(s, ));
int ans = ;
while(!que.empty())
{
Edge tmp = que.top();
que.pop();
int u = tmp.v, cost = tmp.cost;
if(visit[u]) continue;
if(cost > ans) ans = cost;
visit[u] = true;
for(int i=eh[u]; i != -; i = edge[i].next)
{
int v = edge[i].u;
if(!visit[v] && dist[v] > edge[i].cost)
{
dist[v] = edge[i].cost;
que.push(Edge(v, dist[v]));
}
}
}
return ans;
} void addedge(int a, int b, int c)
{
Edge e = Edge(a, b, c, eh[a]);
edge[tot] = e;
eh[a] = tot++;
} void init()
{
tot = ;
memset(eh, -, sizeof(eh));
} int main()
{
scanf("%d%d", &n, &m);
init();
for(int i = ; i <= m; ++i)
{
int u, v, cost;
scanf("%d%d%d", &u, &v, &cost);
addedge(v, u, cost);
addedge(u, v, cost);
}
printf("%d\n", prim());
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918