
算法
文章平均质量分 78
suressay
这个作者很懒,什么都没留下…
展开
-
最小点覆盖poj 2226
#include#include#includeusing namespace std;const int D=2501;const int maxn=55;int x[maxn][maxn],y[maxn][maxn],ma[D*2],vis[D*2],first[D*2];int X,Y,size,ret,N,M;char map[55][55];struc原创 2012-12-05 16:24:37 · 263 阅读 · 0 评论 -
poj 1637 混合欧拉路
#include#include#includeusing namespace std;const int maxn=205;const int EM=1005;const int INF=100000000;int out[maxn],in[maxn],degree[maxn],N,M,S;int s,t,dist[maxn],Q[maxn],first[maxn原创 2012-12-06 22:33:46 · 316 阅读 · 0 评论 -
poj 2404 中国邮递员
本题为无向图,添加适当的边使得每个点度数都为偶数(有欧拉回路),并且所需费用最小。对于无向图,度为奇数的点一定是成对出现的,并且每两个点之间的边一定只能添加 一次才能是得费用最小。#include#include#includeusing namespace std;const int inf=100000000;int mat[20][20],degree[20],dp[1<<原创 2012-12-07 15:31:50 · 964 阅读 · 0 评论 -
哈密顿回路,状态dp poj2288
状态dp求哈密顿回路,dp[p][u][s]来记录状态,表示当前点为u,父亲点为p,状态为s时的最大价值,cnt[p][i][j]表示到从此状态出发的路径。#include#include#includeusing namespace std;const int maxn=15;const int inf=-1000000000;int dp[14][14][1<<13],firs原创 2012-12-07 21:11:55 · 517 阅读 · 0 评论 -
poj 3260 背包
#include#include#includeusing namespace std;int T,N,VM,v[105],c[105],dp[30000];const int inf=100000000;void ozbackpack(int cost,int weight){ for(int j=T+VM;j>=cost;j--) dp[j]=min(dp原创 2012-12-31 13:04:35 · 523 阅读 · 0 评论 -
poj 3693 重复次数最多的子串
#include#include#include#includeusing namespace std;const int nMax=100005;int r[nMax],a[nMax];int sa[nMax], rank[nMax], height[nMax];int wa[nMax], wb[nMax], wv[nMax], wd[nMax];int cmp(int *r原创 2013-02-04 17:07:03 · 298 阅读 · 0 评论 -
背包 poj2184
将si看成体积,fi看成价值,dp[j]表示si和为j时fi的和的最大值。把负数的区间映射到正数区间。 #include#include#include#include#includeusing namespace std;const int M=100000;int dp[M*2+5],V[105],a[105],b[105],N;int main(){ // f原创 2013-01-22 16:48:23 · 264 阅读 · 0 评论 -
poj 1946 DP
dp[i][j][k] 表示当前领头为i牛,头牛剩余能量为j,已经走了k圈还需要的最小时间,然后记忆化搜索。 #include#include#include#includeusing namespace std;const int inf=1000;int dp[25][105][105],N,D,E;int dfs(int l,int e,int d){ if原创 2013-01-21 17:07:09 · 346 阅读 · 0 评论 -
poj 1948 二维背包
dp[i][j]表示组成两条边长度分别为i,j可不可以。最后找到dp[i][j]为1的i和j值,然后确定第三条边,然后判断三条边可不可以组成三角形,如果可以取最大面积。dp[i][j]|=(dp[i][j] | dp[i-a][j] | dp[i][j-a]) a为当前边长度。#include#include#include#include#includeusing nam原创 2013-01-21 21:55:30 · 89 阅读 · 0 评论 -
poj 1177 经典线段树+扫描线就周长并
可以参考陈宏的论文,就是将矩形的线段按x轴排序后每次利用前后直接线段覆盖y值的差值计算竖线的长度,利用前后两直线直接的x周距离乘不相交线段的条数计算横线的长度。具体可以看代码。本题坐标范围较小,可以不用离散化。#include#include#include#include#includeusing namespace std;const int nMax=5005;cons原创 2013-03-18 20:27:49 · 753 阅读 · 0 评论 -
hdu 2819 二分匹配
首先判断是否能行,只要行与列二分匹配。然后对匹配结果ma按从小到达排序,记录排序时的交换即可。由于M#include#include#include#includeusing namespace std;const int nMax=105;int a[nMax][nMax],ma[nMax],vis[nMax],ans[nMax][2],N;int dfs(int u){原创 2013-06-27 11:05:23 · 477 阅读 · 0 评论 -
hud 2825 AC自动机dp
dp[i][j][k] 表示长度为i,以第j个节点为结尾,当前所用单词状态为k时的结果数目,其中单词只有10个,可以状态压缩。然后利用AC自动机转移。#include#include#includeusing namespace std;const int M=20090717;const int nMax=105;const int SON=26;int dp[30][nM原创 2013-06-27 09:15:18 · 487 阅读 · 0 评论 -
poj 3169 差分约束
有N头牛,给出两组关系,一组表示某两头牛间的最大距离,另一组表示最小距离。典型的差分约束系统,题目中求最大值即转换为求最短路即可。#include#include#include#includeusing namespace std;const long long inf=1000000001;const int maxn=1005;int first[maxn],原创 2012-12-03 22:50:19 · 226 阅读 · 0 评论 -
poj 1990 两颗树状数组
#include#include#includeusing namespace std;const int maxn=20005;long long t1[maxn],t2[maxn],ans,N;int M;struct Cow{ int x,v;}c[20005];int cmp(Cow c1,Cow c2){ return c1.v}原创 2012-12-02 23:52:32 · 259 阅读 · 0 评论 -
poj 1167
给出你一小时完整的公交车时刻表,要求你用规定的公交车数和最少的公交路线将所有的时刻覆盖。先将公交车所有可能的路线初始化处理下,由于是完整的时刻表,则每条公交路线的发车间隔一定>=初始时刻,且每条公交路线最少两辆车,则可得出每条路线的初始时刻的范围为0-29. 然后对路线深搜,加上几个剪枝便可以过了。 注释部分为记录的公交路线的信息,本题没有要求输出这个。#include#inclu原创 2012-08-19 11:30:35 · 862 阅读 · 0 评论 -
poj 3648
一对夫妻举行婚礼,有最多30对夫妻参加,其中新娘新郎必须坐在桌子两侧。新娘看不到她所在一侧的人, 只能看到对面的人。新娘看到一对夫妻坐在一边是不吉利的。除此之外,有些人又通奸关系。看到有通奸关系的人左一边也是不吉利的。求出新娘一侧坐的人。每个人只有两个选择,根据通奸关系建图,然后2—sat求解。0表示新郎,1表示新娘,加一条0—>1的边,这样就可以保证选的一组解一定是新娘一边的人。AC原创 2012-08-18 23:29:14 · 446 阅读 · 0 评论 -
poj 2308
DFS+BFS#include#include#include#includeusing namespace std;struct Node{ int x,y,se,d,c;};queue Q;int move[4][2]={-1,0,1,0,0,-1,0,1};int vis[11][11],mat[11][11],card[4],flag,N,M原创 2012-08-20 13:19:19 · 831 阅读 · 0 评论 -
poj 1947
树状DP+背包过的,得到的子树可以不包括根节点。写的垃圾,跑了600MS+。。。。#include#include#includeusing namespace std;const int maxn=155;int first[maxn],vis[maxn],dp[maxn][maxn][2],in[maxn],f[maxn];int N,P,size;struct原创 2012-08-21 10:28:33 · 290 阅读 · 0 评论 -
poj2044
纠结了半天,巨难看的代码。用的广搜,状态只需记录四个角云的位置即可,这样状态数便十分可观了。AC代码:#include#include#include#includeusing namespace std;int flag[370][10],_map[17][10],squ[10][17],mat[370][17],vis[10][7][7][7][7],N,d_now原创 2012-08-18 23:07:41 · 738 阅读 · 0 评论 -
poj 2312
BFS水题,只有遇到砖块才开炮,只需要开vis[305][305][5]来记录状态即可。注意清空队列即输出-1的情况就行。#include#include#include#includeusing namespace std;const int maxn=305;int gx[4]={-1,1,0,0};int gy[4]={0,0,-1,1};int vis[原创 2012-11-04 00:19:51 · 417 阅读 · 0 评论 -
poj 2823
单调队列#include#include#includeusing namespace std;const int inf=100000000;const int maxn=1000006;int N,k,a[maxn];struct Q{ int d,u;}q[maxn];void getmax(){ int f=1,r=0; q[0]原创 2012-11-05 21:40:42 · 246 阅读 · 0 评论 -
poj 2773
容斥定理+二分 0Ms#include#include#includeusing namespace std;const long long inf=(long long)1int a[30],N,M;long long ret,K;void dfs(int s,int b,int dep,long long m){ if(dep%2) ret+=m/b;原创 2012-11-11 11:22:35 · 349 阅读 · 0 评论 -
poj 2833
给你5000000个数,找出前n1个最大 的和前n2个最小的,由于n1和n2都很小,直接维护两个数组保存要求的最大最小值,每输入一个数字更新两个数组就行了。 #include#includeusing namespace std;const int inf=100000009;int n1,n2,n,ma[11],mi[11];int main(){// fre原创 2012-11-11 22:34:25 · 615 阅读 · 0 评论 -
poj 3233
经典题,两次二分。不知道为什么N取的稍大一点时进入函数时会调用chkstk.asm,让我恶心了很久也不知道为啥,不过不影响结果。#include#include#includeusing namespace std;const int N=33;int n,M,K;struct Mat{ int mat[N][N],n,m; void init(int r,原创 2012-11-15 23:41:55 · 346 阅读 · 0 评论 -
poj 3445
水题,算出结果直接递归输出就行了。#include#include#includeusing namespace std;int a[20],hash[100000],T;void init(){ a[0]=2;hash[2]=0; int sum=a[0]; for(int i=1;i { a[i]=sum+i+1; sum+=a[i];原创 2012-11-16 19:46:07 · 287 阅读 · 0 评论 -
poj 1988 并查集
在节点中加入一个pos记录每个节点到当前根节点的距离,cnt记录根节点下cube数量。每次询问结果为t[par].cnt-t[a].pos-1,par为a的父亲节点#include#include#includeusing namespace std;const int maxn=30005;int P;struct Tree{ int p,cnt,pos;原创 2012-12-02 09:35:26 · 248 阅读 · 0 评论 -
poj 3683
一个小镇里面只有一个牧师,同时镇里有多对情侣要举办婚礼,婚礼中有一个仪式必须要牧师主持,仪式可以有两个时间可以选择。牧师每次只能主持一个婚礼。求解一个方案,使得每对情侣都得以举办婚礼。枚举每两对情侣,根据两个时间的冲突关系建图,然后2—sat求解即可。AC代码:#include#include#include#includeusing namespace std;i原创 2012-08-18 23:32:43 · 830 阅读 · 0 评论