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 ...
 
随机推荐
- HDOJ 5073 Galaxy 数学 贪心
			
贪心: 保存连续的n-k个数,求最小的一段方差... .预处理O1算期望. .. Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
 - 一、cocos2d-x 3.0 final使用httpclient编译到android,须要用到的android.mk
			
今天写一个网络框架,在vs上面非常欢快的执行车,心想,尼玛!cocos2d-x 3.0这么方便,预计不久的将来我就能回家种地了,由于不用程序猿了,直接cocos2dstudio拖界面了= =!!. 写 ...
 - nodejs这个过程POST求
			
下面是一个web登陆模拟过程.当我们问一个链接,你得到一个表格,然后填写相应的表格值,然后提交登陆. var http = require('http'); var querystring = req ...
 - 从零单排c++ primer(17)
			
(1)假设一个名字在派生类的作用域内无法正确解析,则编译器将继续在外层的基类作用域中寻找该名字的定义. (2)派生类的成员将隐藏同名的基类成员. (3)除了覆盖继承而来的虚函数之外,派生类最好不要重用 ...
 - CDOJ 92 Journey(LCA&RMQ)
			
题目连接:http://acm.uestc.edu.cn/#/problem/show/92 题意:给定一棵树,最后给加一条边,给定Q次查询,每次查询加上最后一条边之后是否比不加这条边要近,如果近的话 ...
 - android中的文件操作详解以及内部存储和外部存储(转载)
			
原文链接:http://m.blog.csdn.net/article/details?id=17725989 摘要 其实安卓文件的操作和java在pc环境下的操作并无二致,之所以需要单独讲解是因为安 ...
 - 对话框 自定义 IOS风格 包青天
			
activity private void showDialog1() { message = "您输入的邮箱后缀不是公司邮箱地址\n将导致您的借款审核不通过,请重新 ...
 - C#中的占位符
			
当我们需要在屏幕上输出一句话的时候,如果不断的使用+来连接各个字符串,一是容易出错,二是代码显示的非常乱.这时候,占位符就能够发挥作用! 占位符: string name="张三" ...
 - Linq101-Set
			
using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Set { / ...
 - 关于WCF一些基础。
			
关于WCF Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .N ...