Sample Input
3
1 2 1 0 //u v w 是否已建
1 3 2 0
2 3 4 0
3
1 2 1 0
1 3 2 0
2 3 4 1
3
1 2 1 0
1 3 2 1
2 3 4 1
0

Sample Output
3
1
0

将已经建好的路的权值设为0

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; int n ;
const int MAXN=;//最大点数
const int MAXM=;//最大边数
int F[MAXN];//并查集使用
struct Edge
{
int u,v,w;
}edge[MAXM];//存储边的信息,包括起点/终点/权值 int tol;//边数,加边前赋值为0
void addedge(int u,int v,int w)
{ edge[tol].u=u;
edge[tol].v=v;
edge[tol++].w=w;
}
bool cmp(Edge a,Edge b)
{//排序函数,讲边按照权值从小到大排序
return a.w<b.w;
}
int find(int x)
{
if(F[x]==-)return x;
else return F[x]=find(F[x]);
}
int Kruskal()//传入点数,返回最小生成树的权值,如果不连通返回-1
{
memset(F,-,sizeof(F));
sort(edge,edge+tol,cmp);
int cnt=;//计算加入的边数
int ans=;
for(int i=;i<tol;i++)
{
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int t1=find(u);
int t2=find(v);
if(t1!=t2)
{
ans+=w;
F[t1]=t2;
cnt++;
}
if(cnt==n-)break;
}
if(cnt<n-)return -;//不连通
else return ans;
} int main()
{ //freopen("in.txt","r",stdin) ;
while(scanf("%d" , &n) != EOF)
{
if (n == )
break ;
int u , v , w , ok ;
int i ;
tol = ;
for (i = ; i <= n*(n-)/ ; i++)
{
scanf("%d %d %d %d" , &u , &v , &w , &ok) ;
if (ok == )
addedge(u,v,) ;
else
addedge(u,v,w) ;
}
printf("%d\n" , Kruskal()) ; }
return ;
}

hdu 1879 有的边已存在 (MST)的更多相关文章

  1. hdu 1879 继续畅通工程

    /************************************************************************/ /* hdu 1879 继续畅通工程 Time L ...

  2. hdu 1879 继续畅通工程 解题报告

    题目链接:http://code.hdu.edu.cn/showproblem.php?pid=1879 这条题目我的做法与解决Constructing Roads的解法是相同的. 0 表示没有连通: ...

  3. hdu 1879 继续通畅工程(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1879 /************************************************* ...

  4. hdu 1879 继续畅通工程 (并查集+最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1879 继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    ...

  5. HDU 1879 继续畅通工程 (Prim(普里姆算法)+Kruskal(克鲁斯卡尔))

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  6. HDU 4126 Genghis Khan the Conqueror MST+树形dp

    题意: 给定n个点m条边的无向图. 以下m行给出边和边权 以下Q个询问. Q行每行给出一条边(一定是m条边中的一条) 表示改动边权. (数据保证改动后的边权比原先的边权大) 问:改动后的最小生成树的权 ...

  7. HDU 1879 继续畅通工程(最小生成树)

    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经 ...

  8. HDU 1710Binary Tree Traversals(已知前序中序,求后序的二叉树遍历)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1710 解题思路:可以由先序和中序的性质得到 : 先序的第一个借点肯定是当前子树的根结点, 那么在 中序 ...

  9. hdu 1879 继续畅通工程 (最小生成树)

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. JAVA记录-Mybatis介绍

    1.什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyB ...

  2. Study 7 —— CSS美化背景和边框

    图片透明度属性:opacity 背景图片重复属性:background-repeat背景图片位置属性:background-position background-postion:横坐标 纵坐标; b ...

  3. 用 Lua 控制 MIDI 合成器来播放自定义格式乐谱

    用 Lua 控制 MIDI 合成器来播放自定义格式乐谱 作者: FreeBlues 最新: https://www.cnblogs.com/freeblues/p/9936844.html 说明: 本 ...

  4. QT_地图导航 源码下载

    https://github.com/douzujun/MyMapView 主要算法讲解: 1. 计算最短路径(dijkstra算法) Step1: (1)找到最短路径已经确定的顶点,从它已经确定的顶 ...

  5. Parent Proxy 和 Origin Server配置学习

    Parent Proxy Configuration proxy.config.http.parent_proxy_routing_enable 开启/关闭parent caching: proxy. ...

  6. MSVCR120.dll丢失问题

    一.问题:丢失MSVCR120.dll 二.解决方法 到官网下载vcredist_x86.exe安装即可 地址:https://www.microsoft.com/en-us/download/det ...

  7. 五、u-boot 启动流程---u-boot.lds

    5.1 u-boot.lds  链接脚本分析 uboot 编译出来的第一个链接脚本就是执行 u-boot.lds 链接脚本,去掉里面无用的和没有定义的,进行分析. /* 配置头文件,自动生成的,包含芯 ...

  8. REUSE_ALV_GRID_DISPLAY详解

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING I_INTERFACE_CHECK = ' ' "接口一致性检查 * I_BYPASSING ...

  9. Python中os.system和os.popen区别

    Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而选择. 两者的区别是: os.system(cmd)的返 ...

  10. Python 入门基础17 --加密、表格、xml模块

    今日内容: 1.hashlib模块:加密 2.hmac模块:加密 3.configparser模块:操作配置文件 4.subprocess模块:操作shell命令 5.xlrd模块:excel 6.x ...