ZOJ 3204Connect them(最小生成树)
来源:互联网 发布:积分兑换系统源码 编辑:程序博客网 时间:2024/05/28 17:04
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3204
题意:给出一个图的链接矩阵,要求按照字典序最小的情况输出连接边
Prim算法加father数组记录父亲节点,更新时在距离相同的情况下维护父亲节点值最小,然后根据父亲节点和当前节点的大小创建边,最后排序并输出
#include<iostream>#include<cstdio>#include<set>#include<string>#include<string.h>#include<cstring>#include<vector>#include<map>#include<queue>#include<stack>#include<cctype>#include<algorithm>#include<sstream>#include<utility>#define mt(a) memset(a,0,sizeof (a))#define fl(a,b,c) fill(a,b,c)#define SWAP(a,b,t) (t=a,a=b,b=t)#define inf 1000000000+7using namespace std;typedef long long ll;int MaxVal;int vis[120];int dis[120];int graph[120][120];int fa[120];typedef struct aaa{int in, out;}aaa;aaa ans[120];int cnt;bool com(aaa x, aaa y){if (x.in != y.in)return x.in < y.in;return x.out < y.out;}void prim(){mt(vis);mt(ans);mt(fa);cnt = 0;fill(dis, dis+MaxVal,inf);int pos, getfa, i, j,minn;pos = 0; vis[0] = 1;for (int i = 0; i < MaxVal; i++){fa[i] = pos;dis[i] = graph[pos][i];}for (int i = 0; i < MaxVal-1; i++){pos = -1; minn = inf;for (int j = 0; j < MaxVal; j++){if (!vis[j] && (dis[j] < minn||(dis[j] == minn&&fa[j]<pos))){minn = dis[pos = j];}}if (pos == -1)break;if (fa[pos] < pos){ans[cnt].in = fa[pos] + 1;ans[cnt++].out = pos + 1;}else{ans[cnt].in = pos + 1;ans[cnt++].out = fa[pos] + 1;}vis[pos] = 1;for (int j = 0; j < MaxVal; j++){if (!vis[j] && (graph[pos][j] < dis[j]) || (graph[pos][j] == dis[j] && pos<fa[j])){dis[j] = graph[pos][j];fa[j] = pos;}}}}int main(){int T;cin >> T;while (T--){cin >> MaxVal;mt(graph);for (int i = 0; i < MaxVal; i++){for (int j = 0; j < MaxVal; j++){scanf("%d", &graph[i][j]);if (!graph[i][j])graph[i][j] = inf;}}prim();int flag = 1;for (int i = 1; i < MaxVal; i++)if (!vis[i])flag = 0;if (!flag){ printf("-1\n"); continue; }sort(ans, ans + cnt, com);for (int i = 0; i < cnt; i++){if (i)printf(" ");printf("%d %d", ans[i].in, ans[i].out);}printf("\n");}return 0;}
0 0
- zoj 3204 Connect them//最小生成树
- Connect them - ZOJ 3204 最小生成树
- 【最小生成树】 ZOJ 3204 Connect them
- ZOJ 3204Connect them(最小生成树)
- ZOJ 3204 Connect them最小生成树
- ZOJ 3204 Connect them 最小生成树+字典序最小
- ZOJ-3204 Connect them(最小生成树)
- ZOJ 3204 Connect them(最小生成树:kruscal算法)
- zoj 3204 Connect them 最小生成树 打印路径
- ZOJ 3204 Connect them(最小生成树之Krusal 输出字典序最小的)
- zoj 3204 2966 Connect them|建设电力系统(最小生成树)
- ZOJ-3204-Connect them【6th浙江省赛】【最小生成树】
- ZOJ 3204 Connect them 【最小生成树+输出选取的边】
- zoj3204 Connect them 最小生成树 MST
- zoj3204 connect them 最小生成树 暴力
- ZOJ - 3204 Connect them
- ZOJ 3204 Connect them
- ZOJ 3204 Connect them
- 华为机试题:将十六进制字符串形式内容转为内存值形式(java)
- 例题 6-15 UVA 10305 Ordering Tasks 给任务排序
- Dom4j的使用(全而好的文章)
- spring 2.5 AOP之代理类基础理解
- 再叙TIME_WAIT
- ZOJ 3204Connect them(最小生成树)
- 日志协查步骤
- 喝啤酒(预防老年痴呆的深度搜索)
- js函数传递两个参数
- 用python3.x正则表达式匹配中文字符串
- IE>js打印
- 分区存储管理——空闲分区的分配策略
- 蓝桥杯-回文数(BASIC)
- 判断浏览器内核,然后改变图片样式