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 我们已经学习 ...
随机推荐
- 树莓派外设、模块、传感器 —— 数码管(F3461BH)
1. 4 位 8 段(7+点)数码管 树莓派GPIO入门05-驱动数码管显示数字 其内部电路图及各个段的引脚控制如下: 共阳与共阴: 数码管从电源极性上分共阳和共阴两种.解释一下,如果数码管上每一个独 ...
- cocos2dx混合模式应用———制作新手引导高亮区域 (2.2.0)
cocos2dx混合模式应用———制作新手引导高亮区域 转自:http://www.cnblogs.com/mrblue/p/3455775.html 首先,效果预览一下 高亮区域的图片: 示例代码: ...
- 242. Valid Anagram Add to List
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- Unity Shader实现描边效果
http://gad.qq.com/article/detail/28346 描边效果是游戏里面非常常用的一种效果,一般是为了凸显游戏中的某个对象,会给对象增加一个描边效果.本篇文章和大家介绍下利用S ...
- GPIO编程2:使用GPIO监听中断完整程序
一个完整的使用GPIO捕捉中断的程序: #include<stdlib.h> #include<stdio.h> #include<string.h> #inclu ...
- Day2-VIM(三):删除
字符删除 x 删除光标所在处字符 X 删除光标所在前字符 这里没有什么可注意的地方,但需要说明一下的是 通常情况下,新手一旦着急便会按着x不动,从而达到删除一大块文本的目的 如果是头几天使用还好说,但 ...
- 机器学习:PCA(实例:MNIST数据集)
一.数据 获取数据 import numpy as np from sklearn.datasets import fetch_mldata mnist = fetch_mldata("MN ...
- maven 学习 十 关于打包
clean package -Dmaven.test.skip=true -P product 这个命令干的活: 清class文件,打包构建,跳过测试,注意最后一个 -P product, 会激活项目 ...
- git学习4 常用命令
1:更新: 更新后,更新只在Workspace中,没有到暂存区.git status可以查看当前状态. git add <file> 可以放到待提交区. git checko ...
- zedgraph控件的一些比较有用的属性
(1)zedgraph控件属性具体解释: AxisChange()() ->> This performs an axis change command on the graphPane. ...