【解题思路】找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够深厚且模板有错误导致提交错误
 
 #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 ;
}

最新文章

  1. android SQLite数据库总结
  2. 学习zepto.js(原型方法)[2]
  3. [CareerCup] 8.7 Chat Server 聊天服务器
  4. 【WEB API项目实战干货系列】- 接口文档与在线测试(二)
  5. 嵌入式应用中CGI编程中POST、GET及环境变量详解
  6. Python 图形 GUI 库 pyqtgraph
  7. 使用solr报错,错误信息 include(SolrClient.php): failed to open stream: No such file or directory
  8. 电机KV值对应的桨
  9. Flex 按钮添加图标
  10. Windows Phone 同步方式获取网络类型
  11. super在构造函数中的运用
  12. js实用方法记录-简单cookie操作
  13. JavaEE中的MVC(四)AOP代理
  14. 转:java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)
  15. 基于Nginx进行地图瓦片缓存的方案描述
  16. learning mqtt protocol
  17. [USACO5.3]校园网Network of Schools
  18. error C4996: &#39;sprintf&#39;: This function or variable may be unsafe.
  19. [leetcode]Pascal&#39;s Triangle @ Python
  20. Android-监听操作系统短信

热门文章

  1. Introducing Microsoft Sync Framework: Sync Services for File Systems
  2. 2-Medium下的MultipleCommandAssembly
  3. GMT and CST
  4. Python3 学习第十二弹: 补充something
  5. 事件对象event和计时器
  6. 【英语】Bingo口语笔记(11) - 表示“身体抱恙”
  7. ssl创建自签名的https通信
  8. 如何在vmware上创建共享磁盘
  9. cscope 的使用
  10. JS面向对象组件(五) -- 复制对象(拷贝继承)