
algorithm
文章平均质量分 75
GreenHand
喜欢编程,喜欢象棋,喜欢台球。
追寻同道中人
展开
-
双向广搜
<br />#include<iostream>#include<cmath>#include<queue>using namespace std;const int maxn = 301;int n;int used[maxn][maxn];int g[maxn][maxn];int d[8][2] = { {-2,-1}, {-2,1}, {-1,-2}, {-1,2}, {1,-2}, {1,2}, {2,-1}, {2,1} };//int x[] = {-2,-原创 2011-05-30 20:00:00 · 2082 阅读 · 0 评论 -
最大m子段和
//最大m字段和问题// 设b[I,J]表示前j项的i个字段和的最大值//b[i,j] = max{b[i,j-1],b[i-1,j-1]+a[j]}#includeusing namespace std;int a[100001];int solve(int m,int n);int main(){ int m,n; while(cin >> n >> m原创 2011-06-04 09:17:00 · 1138 阅读 · 0 评论 -
最大子矩阵
/*22:15:07 Accepted 1001 15 MS 224 KB Visual C++*/#include #define max(a, b)(a>b ? a:b)const int maxint = 2147483647;int a[101][101];int MaxSeq(int n, int *b){ int sum = -maxint, cu原创 2011-06-04 09:16:00 · 435 阅读 · 0 评论 -
DP最大子序列
#includeusing namespace std;//07:48:47 Accepted 1007 62 MS 676 KB Visual C++#define max(a, b)(a>b ? a:b)const int maxint = 100000001;int a[100001];int s=1, e=1;int SubMax(int n, int *a原创 2011-06-04 09:14:00 · 466 阅读 · 0 评论 -
蛇形添数
<br />#include<iostream>using namespace std;int main(){ int n; int a[100][100]; int i,j,t; while(1) { cin >> n; memset(a,0,sizeof(a)); t = a[1][n] = 1; int x = 1,y=n; while(t<n*n) { while(x<n&&!a[x+1][y]) a[++x][y] = +原创 2011-05-30 21:54:00 · 501 阅读 · 0 评论 -
大组合数
<br />/*求小数的组合数n<3000我们还是可以直接用杨辉三角搞定,但是如果超过3000的数,开个二维数组很不实际,故 我们要寻求一个直接的算法 这里要用到整数的拆分,快速幂模以C(n,m)为例C(n,m)=n!/((n-m)!*m!)首先扰我们的要算比较大的数的阶乘了我们可以先将n!用快速拆分法分为 2^x1*3^x2*5^x3*...*pi^xi(其中p为素数)方法见div函数。(证明有点难度- -!)先不在此证明了拆分后对分式进行降幂运算x1[i]-=(x2[i]+x3[原创 2011-05-30 21:50:00 · 729 阅读 · 0 评论 -
多重背包问题
<br />有N种物品和一个容量为V的背包。第i种物品最多有n[i]件可用,每件费用是c[i],值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。进一步优化,由于有数量约束,可以先将v[i]/w[i]比值按由大到小的顺序排列,如用此法,应该用结构体做数据结构,然后用qsort排序,此处用到了qsort排序的技巧其实就是01背包 不过是加入了每件物品的数量#include<iostream>#include<fstream>using nam原创 2011-05-30 20:03:00 · 2865 阅读 · 2 评论 -
算法导论-DP总结
<br />算法导论动态规划小结:找出最有子结构,其次证明问题具有最有子结构,最后总结出状态转移方程,分析边界情况。#include<iostream>using namespace std;//////////////////////////////////////////矩阵相乘 O(n^3)int sum = 0;int a[100][100] = {0};int b[100][100] = {0};int c[100][100] = {0};int Matri原创 2011-05-30 20:02:00 · 959 阅读 · 0 评论 -
迷宫问题(唯一路径)
<br />#include<iostream>using namespace std;const int MAX = 100;int maze[MAX][MAX];int path[MAX][MAX];int startRow,startCol;int endRow,endCol;bool flag = false;int x[] = {-1,0,1,0};//up right down leftint y[] = {0,1,0,-1};//唯一路径解法bool原创 2011-05-30 20:01:00 · 840 阅读 · 0 评论 -
二分图最大匹配(匈牙利算法)
<br />#include<iostream>#include<string.h>using namespace std;const int N = 120;int g[N][N]; int linked[N];//linked[i] 代表与i连接的顶点 i是right集合中的顶点 linked[i]是left中的顶点bool visit[N]; //顶点i是否访问int n,m; //n的left集合的顶点数 m是right集合的顶点数bool DFS(int x)//原创 2011-05-30 19:55:00 · 446 阅读 · 0 评论 -
Dijkstra+heap
<br />#include<iostream>#include<queue>#include<vector>using namespace std;#define INF 0x3f3f3fconst int maxn = 101;int g[maxn][maxn];int vis[maxn];int dis[maxn];struct node{ int t, w; node( int tt, int ww) : t(tt), w(ww){}原创 2011-05-30 19:52:00 · 681 阅读 · 0 评论 -
排列组合总结
<br />/*排列组合与回溯算法所谓回溯:就是搜索一棵状态树的过程,这个过程类似于图的深度优先搜索(DFS),在搜索的每一步(这里的每一步对应搜索树的第i层)中产生一个正确的解,然后在以后的每一步搜索过程中,都检查其前一步的记录,并且它将有条件的选择以后的每一个搜索状态(即第i+1层的状态节点)。排列:就是从n个元素中同时取r个元素的排列,记做P(n,r)。(当r=n时,我们称P(n,n)=n!为全排列)例如我们有集合OR = {1,2,3,4},那么n = |OR| = 4,切规原创 2011-05-30 22:09:00 · 1238 阅读 · 0 评论 -
qsort用法总结
/*六类qsort排序方法前一段时间做题觉得qsort函数很好用,但有时不太会用比如按结构体一级排序、二级排序、字符串排序等,故通过查资料将其整理一番。以下是其具体分类及用法(若无具体说明是以降序排列):*//*1、对一维数组排序:(Element_type是一位数组中存放的数据类型,可以是char, int, float, double, etc )使用qsort之前,必须自己定义一个比较函数。这个比较函数用于比较两个元素的大小。由于qsort可以排序任意数据类型,包括自定原创 2011-05-30 21:55:00 · 613 阅读 · 0 评论 -
priority_queue+BFS
<br />//题意: row*col的图, ‘X ’代表可行走, ‘.’代表不能走,输入起始位置和终止位置,如果相邻的不是‘X’,要将‘.’处理掉 , 变成‘X’,求出从起始点到终止点的最少处理‘.’的数//要用优先级队列 将最少处理的顶点放在队头#include<iostream>#include<queue>using namespace std;// Accepted 1006 546 MS 12460 KB Visual C++ const in原创 2011-05-30 19:57:00 · 618 阅读 · 0 评论 -
素数测试
<br />#include <stdio.h>#include <stdlib.h>typedef unsigned __int64 hugeint;//求出最大公约数hugeint gcd(hugeint A, hugeint B){ while (A != 0) { hugeint C = B % A; B = A; A = C; } return B;}//求a*b%c,要求:a原创 2011-05-30 20:08:00 · 534 阅读 · 0 评论 -
朴素dijkstra算法
<br />#include<iostream>#include<fstream>using namespace std; const int INF = 0X3f3f3f;const int maxn = 101;int g[maxn][maxn];int prev[maxn];int vis[maxn];int cost[maxn];void dijkstra(int src, int V){ int i, j, minc = INF; f原创 2011-05-30 19:54:00 · 485 阅读 · 0 评论 -
深度搜索与追girl
<br />追girl适合用广搜,找到一个合适的后,就应该把所有备胎放入你的储蓄盒中。然后继续深入,深入的同时再把备胎放入储蓄盒,恩……这样不但能利用不同的时间和不同的girlhappy,还不耽误产量。要用深度搜索就遭了,那就大杯具了,一条路走到黑,到最后和你说我不是你要的,玩完了,大大杯具,还得回去好一个,再一条路走到黑,那不还纠结死,世上最痛苦的事也就这样了!原创 2011-05-30 20:21:00 · 481 阅读 · 0 评论 -
二分图完备匹配必须边
/************************************************************************/|找构成二分图完备匹配所必须的边 |思路: 枚举所有边,如果去掉当前边不能构成完备匹配,则是必须的边,否则不是原创 2011-06-09 15:46:00 · 1445 阅读 · 1 评论