求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边预处理一下,并查集转化为同一个祖先.记得要找他们的祖先再转化.普里姆算法仅仅须要将那些已经存在的边都初始化为0就能够了. kruskal: #include<iostream> #include<cstdlib> #include<cstring> #include<…
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are conne…
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21947    Accepted Submission(s): 8448 Problem Description There are N villa…
Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B,…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are conne…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27178    Accepted Submission(s): 10340 Problem Description There are N vil…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14172    Accepted Submission(s): 5402 Problem Description There are N villa…
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1表示原图不连通 */ const int INF = 0x3f3f3f3f; const int MAXN = 110; bool vis[MAXN]; int lowc[MAXN]; int map[MAXN][MAXN]; int Prim(int cost[][MAXN], int n) {…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更适合用Prim来做. 用Kruscal的话要做一些变化. /*Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s)…
Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5227    Accepted Submission(s): 1896 Problem Description There are N villages, which are numbered from 1 to N, and you should b…
注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <malloc.h> #include<stdlib.h> #include<algorithm> #include<iostream> using namespace std; #define M 110 #define…
题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好的路的起始村庄和结束村庄.. 输入样例说明如下: 解题思路:最小生成树(kruscal算法) 1)以前的题会直接给村庄编号以及村庄距离.而这道题,这是给出村庄的距离矩阵.村庄的编号信息蕴含在 矩阵中.这时候的读取方法为: for(i = 1 ; i <= n ; ++i){ for(j = i +…
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和rich city,求解最多能修几条不相交的路.此题可以转化为LIS问题.转化过程如下: 数据中有2列,为方便表述,暂且叫做第一列和第二列. 1.若第一列是是递增的(给出的2个样例都是递增的),那么要想尽可能多的做连线,则那么就需要找出第二列中最长的递增子序列,若出现非递增的序列,那么连线后一定会相交.…
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23467    Accepted Submission(s): 6710 Problem Description JGShining's kingdom consists of 2n(n is no mo…
Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13995    Accepted Submission(s): 5324 Problem Description There are N villages, which are numbered from 1 to N, and you should…
Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 8315 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each…
Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13518    Accepted Submission(s): 5128 Problem Description There are N villages, which are numbered from 1 to N, and you should…
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14635    Accepted Submission(s): 4158 Problem Description JGShining's kingdom consists of 2n(n is no mo…
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13646    Accepted Submission(s): 3879 Problem Description JGShining's kingdom consists of 2n(n is no mor…
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27358    Accepted Submission(s): 7782 Problem Description JGShining's kingdom consists of 2n(n is no mor…
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 分析:看到这题给出的都是矩阵形式,就知道了可以用Prim算法求MST. #include <iostream> using namespace std; #define typec int #define V 101 const typec inf=0x3f3f3f3f; int vis[V]; typec lowc[V],cost[V][V]; /*======================…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p排序也是一样的.但这题的n的范围是5*10^5次方,所以用n^2算法求 最长上升子序列肯定不行,下面简单介绍一下nlogn时间内求的方法: 从序列里面每次插入一个数,插入到另外一个数组里面,这个数组初始状态是空的,插入一个数时,如果这个数比这个数组里面的任何一个数都大,则直接插入到最后面,否则判断这…
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市,所以要修路,但是不能从富有的修到富有的也不能从贫穷的修到贫穷的,只能从富有的修到贫穷的,但是不允许修交叉路,所以问你最多能修多少条路. 题意 :这个题一开始我瞅了好久都没觉得是DP,后来二师兄给讲了一下才恍然大悟.其实就是用到了DP中的那个最长上升子序列,把题中贫穷城市的坐标当成数组的下标,跟其相连…
Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we ca…
Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B…
简单最小生成树,畅通project.这三道题目都是练习最小生成树的. 注意一下推断是否有通路时,kruskal能够推断每一个点的祖先是否同样.prim能够推断每一个点是否都加进集合里面了,也就是说是否都訪问过. prim算法要把没有给的边初始化为MAX无穷大. .. 代码:(kruskal) #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<…
怎么说呢 这题就是个模板题 但是 hud你妹夫啊说好的只有一组数据呢??? 嗯??? wa到家都不认识了好吗 #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm> #define INF 0x3f3f3f3f using namespace std; typedef pair<int, int> pii;…
主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下降子序列.太NB了.复杂度n*log(n). 解法:dp+二分 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorit…
题意:给出n个村庄之间的距离,再给出已经连通起来了的村庄.求把所有的村庄都连通要修路的长度的最小值. 思路:Kruskal算法 课本代码: //Kruskal算法 #include<iostream> using namespace std; int fa[120]; int get_father(int x){ return fa[x]=fa[x]==x?x:get_father(fa[x]);//判断两个节点是否属于一颗子树(并查集) } int main(){ int n; int p[…
本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来分享一下. #include <stdio.h> #include <string.h> #define N 500005 int map[N], dp[N]; int main () { ; while (scanf ("%d", &n) != EOF)…