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 我们已经学习 ...
随机推荐
- Zeroc Ice 负载均衡之Icegrid simple
最近学习Icestorm的replicated例子,在本地计算机上面跑通了,但在两台机器上(一台服务器192.168.0.113,一台客户端192.168.0.188),怎么都跑不通.上网求助,大家给 ...
- [转]NYOJ-511-移动小球
大学生程序代写 http://acm.nyist.net/JudgeOnline/problem.php?pid=511 这道题很容易想到要构建一个循环链表来确定每个球的相对位置,就是操作比较繁琐,考 ...
- mysql-jdbc创建connection理解
jdbc源码分析(http://blog.csdn.net/brilliancezhou/article/details/5499738) 创建JDBC连接代码 Class.forName(" ...
- Linux-CentOS7 安装VMware Workstation 12
转自:http://blog.csdn.net/aoshilang2249/article/details/48656107 1.下载VMware 衔接地址 http://www.vmware.com ...
- 用HAWQ轻松取代传统数据仓库(八) —— 大表分区
一.HAWQ中的分区表 与大多数关系数据库一样,HAWQ也支持分区表.这里所说的分区表是指HAWQ的内部分区表,外部分区表在后面“外部数据”篇讨论.在数据仓库应用中,事 实表通常有非常多 ...
- RabbitMQ教程总结
[译]RabbitMQ教程一 主要通过Hello Word对RabbitMQ有初步认识 [译]RabbitMQ教程二 工作队列,即一个生产者对多个消费者 循环分发.消息确认.消息持久.公平分发 [译] ...
- MongoDB 3.4 高可用集群搭建(二)replica set 副本集
转自:http://www.lanceyan.com/tech/mongodb/mongodb_repset1.html 在上一篇文章<MongoDB 3.4 高可用集群搭建(一):主从模式&g ...
- Celery-4.1 用户指南: Daemonization (系统守护进程)
Generic init-scripts 查看Celery发布里的 extra/generic-init.d/ 文件夹. 这个文件夹中包含了celery worker 程序的通用bash初始化脚本,可 ...
- 类型:Oracle;问题:oracle 查询表详细信息;结果:oracle查询表信息(索引,外键,列等)
oracle查询表信息(索引,外键,列等) oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息,索引信息查询SQL如下,希望对大家有所帮助: 1.查询出所有的用户表sel ...
- windows下查看端口占用(砖)
第一步,执行命令:netstat -ano -a 显示所有连接和监听端口 -n 以数字形式显示地址和端口号. 此选项一般与 -a选项组合使用 -o 显示与每个连接相关的所属进程 ID. Active ...