随笔分类 - 并查集 最短路 最小生成树
摘要:道题比较坑,就是输入N,M之后可能有多个空格这里要注意;方法:1:建图,把A进行编号在存储到图中;2:利用BFS找出任意两点的最短距离;3:利用kruscal生成最小生成树;View Code #include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>#include<cstring>#include<vector>using namespace std;in
阅读全文
摘要:一道简单的最小生成树问题:View Code #include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>#include<map>#include<cstring>#include<vector>using namespace std;class Kru{public: int x,y,dis;}kru[10024];int set[124];bo
阅读全文
摘要:一道简单的最小生成树问题;View Code #include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>#include<map>#include<cstring>#include<vector>using namespace std;class Kru{public: int x,y,dis;}kru[250024];int set[524];b
阅读全文
摘要:题意:用一个7位的string代表一个车的编号,两个编号之间的distance代表这两个编号之间相同位置不同字母的个数。如果一个编号要“衍生”另一个编号,代价是这两个编号之间相应的distance也就是改变字母数,现在要求总的改变数最小,也就是distance之和最小。4aaaaaaabaaaaaaabaaaaaaabaaaa编号2,3,4分别从第一编号衍生出来的代价最小,因为2,3,4分别与第一编号只有一个字母是不同的,相应的distance都是1,加起来是3。也就是总的改变数最小为3。View Code #include<iostream>#include<cstdio&
阅读全文
摘要:这个题求是否经过兑换后钱能不能增加。这题可以用bellman比较容易解决;View Code #include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>#include<set>#include<cstring>#include<string>#include<vector>using namespace std;class Edge{
阅读全文
摘要:题意:以任意顶点为起点,能够连通所有的节点,问你以哪个节点为起点时到所有的节点的最短路的最长路最短;View Code #include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>#include<set>#include<cstring>#include<vector>using namespace std;const int inf = 0x7f
阅读全文
摘要:题意:一只青蛙在湖中1号石头上, 它想去2石头号上去找另外一只青蛙,但是 湖里的水很脏,它不愿意游泳,所以它要跳过去;给出 两只青蛙所在石头的坐标, 及湖里其他石头的坐标;任一两个坐标点间都是双向连通的。显然从A到B存在至少一条的通路,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中又有一个最大距离。现在要求求出所有通路的最大距离,并把这些最大距离作比较,把最小的一个最大距离作为青蛙的最小跳远距离。解法一:用克鲁斯卡尔:View Code #include<iostream>#include<cstdio>#include<cstdlib>#inc
阅读全文
摘要:这个一道最短路题;这里难处理的就是等级只差的问题;这里我们就用枚举法;假设等级只差为L=3;酋长的等级为5,这里就只存在这几种情况2-5,3-6,4-7,5-8;我们只要处理这几种情况就可以了;至于建图就直接用钱数就可以了;View Code #include<cstdio>#include<iostream>using namespace std;const int inf = 0x7fffffff;int level[124],map[124][124],L,N,dis[124],hash[124];void Empty( ){ for( int i = 0 ; i
阅读全文
摘要:这是一道求有没有负环的题,用到bellman算法;这里要注意的是路是双向的;View Code #include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>using namespace std;class Weight{public: int S,E,T;}edge[5024];bool Bell_man( int count, int N ){ int dis[1024] = {0},flag = 0; for( int
阅读全文
摘要:这题就是是一个最小生成树问题,题目意思,给你一个数n接下来有n-1行,每一行代表一个地点与多个地点相连结,例如:A 2 B 12 I 25代表A与连接2地方B,I并且价钱为12,25;#include<stdio.h>#include<stdlib.h>struct t{ int x,y,time;}kru[1024];int set[27];int find( int x ){ return set[x]==x?x:set[x]=find( set[x] ); }int cmp( const void *a,const void *b ){ return ( ( t.
阅读全文
摘要:这题就是注意一下当状态为1是就把这两给点合并到一个集合;#include<stdio.h>#include<stdlib.h>struct t{ int x,y,flag,cost; }kru[5024];int set[124];int cmp( const void *a,const void *b ){ return ( ( t * )a )->cost-( ( t * )b )->cost; }int find( int x ){ return x==set[x]?x:set[x]=find( set[x] ); }int kruscal( i...
阅读全文
摘要:我用G++提交就TLE了,而用C++提交就过了,这题就是一个并查集与克鲁斯卡尔。#include<stdio.h>#include<stdlib.h>struct t{ int x,y,cost; }kru[25024];int set[524];int cmp( const void *a,const void *b ){ return ( ( t * )a )->cost - ( ( t * )b )->cost; }inline int find( int x ){ return set[x]==x?x:set[x]=find( set[x] ); .
阅读全文

浙公网安备 33010602011771号