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 ...
随机推荐
- Android 免费短信获取国家列表和国家代码
StringBuffer str = new StringBuffer(); for (Map.Entry<Character, ArrayList<String[]>> en ...
- HDU--2040
亲和数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 细说Lucene源码(一):索引文件锁机制
大家都知道,在多线程或多进程的环境中,对统一资源的访问需要特别小心,特别是在写资源时,如果不加锁,将会导致很多严重的后果,Lucene的索引也是如此,lucene对索引的读写分为IndexReader ...
- [PWA] 12. Intro to IndexedDB
Use the library indexedDB-promised. Create a database and stroe: import idb from 'idb'; // Open(db_n ...
- Junit使用教程(一)
几乎所有程序员都听说过Junit的大名,但不知真正懂得运用它的人有多少,我便是其中的一个小白. 知道Junit是用来测试的,但却把“宝刀”当成了“菜刀”用.为了从此不再菜鸟,特此总结整理了下Junit ...
- jboss7 Java API for RESTful Web Services (JAX-RS) 官方文档
原文:https://docs.jboss.org/author/display/AS7/Java+API+for+RESTful+Web+Services+(JAX-RS) Content Tuto ...
- log4j配置文件详解---转
使用步骤注意: 1. 从http://logging.apache.org/log4j/1.2/ 下载文件 2. 在src目录下加入log4j.properties,将jar包放入build -pat ...
- POJ 2007 Scrambled Polygon 凸包
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7214 Accepted: 3445 ...
- LINQ的基本用法
1.var q =from c in db.Customers select c.ContactName; 这个语句只是一个声明或者一个描述,并没有真正把数据取出来,只有当你需要该数据的时候,它才会执 ...
- Struts2 删除后直接直接到List显示页面
package com.sun; import java.util.List; import java.util.Map; import org.hibernate.Session; import o ...