摘要: 题意:N的虫里面有M对情侣关系,判断其中存不存在“基情”,即在之前确定的异性关系中出现了同性的情侣。分析:我们可以假设异性在不同的并查集里,那么假设有一对情侣a和b是异性关系,那么a和b的异性是同性关系,b和a的异性也是同性关系,即a和b的异性 以及 b和a的异性 在同一个并查集里。为了模拟这个过程,我们假设对于任意一个元素c , c+n是他的默认异性,接下来每一次合并之前判断有没有矛盾就行了。View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std 阅读全文
posted @ 2012-07-05 03:28 lenohoo 阅读(193) 评论(0) 推荐(0)
摘要: 题意:给出一个0-1矩阵,判断是否能从其中选择几行使得选出的行中每一列都有且仅有一个1。分析:dfs , 此题也可以用Dancing Links 优化,因为是一道精确覆盖的题普通搜索:View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;const int maxn = 18 , maxm = 303;int n , m;int a[maxn][maxm];bool vis[maxm];int ok;bool pan(int i) { for(i 阅读全文
posted @ 2012-07-05 02:49 lenohoo 阅读(294) 评论(0) 推荐(0)
摘要: 题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是 默认的是 第一个指向的方向,所以如果要选择别的方向的话得 进行一次切换操作 ,给定一个起点一个终点 ,问最少进行几次 切换操作 能够 使 火车 完成这个历程 , 如果开不到,输出“-1”。分析:设默认路径边权为0,备选路径边权为1,求单源最短路即可。View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;#define re(i,n) for( 阅读全文
posted @ 2012-07-05 00:05 lenohoo 阅读(283) 评论(0) 推荐(0)
摘要: 题意:有N个客户,M个仓库,和K种货物。已知每个客户需要每种货物的数量,每个仓库存储每种货物的数量,每个仓库运输各种货物去各个客户的单位费用。判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用。分析:首先判断每个仓库的库存是否满足N个客户的需要,在这个条件下,因为有K种货物,而K种货物又是从不同的仓库发出的,所以对于每一种货物,我们可以设想起其为一种费用,对每种货物进行一次 最小费用最大流即可。View Code #include <cstdio>#include <cstring>#include <iostream>#include & 阅读全文
posted @ 2012-07-04 18:33 lenohoo 阅读(138) 评论(0) 推荐(0)
摘要: 题意:给出20个国家的连接情况,然后求出连接2个目标国家的最少边数。算法:转化为最短路问题。将每条边的权值设为1,“最少边数”即等价为“最短路”View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;#define re1(i,n) for(int i=1;i<=n;i++)const int maxn = 21;int map[maxn][maxn];int main() { int n , m; int cas = 1; while(~s 阅读全文
posted @ 2012-07-04 04:28 lenohoo 阅读(186) 评论(0) 推荐(0)
摘要: 题意:有一迷宫,求从入口到出口的最短路、左优先最短路、右优先最短路。算法:bfs+dfsView Code #include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;const int maxn = 50;bool vis[maxn][maxn];char map[maxn][maxn];int ldir[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };int rdir[4][2] = { {-1, 阅读全文
posted @ 2012-07-04 02:54 lenohoo 阅读(150) 评论(0) 推荐(0)
摘要: 题意:已知起点s,求从s出发,依次到达n个点,然后再回到起点s所需的最短路径。分析:dfs,从第一个点开始,枚举所有的边,遍历到头,当枚举完所有的点。View Code #include <cstdio>#include <cstring>#include <iostream>#include <cmath>#include <cstdlib>using namespace std;int x[500] , y[500];bool vis[500];int n;int GetDis(int a, int b) { return abs 阅读全文
posted @ 2012-07-03 20:56 lenohoo 阅读(178) 评论(0) 推荐(0)
摘要: 题意:给你n个数,求出所有6个数的组合数分析:dfsView Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;int b[30];int a[10];int n;void Output() { printf("%d",b[a[0]]); for(int i=1;i<6;i++) printf(" %d",b[a[i]]); printf("\n");}void dfs(int dep, 阅读全文
posted @ 2012-07-03 15:08 lenohoo 阅读(216) 评论(0) 推荐(0)
摘要: 题意: n个人 玩k张牌,你给你上家开始发牌,每发一张,你得把牌首的p张牌放到牌尾去,输出你最终得到的牌的初始位置。分析:模拟View Code #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn =100100;bool b[maxn];int a[maxn];/ 当前已经发到第i张牌,返回下一张牌的编号int GetNextNumber(int i , int p ,int k) 阅读全文
posted @ 2012-07-03 14:18 lenohoo 阅读(570) 评论(0) 推荐(0)
摘要: 题意:从点(0,0)出发,目的地为(x,y),其中有一些点不能通过,求最短距离。分析:bfs View Code #include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;const int maxn = 1010;bool vis[maxn][maxn];int dir[4][2] = { {0,-1},{0,1},{-1,0},{1,0} };struct Node { int x,y,step; };int X ,Y , 阅读全文
posted @ 2012-07-03 02:18 lenohoo 阅读(253) 评论(0) 推荐(0)