SDUT 3362 数据结构实验之图论六:村村通公路
数据结构实验之图论六:村村通公路
Problem Description
Input
Output
Example Input
5 8
1 2 12
1 3 9
1 4 11
1 5 3
2 3 6
2 4 9
3 4 4
4 5 6
Example Output
19
DQE:
#include <iostream>
#include <cstdio>
#include <climits> using namespace std; #define MVN 1010 typedef struct AdjMatrix
{
int w;
char *info;
}AM; typedef struct MGraph
{
int vex[MVN];
AM arc[MVN][MVN];
int vexn,arcn;
}MG; void creat(MG &G)
{
int i,j,w,k;
for(i=;i<=G.vexn;i++)
for(j=;j<=G.vexn;j++)
G.arc[i][j].w=INT_MAX;
for(k=;k<=G.arcn;k++)
{
scanf("%d %d %d",&i,&j,&w);
G.arc[i][j].w=G.arc[j][i].w=w;
}
} struct{int i,w;}C[MVN]; //辅助数组{树上最近顶点编号,到该顶点距离}
int Mini(MG &G,int &sum)
{
int count=; //统计树上顶点
int i,j,k,min;
//初始化辅助数组
for(i=;i<=G.vexn;i++)
{
C[i].i=;
C[i].w=G.arc[][i].w;
}
C[].w=;
count++;
//将最近的顶点加入树
for(k=;k<=G.vexn;k++)
{
min=INT_MAX;
i=-;
for(j=;j<=G.vexn;j++)
if(C[j].w>)
if(C[j].w<min)
{
i=j;
min=C[j].w;
}
if(i==-) //没有可加顶点时返回至调用处
return count;
sum+=C[i].w;
C[i].w=;
count++;
//新顶点入树后更新辅助数组
for(j=;j<=G.vexn;j++)
if(G.arc[i][j].w<C[j].w)
{
C[j].i=i;
C[j].w=G.arc[i][j].w;
}
}
return count;
} int main()
{
MG G;
while(scanf("%d %d",&G.vexn,&G.arcn)!=EOF)
{
creat(G);
int sum=; //距离之和
if(Mini(G,sum)==G.vexn)
printf("%d\n",sum);
else
printf("-1\n");
}
return ;
} /***************************************************
User name: ***
Result: Accepted
Take time: 0ms
Take Memory: 184KB
Submit time: 2016-11-17 19:14:49
****************************************************/
解法二(不推荐):最小生成树问题,学会使用countw数组连接下一个顶点,配合深度优先搜索判断公路是否连通,效率低下难懂,改用循环更佳。
#include <iostream>
#include <cstdio>
#include <climits> //INT_MAX using namespace std; #define MVN 101 typedef struct AdjMatrix
{
int w;
char *info;
}AM; typedef struct MGraph
{
int vex[MVN];
AM arc[MVN][MVN];
int vexnum,arcnum;
}MG; struct ue
{
int pr;
int w;
}countw[MVN],mi; void creat(MG &G)
{
int i,j,w,k;
for(k=;k<=G.vexnum;k++)
G.vex[k]=k;
for(k=;k<=G.arcnum;k++)
{
scanf("%d %d %d",&i,&j,&w);
G.arc[i][j].w=w;
G.arc[j][i].w=w;
}
} int count,sum;
void DFS(MG &G,bool *visited,int i)
{
count++;
visited[i]=true;
int k;
mi.w=INT_MAX;
mi.pr=;
for(k=;k<=G.vexnum;k++)
{
if(countw[k].w<mi.w && visited[k]==false)
{
mi.pr=k;
mi.w=countw[k].w;
}
}
if(mi.pr!=)
{
for(k=;k<=G.vexnum;k++)
{
if(countw[k].w>G.arc[k][mi.pr].w)
{
countw[k].pr=mi.pr;
countw[k].w=G.arc[k][mi.pr].w;
}
}//更新countw
sum+=G.arc[countw[mi.pr].pr][mi.pr].w;
DFS(G,visited,mi.pr);
}
} int main()
{
MG G;
while(scanf("%d %d",&G.vexnum,&G.arcnum)!=EOF)
{
int k,o;
for(k=;k<=G.vexnum;k++)
{
for(o=;o<=G.vexnum;o++)
{
G.arc[k][o].w=INT_MAX;
}
}//邻接矩阵初始化
creat(G);
bool visited[MVN]={false};
for(k=;k<=G.vexnum;k++)
{
if(G.arc[][k].w<INT_MAX)
{
countw[k].pr=;
countw[k].w=G.arc[][k].w;
}
else
countw[k].w=INT_MAX;
}
count=;
sum=;
DFS(G,visited,);
if(count==G.vexnum)
printf("%d\n",sum);
else
printf("-1\n");
}
return ;
} /***************************************************
User name: ***
Result: Accepted
Take time: 0ms
Take Memory: 172KB
Submit time: 2016-11-09 21:00:48
****************************************************/
SDUT 3362 数据结构实验之图论六:村村通公路的更多相关文章
- SDUT OJ 数据结构实验之图论六:村村通公路(最小生成树)
数据结构实验之图论六:村村通公路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT 3345 数据结构实验之二叉树六:哈夫曼编码
数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 字符的编 ...
- SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列
数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...
- SDUT OJ 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...
- SDUT OJ 数据结构实验之图论四:迷宫探索
数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之二叉树六:哈夫曼编码
数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之链表六:有序链表的建立
数据结构实验之链表六:有序链表的建立 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...
- SDUT 3403 数据结构实验之排序六:希尔排序
数据结构实验之排序六:希尔排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 我们已经学习 ...
随机推荐
- Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一)
最近用vue2做了一个微信商城项目,因为做的比较仓促,所以一边写一下整个流程,一边稍做优化. 项目github地址:https://github.com/seven9115/vue-fullstack ...
- UVA 11637 Garbage Remembering Exam
#include <iostream> #include <stdio.h> #include <cstring> #include <math.h> ...
- oracle获得当前时间,精确到毫秒并指定精确位数
oracle获得当前时间的,精确到毫秒 可以指定精确豪秒的位数 select to_char(systimestamp, 'yyyymmdd hh24:mi:ss.ff ') from dual; ...
- SQL Sever 学习系列之一
SQL Sever 学习系列之一 本学习系列,从实际工作需要中积累,对于一个新手而言,写出几条漂亮的查询语句,应该是可以受启发的. 一.问题的需求是:员工薪酬发放,现有资金能发放多少人,哪些人应得? ...
- BZOJ2648:SJY摆棋子
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- JavaScript正则常用知识总结
一.JavaScript正则相关方法 str.match(regexp)与regexp.exec(str)功能类似. str.search(regexp)与regexp.test(str)功能类似. ...
- Linux基础命令-echo
echo命令 功能:显示字符 (末尾自带换行功能) 语法:echo [-neE][字符串] 说明:echo会将输入的字符串送往标准输出.输出的字符串间以空白字符隔开, 并在最后加上换行号 -n 不在字 ...
- JSF在ui:include中传递参数到对应控制层
在JSF中使用ui:include方法可以引入一个页面到当前页面中,如果要向被包含的页面中传入参数,可以使用ui:param标签,这个标签类似于f:param,只不过一个用于页面,一个用于实际标签.示 ...
- hihoCoder#1139(二分+bfs)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回和上上回里我们知道Nettle在玩<艦これ>,Nettle在整理好舰队之后终于准备出海捞船和敌军交战了 ...
- 分析诊断工具之一:MYSQL性能查看(多指标)
网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步设置参考,我们需要根据自己的情况进行配置优化,好的做法是MySQL服务器稳定运行了一 ...