POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net
http://poj.org/problem?id=1258
水题。
题目就是让你求MST,连矩阵都给你了。
prim版
#include<cstdio>
const int MAXN=101;
const int INF=100000+10;
int map[MAXN][MAXN];
int dis[MAXN];
int n; void prim()
{
bool vis[MAXN]={0};
for(int i=0;i<=n;i++)
dis[i]=INF; int cur=1;
vis[cur]=1;
dis[cur]=0; for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(!vis[j] && dis[j] > map[cur][j])
dis[j]=map[cur][j]; int mini=INF;
for(int j=1;j<=n;j++)
if(!vis[j] && dis[j] <mini)
mini=dis[cur=j]; vis[cur]=1;
}
} int main()
{ while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&map[i][j]); prim();
int ans=0;
for(int i=1;i<=n;i++)
ans+=dis[i]; printf("%d\n",ans);
} return 0;
}
kruskal
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=101;
const int INF=100000+10;
int fa[MAXN];
int n; struct data
{
int x,y;
int dis;
}a[MAXN*MAXN];
bool operator< (const data& c,const data &d)
{
return c.dis<d.dis;
} int find(int cur)
{
return cur==fa[cur]? cur:fa[cur]=find(fa[cur]);
} int main()
{ while(~scanf("%d",&n))
{
int len=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&a[len].dis);
a[len].x=i;
a[len].y=j;
len++;
} sort(a,a+len);
int ans=0;
for(int i=1;i<=n;i++)
fa[i]=i; for(int i=0;i<len;i++)
{
int rootx=find(a[i].x);
int rooty=find(a[i].y);
if(rootx!=rooty)
{
fa[rootx]=rooty;
ans+=a[i].dis;
} }
printf("%d\n",ans);
} return 0;
}
-------------------------------------------------我是可爱的分割线-------------------------------------------------
poj2485 Highways
http://poj.org/problem?id=2485
题目要求求MST上最长的边。
上面的代码一改就过了。。。。
prim
#include<cstdio>
const int MAXN=520;
const int INF=100000+10;
int map[MAXN][MAXN];
int dis[MAXN];
int n; void prim()
{
bool vis[MAXN]={0};
for(int i=0;i<=n;i++)
dis[i]=INF; int cur=1;
vis[cur]=1;
dis[cur]=0; for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(!vis[j] && dis[j] > map[cur][j])
dis[j]=map[cur][j]; int mini=INF;
for(int j=1;j<=n;j++)
if(!vis[j] && dis[j] <mini)
mini=dis[cur=j]; vis[cur]=1;
}
} int main()
{ int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&map[i][j]); prim();
int ans=0;
for(int i=1;i<=n;i++)
{
if(ans < dis[i])
ans=dis[i];
} printf("%d\n",ans);
} return 0;
}
kruskal
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=520;
const int INF=100000+10;
int fa[MAXN];
int n; struct data
{
int x,y;
int dis;
}a[MAXN*MAXN];
bool operator< (const data& c,const data &d)
{
return c.dis<d.dis;
} int find(int cur)
{
return cur==fa[cur]? cur:fa[cur]=find(fa[cur]);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int len=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&a[len].dis);
a[len].x=i;
a[len].y=j;
len++;
} sort(a,a+len);
int ans=0;
for(int i=1;i<=n;i++)
fa[i]=i; for(int i=0;i<len;i++)
{
int rootx=find(a[i].x);
int rooty=find(a[i].y);
if(rootx!=rooty)
{
fa[rootx]=rooty;
ans=a[i].dis;
} }
printf("%d\n",ans);
} return 0;
}
POJ 1258 Agri-Net|| POJ 2485 Highways MST的更多相关文章
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258
#include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- POJ 1258 Agri-Net(Prim算法求解MST)
题目链接: http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- poj 2485 Highways (最小生成树)
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...
- POJ 2485 Highways && HDU1102(20/200)
题目链接:Highways 没看题,看了输入输出.就有种似曾相识的感觉,果然和HDU1102 题相似度99%,可是也遇到一坑 cin输入居然TLE,cin的缓存不至于这么狠吧,题目非常水.矩阵已经告诉 ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
随机推荐
- 一个Web报表项目的性能分析和优化实践(七):性能监测工具JavaMelody
简介 JavaMelody 能够监测Java或Java EE应用程序服务器,并以图表的方式显示:Java内存和Java CPU使用情况,用户Session数量,JDBC连接数,和http请求.sql请 ...
- Spring Cloud Sleuth通过Kafka将链路追踪日志输出到ELK
1.工程简介 elk-eureka-server作为其他三个项目的服务注册中心 elk-kafka-client调用elk-kafka-server,elk-kafka-server再调用elk-ka ...
- IAR FOR STM8 学习笔记 固件库 GPIO
经过一番挣扎,还是决定使用官方的固件库了.. 从网上下一个STM8S的固件库,记得是FOR IAR的. 找到里面的IAR模板就可以开始用了. 这些都是直接写好的库函数,可以直接调用,但首先得先读懂,先 ...
- Codefroces 784 愚人节题目(部分)
A. Numbers Joke time limit per test 2 seconds memory limit per test 64 megabytes input standard inpu ...
- js全选反选按钮实现
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Leetcode:populating_next_right_pointers_in_each_node题解
一. 题目 对于结构体:struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...
- redhat6.5安装10201解决办法
rpm --import /etc/pki/rpm-gpg/RPM*yum install -y --skip-broken compat-libstdc++* elfutils-libelf* g ...
- [appium]-9宫格解锁方法
from appium.webdriver.common.touch_action import TouchAction TouchAction(self.driver).press(x=228,y= ...
- hibernate 注解配置<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/X
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Android圆环控件
Android圆环控件 近期在做一个功能.界面效果要求例如以下: 看到这个界面,我首先想到了曾经在做phone模块的时候,我们定制的来电界面InCallTouchUi,界面效果是相似的. 来电控件使用 ...