POJ 2421 Constructing Roads
题意:要在n个城市之间建造公路,使城市之间能互相联通,告诉每个城市之间建公路的费用,和已经建好的公路,求最小费用。
解法:最小生成树。先把已经建好的边加进去再跑kruskal或者prim什么的。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long using namespace std; struct node
{
int u, v, val;
bool operator < (const node &tmp) const
{
return val < tmp.val;
}
}edge[10005];
int father[105];
int Find(int a)
{
if(a != father[a]) father[a] = Find(father[a]);
return father[a];
}
bool Union(int a, int b)
{
int c = Find(a), d = Find(b);
if(c == d) return false;
father[c] = d;
return true;
}
int main()
{
int n;
while(~scanf("%d", &n))
{
int cnt = 0;
for(int i = 1; i <= n; i++)
father[i] = i;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
int x;
scanf("%d", &x);
if(i <= j) continue;
edge[cnt].u = i;
edge[cnt].v = j;
edge[cnt++].val = x;
}
}
int m;
scanf("%d", &m);
for(int i = 0; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
Union(a, b);
}
sort(edge, edge + cnt);
int ans = 0;
for(int i = 0; i < cnt; i++)
{
if(Union(edge[i].u, edge[i].v)) ans += edge[i].val;
}
printf("%d\n", ans);
}
return 0;
}
POJ 2421 Constructing Roads的更多相关文章
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- poj 2421 Constructing Roads 解题报告
题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...
- POJ - 2421 Constructing Roads (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- POJ 2421 Constructing Roads(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
- POJ - 2421 Constructing Roads(最小生成树&并查集
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads
给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...
随机推荐
- TopCoder 603 div1 & div2
div2 250pts MiddleCode 题意:s串长度为奇数时,将中间字符取掉并添加到t末尾:长度为偶数时,将中间两个较小的字符取掉并添加到末尾. 分析:直接做,学习了一下substr(s, p ...
- hdu2544 最短路
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路径DIJKSTRA #include<stdio.h> #include<m ...
- linux上应用程序的执行机制
linux上应用程序的执行机制 执行文件是如何在shell中被"执行"的.本文中尽可能少用一些源码,免得太过于无 聊,主要讲清这个过程,感兴趣的同学可以去查看相应的源码了解更多的信 ...
- 2014-9-17二班----7 web project
package cn.rwkj.servlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServle ...
- 动态调整对话框属性(去掉标题栏,去掉边框,修改类似成Border:NONE样式)(调用ModifyStyle和ModifyStyleEx,然后调用SetWindowPos重新显示)
// 动态修改对话框属性,去掉对话框标题栏,设置Border为NONE属性. if(dlg.GetSafeHwnd()) { dlg.ModifyStyle(WS_CAPTION, 0); // 去掉 ...
- 34. Search for a Range
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- JS代码片段:一个日期离现在多久了
var dateDiff = function(hisTime,nowTime){ if(!arguments.length) return ''; var arg = arguments, now ...
- 转:socket编程在windows和linux下的区别
如无其它说明,本文所指Linux均表示2.6内核Linux,GCC编译器,Windows均表示Windows XP系统,Visual Studio 2005 sp1编译环境. 下面大概分几个方面进行罗 ...
- c#关键字详解
c#关键字 关键字是对编译器有特殊意义的预定义的保留标识符.它们不能在程序中用作普通标识符,除非在它们前面加上@前缀. 第一部分 一.访问关键字:base,this base:访问基类的成员. 用 ...
- php整理(一):变量和字符串
PHP中的变量: 1. 定义:$符号来定义变量 2. 说明: (1)PHP弱语言,定义变量的时候不用声明类型,但是并不代表PHP没有数据类型 (2)变量名是区分大小写的,只能是数字,字母或者下划线 ( ...