题目链接:http://code.hdu.edu.cn/showproblem.php?pid=1879

这条题目我的做法与解决Constructing Roads的解法是相同的。

0 表示没有连通;1代表已连通。在已连通村庄的道路的基础上,找到扩展出来的最小生成树。

 #include <iostream>
#include <algorithm>
using namespace std; const int maxe = + ;
int rep[maxe], vis[maxe]; struct sets
{
int v1, v2;
int value;
} exa[maxe]; int cmp(sets a, sets b)
{
return a.value < b.value;
} int find(int x)
{
return x == rep[x] ? x : find(rep[x]);
} int main()
{
int i, n, a, b, c, x, y, cnt, flag, sign;
while (scanf("%d", &n) != EOF && n)
{
for (i = ; i <= n; i++)
rep[i] = i;
cnt = flag = ;
n *= (n-)/;
memset(vis, , sizeof(vis));
for (i = ; i < n; i++)
{
scanf("%d%d%d%d", &a, &b, &c, &sign);
if (a > b)
swap(a, b);
if (sign == )
{
exa[cnt].v1 = a;
exa[cnt].v2 = b;
exa[cnt].value = c;
cnt++;
}
else
{
x = find(a);
y = find(b);
if (x > y)
swap(x, y);
if (vis[a] && vis[b])
rep[x] = y;
else
{
if (x == y)
flag = ;
if (!vis[a] && !vis[b])
{
rep[x] = y;
vis[a] = vis[b] = ;
}
else if (!vis[a])
{
rep[x] = y;
vis[a] = ;
}
else if (!vis[b])
{
rep[x] = y;
vis[b] = ;
}
}
}
}
sort(exa, exa+cnt, cmp);
if (!flag)
{
int minval = ;
for (i = ; i < cnt; i++)
{
x = find(exa[i].v1);
y = find(exa[i].v2);
if (x != y)
{
minval += exa[i].value;
if (x < y)
rep[x] = y;
else
rep[y] = x;
}
}
printf("%d\n", minval);
}
else
printf("0\n");
}
return ;
}

hdu 1879 继续畅通工程 解题报告的更多相关文章

  1. hdu 1233 还是畅通工程 解题报告

    题目链接:http://code.hdu.edu.cn/showproblem.php?pid=1233 并查集的运用, 实质就是求最小生成树.先对所有的村庄距离从小到大排序,然后判断村庄之间是否属于 ...

  2. hdu 1879 继续畅通工程

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

  3. 【九度OJ】题目1028:继续畅通工程 解题报告

    [九度OJ]题目1028:继续畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1028 题目描述: 省政府" ...

  4. 【九度OJ】题目1024:畅通工程 解题报告

    [九度OJ]题目1024:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅 ...

  5. 【九度OJ】题目1017:还是畅通工程 解题报告

    [九度OJ]题目1017:还是畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1017 题目描述: 某省调查乡村交通 ...

  6. 【九度OJ】题目1012:畅通工程 解题报告

    [九度OJ]题目1012:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1012 题目描述: 某省调查城镇交通状况 ...

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

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

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

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

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

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

随机推荐

  1. Yii2 RBAC 用到的表

    Yii2 RBAC用到的四张auth表位于 vendor/yiisoft/yii2/rbac/migration文件夹里面,可以用migration生成 yii migrate --migration ...

  2. Oracle创建DBLink的方法

    文章从http://blog.csdn.net/davidhsing/article/details/6408770拷贝过来的 1.如果需要创建全局 DBLink,则需要先确定用户有创建 dblink ...

  3. POJ-2299 Ultra_QuickSort 线段树+逆序对数

    Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50737 Accepted: 18595 Des ...

  4. 洛谷P1363 幻想迷宫

    题目描述 背景 Background (喵星人LHX和WD同心协力击退了汪星人的入侵,不幸的是,汪星人撤退之前给它们制造了一片幻象迷宫.) WD:呜呜,肿么办啊…… LHX:momo...我们一定能走 ...

  5. jQuery的查找

    children([expr])概述 :取得一个包含匹配的元素集合中每一个元素的所有子元素的元素集合.可以通过可选的表达式来过滤所匹配的子元素.注意:parents()将查找所有祖辈元素,而child ...

  6. Jquery easyui datagrid 删除多行问题

    http://www.cnblogs.com/Dtscal/archive/2012/07/04/2576639.html 最近模仿了刘冬大哥的<开源框架完美组合之Spring.NET + NH ...

  7. c/c++细节知识整理

    这篇文章总结了部分c/c++琐碎的细节知识. 目录如下: (一)bool类型 知识点出处较多,无法一一列举,向原作者致敬. (一)bool类型 在c99标准以前,c语言并没有定义bool类型.如果需要 ...

  8. Ten Tips for Writing CS Papers, Part 2

    Ten Tips for Writing CS Papers, Part 2 This continues the first part on tips to write computer scien ...

  9. hdu 1284 钱币兑换问题(动态规划)

    Ac code : 完全背包: #include<stdio.h> #include<string.h> int dp[4][40000]; int main(void) { ...

  10. Linq 中 表连接查询

    public void Test(){ var query = from a in A join b in B on A.Id equals B.Id into c from d in c.Defau ...