POJ 1251 Jungle Roads (zoj 1406) MST
传送门:
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的更多相关文章
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- POJ 1251 Jungle Roads - C语言 - Kruskal算法
Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 1251 Jungle Roads(最小生成树)
题意 有n个村子 输入n 然后n-1行先输入村子的序号和与该村子相连的村子数t 后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离 求链接全部村子的最短路径 还是裸的最小生成树咯 ...
- 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 ...
- POJ 1251 Jungle Roads (最小生成树)
题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- POJ 1251 Jungle Roads
题意:嗯……没看题……看了眼图……求个最小生成树. 解法:kruskal. 代码: #include<stdio.h> #include<iostream> #include& ...
- [ 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求最小 ...
随机推荐
- [React] Define defaultProps and PropTypes as static methods in class component
class Toggle extends Component { static propTypes = { defaultOn: PropTypes.bool, on: PropTypes.bool, ...
- Linux下进程终止过程
不管是在什么系统中,当进程终止之后.系统都须要释放进程占有的资源. 否则.系统资源会被耗尽. 以下将具体说明Linux系统中,进程终止的过程. 进程终止方式 linux的进程终止方式有8种,当中5种是 ...
- 25.C++多线程
#include <iostream> #include <thread> #include <Windows.h> using namespace std; vo ...
- 一句SQL按照某个字段数值拆分出对应的数据条数,借助数据库常量表【master..spt_values】实现
简介:master..spt_values,数据行拆分简单小技巧 SELECT ProjGUID , CostGUID , SUM(FtAmount) AS FtAmount , BeginMonth ...
- 【agc014d】Black and White Tree
又是被虐的一天呢~(AC是不可能的,这辈子不可能AC的.做题又不会做,就是打打暴力,才能维持骗骗分这样子.在机房里的感觉比回家的感觉好多了!里面个个都是大佬,个个都是死宅,我超喜欢在里面的!) (↑以 ...
- 洛谷 P3654 First Step (ファーストステップ)
洛谷 P3654 First Step (ファーストステップ) https://www.luogu.org/problemnew/show/P3654 题目描述 可是……这个篮球场,好像很久没有使用过 ...
- SQl 事务增加数据
连SQl简单地事务处理多表的情况 begin transaction declare @error int declare @QID int set @error = 0 insert into HW ...
- springmvc hibernate整合
今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多 了,Spring-Security的就留在下一篇吧,这篇主 ...
- Hypervisor scheduler
Techniques for configuring a hypervisor scheduler to make use of cache topology of processors and ph ...
- tomcat 服务形式检测
http://blog.chinaunix.net/uid-20449851-id-2369842.html