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 ...
随机推荐
- sparkSQL1.1入门之二:sparkSQL执行架构
在介绍sparkSQL之前.我们首先来看看,传统的关系型数据库是怎么执行的.当我们提交了一个非常easy的查询: SELECT a1,a2,a3 FROM tableA Where con ...
- android中Canvas使用drawBitmap绘制图片
1.主要的绘制图片方法 //Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置 drawBitmap(Bitmap bitmap, float left, float ...
- 如何开通www国际域名个人网站
欢迎访问我的域名http://www.baiqiantao.xyz 一.准备知识 什么是域名? 所有以www开头的域名,就是国际域名:不以www开头的域名,都是为子域名. 域名都有后缀,后缀是有意义的 ...
- HttpClient4.0
****************************HttpClient4.0用法***************************** 1.初始化HttpParams,设置组件参数 //Ht ...
- DEDECMS批量修改默认文章和列表命名规则的方法
很多人因为添加分类而苦恼,尤其是批量添加的时候,必须要重新修改一下文章命名规则和列表命名规则,都是为了做SEO.如果进行默认值的修改,就会事半功倍.不多说. 一.DEDE5.5修改默认文章命名规则. ...
- GridView布局及适配器优化
1.布局样式 <GridView android:id="@+id/gridView" android:layout_width="fill_parent" ...
- urlwrite伪静态(SAE、PHP、JSP)
在SAE里,直接配置config.yaml文件,文件可以配置的内容包含: 目录默认页面 自定义错误页面 压缩 页面重定向 页面过期 设置响应Header的Content-Type appname: x ...
- PLSQL远程连接到Oracle服务器
这里只介绍一种远程连接服务器方法,即本机安装了Oracle客户端和PLSql工具,服务器安装在虚拟机或者另一台电脑上 1.打开Oracle客户端的Net Manager,选择Oracle Net配置— ...
- js单条新闻向上滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- 多核CPU利用测试
一直在想程序上是否特意让线程在指定的CPU上去运行,这样可以提高运行效率,所以特地写个代码让CPU使用率画正弦曲线的实验,我使用的是AMD X4 641的CPU,为四核四线程的片子. 代码如下 # ...