POJ 2395 Out of Hay(MST)
2024-08-24 21:10:43
【解题思路】找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够深厚且模板有错误导致提交错误
#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 ;
}
最新文章
- android SQLite数据库总结
- 学习zepto.js(原型方法)[2]
- [CareerCup] 8.7 Chat Server 聊天服务器
- 【WEB API项目实战干货系列】- 接口文档与在线测试(二)
- 嵌入式应用中CGI编程中POST、GET及环境变量详解
- Python 图形 GUI 库 pyqtgraph
- 使用solr报错,错误信息 include(SolrClient.php): failed to open stream: No such file or directory
- 电机KV值对应的桨
- Flex 按钮添加图标
- Windows Phone 同步方式获取网络类型
- super在构造函数中的运用
- js实用方法记录-简单cookie操作
- JavaEE中的MVC(四)AOP代理
- 转:java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)
- 基于Nginx进行地图瓦片缓存的方案描述
- learning mqtt protocol
- [USACO5.3]校园网Network of Schools
- error C4996: &#39;sprintf&#39;: This function or variable may be unsafe.
- [leetcode]Pascal&#39;s Triangle @ Python
- Android-监听操作系统短信