传送门:

http://poj.org/problem?id=1251

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=406

POJ RE死了,改成cin救过了。。不过ZOJ原来的就能过,估计是POJ的数据多了个空格什么的。

1.prim

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; const int MAXN=27;
const int INF=99999;
int dis[MAXN];
int map[MAXN][MAXN];
int n;
void prim()
{
bool vis[MAXN]={0}; for(int i=0;i<n;i++)
dis[i]=INF; int cur=0;
vis[cur]=1;
dis[cur]=0; for(int i=0;i<n;i++)
{
int mini=INF;
for(int j=0;j<n;j++)
if(!vis[j] && map[cur][j] !=INF && dis[j] > map[cur][j])//先选出地图上权值小的
dis[j]=map[cur][j]; for(int j=0;j<n;j++)
if(!vis[j] && mini> dis[j])
mini=dis[cur=j];
vis[cur]=1;
} } int main()
{
while(scanf("%d",&n),n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
map[i][j]=INF; for(int i=0;i < n-1;i++)
{
char temp;
int from,len,to,cost;
cin>>temp>>len;
from=temp-'A';
while(len--)
{
cin>>temp>>cost;
to=temp-'A';
map[from][to]=map[to][from]=cost;
} }
prim();
int sum=0;
for(int i=0;i<n;i++)
sum+=dis[i];
printf("%d\n",sum);
} return 0;
}

下面是ZOJ AC 但是 POJ RE的就是输入格式不同。

#include<cstdio>
#include<cstring>
const int MAXN=27;
const int INF=99999;
int dis[MAXN];
int map[MAXN][MAXN];
int n;
void prim()
{
bool vis[MAXN]={0}; for(int i=0;i<n;i++)
dis[i]=INF; int cur=0;
vis[cur]=1;
dis[cur]=0; for(int i=0;i<n;i++)
{
int mini=INF;
for(int j=0;j<n;j++)
if(!vis[j] && map[cur][j] !=INF && dis[j] > map[cur][j])//先选出地图上权值小的
dis[j]=map[cur][j]; for(int j=0;j<n;j++)
if(!vis[j] && mini> dis[j])
mini=dis[cur=j];
vis[cur]=1;
} } int main()
{
while(scanf("%d",&n),n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
map[i][j]=INF; for(int i=0;i < n-1;i++)
{
getchar();
char temp;
int from,len,to,cost;
scanf("%c %d",&temp,&len);
from=temp-'A';
for(int j=0;j<len;j++)
{
scanf(" %c %d",&temp,&cost);
to=temp-'A';
map[to][from]=map[from][to]=cost;
} }
prim();
int sum=0;
for(int i=0;i<n;i++)
sum+=dis[i];
printf("%d\n",sum);
} return 0;
}

方法2   kruskal

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=28;
const int INF=99999;
int dis[MAXN];
int n,city_cnt,sum;
int fa[MAXN]; struct city
{
int x,y;
int cost;
}a[MAXN*MAXN]; bool operator <(const city &x,const city &y)
{
return x.cost<y.cost;
} int find(int cur)
{
return fa[cur]==cur? cur: fa[cur]=find(fa[cur]);
} int main()
{
while(scanf("%d",&n),n)
{ sum=city_cnt=0;
for(int i=0;i < n-1;i++)
{
char temp;
int from,num,to,cost;
cin>>temp>>num;
from=temp-'A';
while(num--)
{
cin>>temp>>cost;
to=temp-'A';
a[city_cnt].x=from;
a[city_cnt].y=to;
a[city_cnt++].cost=cost;
} } sort(a,a+city_cnt);
for(int i=0;i<n;i++)
fa[i]=i; for(int i=0;i<city_cnt;i++)
{
int root_x=find(a[i].x);
int root_y=find(a[i].y);
if(root_x!=root_y)
{
sum+=a[i].cost;
fa[root_x]=root_y;
}
}
printf("%d\n",sum);
} return 0;
}

POJ 1251 Jungle Roads (zoj 1406) MST的更多相关文章

  1. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  2. POJ 1251 Jungle Roads - C语言 - Kruskal算法

    Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...

  3. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

  4. POJ 1251 Jungle Roads(最小生成树)

    题意  有n个村子  输入n  然后n-1行先输入村子的序号和与该村子相连的村子数t  后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离  求链接全部村子的最短路径 还是裸的最小生成树咯 ...

  5. POJ 1251 Jungle Roads(Kruskal算法求解MST)

    题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...

  6. POJ 1251 Jungle Roads (最小生成树)

    题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...

  7. HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

    双向边,基础题,最小生成树   题目 同题目     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...

  8. POJ 1251 Jungle Roads

    题意:嗯……没看题……看了眼图……求个最小生成树. 解法:kruskal. 代码: #include<stdio.h> #include<iostream> #include& ...

  9. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题六 最小生成树 POJ 1251 Jungle Roads

    题意: 有n个点 每个点上有一些道路 求最小生成树 解释下输入格式 A n v1 w1 v2 w2 A点上有n条边 A到v1权值是w1 A到v2权值是w2 思路: 字符串处理之后跑kruskal求最小 ...

随机推荐

  1. 搜索 debian8.7.1 ,google vs baidu

    国外的 Linux 比国内流行, debian官方网站只能找到当前版本DVD文件.想找旧版的Debian在百度一圈后徒劳无功,于是把目标转向 google ,只需要输入 debian?8.7.1-i3 ...

  2. The program yum-complete-transaction is found in the yum-utils package

    用yum安装的时候出现 The program yum-complete-transaction is found in the yum-utils package. 错误提示的解决方法:# 安装 y ...

  3. 47.__if_not_exists语句

    #include <iostream> using namespace std; template<class T> class myclass { public: T t; ...

  4. Sql延时

    IF EXISTS(SELECT * FROM sys.procedures WHERE name='usp_wait30s')BEGIN DROP PROC usp_wait30sENDgocrea ...

  5. Spring源码分析专题 —— IOC容器启动过程(上篇)

    声明 1.建议先阅读<Spring源码分析专题 -- 阅读指引> 2.强烈建议阅读过程中要参照调用过程图,每篇都有其对应的调用过程图 3.写文不易,转载请标明出处 前言 关于 IOC 容器 ...

  6. js面向对象3-继承

    一.了解继承  首先我们一起了解下js中继承,其实继承就是后辈继承前辈的属性和方法. 二.继承的方法 从父类继承属性和方法 这是对象冒充的方法,模仿java的继承方法.实现的原理是,通过改变父类的执行 ...

  7. NOI2005维修数列(splay)

    题目描述: Description 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格 Input 输入的第1 行包含两个数N 和M( ...

  8. 【Codeforces Round #453 (Div. 2) A】 Visiting a Friend

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 维护最右端的端点就好. [代码] #include <bits/stdc++.h> using namespace st ...

  9. Java开源电商项目比較

    这里比較的都是国外的开源项目,备选项目有: Smilehouse Workspace.Pulse.Shopizer.ofbiz.bigfish.broadleaf 1.Smilehouse Works ...

  10. RocketMQ集群消费的那些事

    说明 RocketMQ集群消费的时候,我们经常看到类似注释里面 (1,(2 的写法,已经有时候有同学没注意抛异常的情况就是(3 模拟的情况.那么这3种情况到底是怎么样的呢?你是否都了然于心呢?下面我们 ...