数据结构实验之图论六:村村通公路

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

当前农村公路建设正如火如荼的展开,某乡镇政府决定实现村村通公路,工程师现有各个村落之间的原始道路统计数据表,表中列出了各村之间可以建设公路的若干条道路的成本,你的任务是根据给出的数据表,求使得每个村都有公路连通所需要的最低成本。

Input

连续多组数据输入,每组数据包括村落数目N(N <= 1000)和可供选择的道路数目M(M <= 3000),随后M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个村庄的编号和修建该道路的预算成本,村庄从1~N编号。 

Output

输出使每个村庄都有公路连通所需要的最低成本,如果输入数据不能使所有村庄畅通,则输出-1,表示有些村庄之间没有路连通。 

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 数据结构实验之图论六:村村通公路的更多相关文章

  1. SDUT OJ 数据结构实验之图论六:村村通公路(最小生成树)

    数据结构实验之图论六:村村通公路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  2. SDUT 3345 数据结构实验之二叉树六:哈夫曼编码

    数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 字符的编 ...

  3. SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...

  4. SDUT OJ 数据结构实验之图论八:欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  5. SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...

  6. SDUT OJ 数据结构实验之图论四:迷宫探索

    数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  7. SDUT OJ 数据结构实验之二叉树六:哈夫曼编码

    数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  8. SDUT OJ 数据结构实验之链表六:有序链表的建立

    数据结构实验之链表六:有序链表的建立 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...

  9. SDUT 3403 数据结构实验之排序六:希尔排序

    数据结构实验之排序六:希尔排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 我们已经学习 ...

随机推荐

  1. Ubuntu16.04上安装搜狗输入法

    一.下载搜狗输入法的deb包: http://pinyin.sogou.com/linux/ 二.打开终端输入命令: $ sudo dpkg -i sogoupinyin_2.0.0.0078_i38 ...

  2. vue-cli 脚手架项目-package.json

    使用vue-cli脚手架新建的项目中,含有package.json. package.json是npm的配置文件,里面设定了脚本以及项目依赖的库. npm run dev 这样的命令就写在packag ...

  3. HNOI2004 宠物收养所 (平衡二叉树)

    题目链接 平衡树基础题,用于测试各种平衡树的性能(雾) treap: #include<bits/stdc++.h> typedef long long ll; using namespa ...

  4. freemarker实现第一个HelloWorld

    第一步:引入freemarker jar包 第二步:创建templates下的test01.ftl 第三步:在web.xml下 第四步:编写后台代码 package com.wisezone.test ...

  5. bzoj 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——前缀和优化dp / 排列组合

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 好简单呀.而且是自己想出来的. dp[ i ]表示最后一个牡牛在 i 的方案数. 当前 ...

  6. Python collections系列之有序字典

    有序字典(orderedDict ) orderdDict是对字典类型的补充,他记住了字典元素添加的顺序 1.创建一个有序字典 import collections dic = collections ...

  7. maven 历史版本下载

    1.登录http://maven.apache.org/download.cgi 2.拉倒最下面,点击 archives 3.可以看到maven个版本,找自己需要的下载

  8. -3dB的理解

    -3dB到底是什么?集成运放-3dB带宽又是什么? 以无源高通电路为例,介绍-3dB的意义. 输出与输入只比: Au=Uo/Ui=R/(R+1/j*2*PI*f*C)=1/(1+1/j*2*PI*f* ...

  9. FPGA的年龄

    FPGA的年龄 1984年,Xilinx公司发布了第一个FPGA(但直到1985年这些器件才真正发货).尽管这些器件比当时那些简单的可编程逻辑器件(PLD)复杂的多,但大多数数字设计工程师却仅仅用这些 ...

  10. 蓝桥杯 算法训练 ALGO-147 4-3水仙花数

    算法训练 4-3水仙花数   时间限制:1.0s   内存限制:256.0MB 问题描述 打印所有100至999之间的水仙花数.所谓水仙花数是指满足其各位数字立方和为该数字本身的整数,例如 153=1 ...