poj 1251 Jungle Roads(最小生成树)
来源:互联网 发布:秦美人四灵进阶数据 编辑:程序博客网 时间:2024/06/10 05:25
思路:最小生成树模板。
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int INF=0x3f3f3f3f;const int MAXN=110;bool vis[MAXN];int lowc[MAXN];int Prim(int cost[][MAXN],int n){ int ans=0; memset(vis,false,sizeof(vis)); vis[0]=true; for(int i=1; i<n; i++)lowc[i]=cost[0][i]; for(int i=1; i<n; i++) { int minc=INF; int p=-1; for(int j=0; j<n; j++) if(!vis[j]&&minc>lowc[j]) { minc=lowc[j]; p=j; } if(minc==INF)return -1;//原图不连通 ans+=minc; vis[p]=true; for(int j=0; j<n; j++) if(!vis[j]&&lowc[j]>cost[p][j]) lowc[j]=cost[p][j]; } return ans;}int main(){ int cost[MAXN][MAXN]; int N; int u,v,w; while(~scanf("%d",&N)) { if(N==0)break; for(int i=0; i<MAXN; ++i) { for(int j=0; j<MAXN; ++j) { cost[i][j]=INF; } } int M=N-1; char c; int t; for(int i=0; i<M; ++i) { cin>>c>>t; u=c-'A'; while(t--) { cin>>c>>w; v=c-'A'; cost[u][v]=cost[v][u]=w; } } printf("%d\n",Prim(cost,N)); } return 0;}
0 0
- POJ 1251 Jungle Roads 最小生成树
- poj 1251 Jungle Roads ---最小生成树
- Poj 1251 Jungle Roads -- 最小生成树
- poj 1251 Jungle Roads 最小生成树
- POJ 1251 Jungle Roads(最小生成树)
- poj 1251 Jungle Roads 最小生成树
- Jungle Roads - POJ 1251 最小生成树
- POJ-1251 Jungle Roads(最小生成树)
- poj 1251 Jungle Roads(最小生成树)
- POJ 1251 Jungle Roads 最小生成树
- [最小生成树] poj 1251 Jungle Roads
- poj 1251 Jungle Roads 最小生成树
- poj 1251 Jungle Roads(最小生成树)
- POJ 1251 Jungle Roads 【最小生成树】
- POJ 1251 Jungle Roads 最小生成树
- Jungle Roads 【poj-1251】 【最小生成树】
- 【POJ 1251】Jungle Roads(最小生成树)
- POJ-1251 Jungle Roads (最小生成树)
- 个人笔记 css 03 设置表格显示外边框
- spark streaming---使用gradle混合编译Scala和Java项目并打包
- 概率dp- Ilya and Escalator
- QT检测客户端
- 分布式网站架构后续:zookeeper技术浅析
- poj 1251 Jungle Roads(最小生成树)
- LeetCode198. House Robber
- 【推荐】大规模的自然场景文字检测与识别数据库
- AC自动机入门
- LeetCode213. House Robber II
- CentOS 命令登录MySQL时,报错 ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)
- Unity Android 阴影不显示、阴影显示不正确 解决备忘
- C++10
- 1548-Cannot load from mysql.proc. The table is probably corrupted