POJ2485 Highways(最小生成树)
题目链接。
分析:
比POJ2253要简单些。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map> using namespace std; const int maxn = ;
const int INF = (<<); int G[maxn][maxn], d[maxn], n; int prim() {
bool vis[maxn];
memset(vis, false, sizeof(vis)); for(int i=; i<n; i++) {
d[i] = G[][i];
} d[] = ;
vis[] = true; for(int i=; i<n-; i++) {
int m = INF, x;
for(int y=; y<n; y++) if(!vis[y] && m >= d[y]) m = d[x=y];
vis[x] = true;
for(int y=; y<n; y++)
if(!vis[y]) {
int maxx = max(d[x], G[x][y]);
d[y] = min(d[y], maxx);
}
} int ans = ;
for(int i=; i<n; i++) {
ans = max(ans, d[i]);
} return ans;
} int main() {
int T; scanf("%d", &T); while(T--) {
scanf("%d", &n); for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
scanf("%d", &G[i][j]);
}
} printf("%d\n", prim());
} return ;
}
看到有人用其它方法过了,就是prim加了个判断,按着那人思路,再写了一遍。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map> using namespace std; const int maxn = ;
const int INF = (<<); int G[maxn][maxn], d[maxn], n; int prim() {
bool vis[maxn];
memset(vis, false, sizeof(vis)); int maxx = ; for(int i=; i<n; i++) {
d[i] = G[][i];
} d[] = ;
vis[] = true; for(int i=; i<n-; i++) {
int m = INF, x;
for(int y=; y<n; y++) if(!vis[y] && m >= d[y]) m = d[x=y];
vis[x] = true;
if(maxx < m) maxx = m;
for(int y=; y<n; y++) if(!vis[y] && d[y] > G[x][y]) d[y] = G[x][y];
} return maxx;
} int main() {
int T; scanf("%d", &T); while(T--) {
scanf("%d", &n); for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
scanf("%d", &G[i][j]);
}
} printf("%d\n", prim());
} return ;
}
POJ2485 Highways(最小生成树)的更多相关文章
- POJ2485——Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- POJ 2485 Highways 最小生成树 (Kruskal)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ 1751 Highways(最小生成树Prim普里姆,输出边)
题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...
- poj2485 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- spoj 104 Highways (最小生成树计数)
题目链接:http://www.spoj.pl/problems/HIGH/ 题意:求最小生成树个数. #include<algorithm> #include<cstdio> ...
- POJ2485:Highways(模板题)
http://poj.org/problem?id=2485 Description The island nation of Flatopia is perfectly flat. Unfortun ...
随机推荐
- iphone开发中数据持久化之——属性列表序列化(一)
数据持久化是应用程序开发过程中的一个基本问题,对应用程序中的数据进行持久化存储,有多重不同的形式.本系列文章将介绍在iphone开发过程中数据持久化的三种主要形式,分别是属性列表序列号.对象归档化以及 ...
- 在Quick-cocos2dx中使用云风pbc解析Protocol Buffers,支持win、mac、ios、android
本例主要介绍 如何将 pbc 集成到quick-cocos2dx框架中,让我们的cocos2dx客户端Lua拥有编解码Protocol Buffers能力. 参考: 云风pbc的用法: http:// ...
- [C++] [算法] KMP算法
KMP串匹配算法是一个经典的算法. 传统BF算法是传统的字符串匹配算法.很好理解.叶实现.但时间复杂度太高. 本文将从字符串模式字符串被称为.为了匹配字符串被称为主弦. KMP配时能够少移动从串的位置 ...
- 深入懂得android view 生命周期
作为自定义 view 的基础,如果不了解android view 的生命周期 , 那么你将会在后期的维护中发现这样那样的问题 ....... 做过一段时间android 开发的同学都知道,一般 on ...
- Where Jboss7.1 take war application to deploy--reference
Question i've deployed the jboss-as-helloworld-errai application in my standalone jboss7.1 instance, ...
- nginix 笔记
1. 一个master进程,多个worker进程,worker进程数目可自动配置为核的数目 2. 配置文件ngnix.conf存放在linux的/etc/ngnix目录下
- C#学习第四天
今天主要学习了结构方面的知识,首先是定义,代码: struct<typeName> { <memberDeclarations> } struct route { public ...
- Rational rose下载,安装,破解
rationalrose是一个镜像文件,后缀名是bin 之前尝试过用虚拟光驱来打开,不知道为什么,在win10的环境下,虚拟光驱硬是不能加载bin文件,后来拷到虚拟机上,打开了bin镜像文件,得到了一 ...
- 网页嵌入百度地图和使用百度地图api自定义地图的详细步骤
在网页中插入百度地图 如果想在自己的网页上面加入百度地图的话,可以用百度地图的api.具体使用方法如下: 第一步:进入百度创建地图的网站http://api.map.baidu.com/lbsapi/ ...
- PHP 网页爬虫
只能爬一个页面 <?php function get_urls($url){ $url_array=array(); $the_first_content=file_get_contents($ ...