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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

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

Input

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

Output

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

Sample 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

Sample Output

19

典型最小生成树问题,模板题;

#include <iostream>
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
#define ERROR -1
using namespace std; int n, m;
int G[1100][1100];
int dist[1100];
int i, j, Min;
int money, num;
int V, W; int FindMinDist ( )
{
Min = INF;
for( i=1; i<=n; i++ )
{
if( dist[i] !=0 && dist[i] < Min )
{
V = i;
Min = dist[i];
}
}
if( Min == INF )
{
V = ERROR;
}
return V;
} void Prim ( )
{
money = -1;
num = 0;
while( 1 )
{
V = FindMinDist ( );
if( V == ERROR )
break;
money += dist[V];
dist[V] = 0; num++; // 将V收进树里
for( W=1; W<=n; W++ )
{
if( dist[W] != 0 )
{
if( G[V][W] < dist[W] )
{
dist[W] = G[V][W];
}
} } }
} int main()
{
while( cin >> n >> m )
{
int s = 1;
for( i=1; i<=n; i++ )
{
for( j=1; j<=n; j++ )
{
if(i == j) G[i][j] = 0;
else G[i][j] = INF;
}
dist[i] = G[s][i];
}
dist[s] = 1;
while( m-- )
{
int a, b, c;
cin >> a >> b >> c;
if( G[a][b] > c )
{
G[a][b] = c;
G[b][a] = c;
}
}
Prim( );
if(num < n)
cout << -1 << endl;
else
cout << money << endl;
}
return 0;
}

SDUT OJ 数据结构实验之图论六:村村通公路(最小生成树)的更多相关文章

  1. SDUT 3362 数据结构实验之图论六:村村通公路

    数据结构实验之图论六:村村通公路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 当前农村公 ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. CSS DIV 居中

    左右居中 margin-left: auto; margin-right: auto; 上下居中

  2. 12个有趣的c语言面试题&nbsp;

    1.gets()函数 问:请找出下面代码里的问题: #include int main(void) { char buff[10]; memset(buff,0,sizeof(buff)); gets ...

  3. easylogging++学习记录(一):接入

    easylogging++是一个非常轻量级并且非常高效的一个日志库,支持文件配置,支持线程安全,并且其自定义格式非常的方便,最关键的是,其所有代码都集中在一个.h头文件之中,完全不需要引用第三方库,接 ...

  4. 414. Third Maximum Number数组中第三大的数字

    [抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...

  5. Mips下交叉编译dropbear

    1. 编译zlib-1.2.8 在编译dropbear的时候,会遇到“configure: error: *** zlib missing - install first or check confi ...

  6. 几种导入osm(openstreetmap)数据的方法

    一osm2pgsql+postgresql+postgis osm2pgsql­——是由OpenStreetMap开发的一个命令行工具负责将OSM数据导入到基于PostgresSql的Postgis的 ...

  7. Nhibernate HQL 匿名类(严格说是map的使用以及构造函数的使用

    1.map的使用 var hql=string.Format(@"select new map( tc.LimitIndexType as LimitIndexType, tc.LimitS ...

  8. 互联网+大数据解决方案(ppt)

    from: 互联网+大数据解决方案(ppt) 导读:大数据(bigdata),或称巨量资料,指的是所涉及的资料量规模巨大到无法透过目前主流软件工具,在合理时间内达到撷取.管理.处理.并整理成为帮助企业 ...

  9. hibernate使用记录

    1.执行SQL语句而非hql语句,getSession().createQuery(sql2) 执行的是hibernate语句; Query query2 = this.onlineMonitorDa ...

  10. location.replace() keeps the history under control

    from https://dev.opera.com/articles/efficient-javascript Occasionally, it is necessary to change the ...