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 我们已经学习 ...
随机推荐
- @Override重写
package com.wisezone.f; //父类 public class Person { //姓名 private String name; //年龄 private int age; / ...
- redis3.2.11 安装
wget http://download.redis.io/releases/redis-3.2.11.tar.gz [root@hdp01 src]# .tar.gz -C /opt/ [root@ ...
- BZOJ5336: [TJOI2018]party
BZOJ5336: [TJOI2018]party https://lydsy.com/JudgeOnline/problem.php?id=5336 分析: 好题. 正常的思路是设\(f[i][j] ...
- Maven实现直接部署Web项目到Tomcat7
如题目,自动部署到Web服务器,直接上过程. 1.Tomcat7的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户. <role rolena ...
- HP 防止cciss设备被DM映射
http://h10025.www1.hp.com/ewfrf/wc/document?cc=cn&lc=zh-hans&dlc=zh-hans&docname=c034933 ...
- java集合类(2)
java集合的主要分为三种类型:JAVA集合位于 java.util包 Set(集) List(列表) Map(映射) arrays函数, equals():比较两个array是否相等. fill() ...
- Android SDK下载项的说明
Tools下 1.android sdk tools 软件开发工具包(software development kit):包括测试.调试.第三方工具.模拟器.数据管理工具等. 2.android sd ...
- TCS3200颜色传感器测试实验
TCS3200颜色传感器测试实验 2013-08-02 17:18:24 分享: 标签: Arduino TCS3200 传感器 TCS3200颜色传感器是一款全彩的颜色检测器,包括了一块TAO ...
- oracle错误-ORA-12519, TNS:no appropriate service handler found
转自:https://blog.csdn.net/u013147600/article/details/48380841
- C++深度解析教程学习笔记(2)C++中的引用
1.C++中的引用 (1)变量名的回顾 ①变量是一段实际连续存储空间的别名,程序中通过变量来申请并命名存储空间 ②通过变量的名字可以使用存储空间.(变量的名字就是变量的值,&变量名是取地址操作 ...