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

POJ 2395 Out of Hay(MST)的更多相关文章

  1. POJ 2395 Out of Hay (prim)

    题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. B ...

  2. POJ 2395 Out of Hay( 最小生成树 )

    链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* & ...

  3. POJ 2395 Out of Hay(求最小生成树的最长边+kruskal)

    Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18472   Accepted: 7318 Descr ...

  4. POJ 2395 Out of Hay(最小生成树中的最大长度)

    POJ 2395 Out of Hay 本题是要求最小生成树中的最大长度, 无向边,初始化es结构体时要加倍,别忘了init(n)并查集的初始化,同时要单独标记使用过的边数, 判断ans==n-1时, ...

  5. 瓶颈生成树与最小生成树 POJ 2395 Out of Hay

    百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶 ...

  6. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  7. COJ 0500 杨老师的路径规划(MST)最小生成树

    杨老师的路径规划(MST) 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 为满足同学们需求,杨老师在实验楼4层新建了好多个计算 ...

  8. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  9. POJ.3087 Shuffle'm Up (模拟)

    POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...

随机推荐

  1. BZOJ 2751 容易题

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2751 题意:有一个数列A已知对于所有的A[i]都是1到n的自然数,并且知道对于一些A[i ...

  2. SQL[连载2]语法及相关实例

    SQL[连载2]语法及相关实例 SQL语法 数据库表 一个数据库通常包含一个或多个表.每个表由一个名字标识(例如:"Websites"),表包含带有数据的记录(行). 在本教程中, ...

  3. php面试题整理

    PHP经典面试题:(不断跟进补充中...) 1.用PHP打印出前一天的时间格式是2009-02-10 22:21:21(2分) echo date('Y-m-d H:i:s', strtotime(' ...

  4. 注意map<> 的[]

    其实在之前一篇关于map的基本操作中已经提到过注意[]操作,这里再强调一下. 先看下面的程序: #include<iostream> #include<map> using n ...

  5. 网易新闻页面信息抓取 -- htmlagilitypack搭配scrapysharp

    最近在弄网页爬虫这方面的,上网看到关于htmlagilitypack搭配scrapysharp的文章,于是决定试一试~ 于是到https://www.nuget.org/packages/Scrapy ...

  6. jpa+spring配置多数据源

    property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?useU ...

  7. 最全的PHP常用函数大全

    PHP的一些常用函数 quotemeta() 函数在字符串中某些预定义的字符前添加反斜杠. quoted_printable_decode() 函数对经过 quoted-printable 编码后的字 ...

  8. Qt之QLabel

    简述 QLabel提供了一个文本或图像的显示,没有提供用户交互功能. 一个QLabel可以包含以下任意内容类型: 内容 设置 纯文本 使用setText()设置一个QString 富文本 使用setT ...

  9. POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)

    题意:有n个站排成一列,针对每个站的位置与距离关系,现有多个约束条件,约束条件分两种:(1)确定的.明确说明站a距离站b多少个单位距离.(2)不确定的.只知道a在b的左边至少1个单位距离.  根据已知 ...

  10. *ecshop安装模板

    1. 安装模板 例: 新建abc.com文件 复制default下的style.css 修改两个http://www.ecshop.com/ 为 http://www.hua.com/ 复制defau ...