上一页 1 2 3 4 5 6 7 8 9 10 ··· 57 下一页

2011年8月24日

SPFA算法之一

摘要: //poj 1511 Invitation Cardstle//有p个点和e条边,从第一个点出发,求到每个点再回来所需要的最短路之和。边是有向边。//1 <= p,e<= 1000000 显然邻接矩阵是不可能存下来的,所以用邻接表//先正向建边求一次最短路径(从顶点1到其他顶点),再反向建边再求一次最短路径,两次求和#include<iostream> using namespace std;struct Edge { int index; int weight; Edge *next;}Vertex[1000002]; ... 阅读全文

posted @ 2011-08-24 16:07 sysu_mjc 阅读(236) 评论(0) 推荐(0) 编辑

RMQ区间最值

摘要: //poj 3264 #include <iostream> //线段树,求区间的最值问题using namespace std;struct segment{ int max,min;}table[200000]; //4*50000int arr[50005],MAX,MIN;void built_tree(int s,int t,int n){ if(s==t) table[n].max=table[n].min=arr[s]; else { int mid=(s+t)/2; built... 阅读全文

posted @ 2011-08-24 16:03 sysu_mjc 阅读(194) 评论(0) 推荐(0) 编辑

RMQ的ST算法

摘要: //poj 3264 Balanced Lineup#include<iostream> //ST算法,即(Sparse_Table ,稀疏表),可以在O(nlogn)的预处理以后,实现O(1)的查询效率#include <cmath>using namespace std;const int max_n=50005;int arr[max_n],n,t,l,r; //原数列arr从下标1开始int rmq_m[max_n][16],rmq_n[max_n][16]; //16>=log2(50000)//rmq_m[i][j],rmq_n[i][j]... 阅读全文

posted @ 2011-08-24 16:02 sysu_mjc 阅读(339) 评论(0) 推荐(1) 编辑

Floyd 算法

摘要: #include <iostream> //每对顶点之间的最短路径,弗洛伊德(Floyd)算法using namespace std; const int MaxWeight=100000; int Vertex,edge[1000][1000],distF[1000][1000],i,j;void init(){ for(i=1;i<=Vertex;i++) //结点坐标都是从1开始的 for(j=1;j<=Vertex;j++) { if(... 阅读全文

posted @ 2011-08-24 16:01 sysu_mjc 阅读(173) 评论(0) 推荐(0) 编辑

迭代加深搜索

摘要: Search Techniques搜索方式译 by Lucky Crazy样例:n皇后问题 [经典问题]将n个皇后摆放在一个n x n的棋盘上,使得每一个皇后都无法攻击到其他皇后。深度优先搜索 (DFS)显而易见,最直接的方法就是把皇后一个一个地摆放在棋盘上的合法位置上,枚举所有可能寻找可行解。可以发现在棋盘上的每一行(或列)都存在且仅存在一个皇后,所以,在递归的每一步中,只需要在当前行(或列)中寻找合法格,选择其中一个格摆放一个皇后。1search(col)2iffilledallcolumns3printsolutionandexit4foreachrow5ifboard(row,col) 阅读全文

posted @ 2011-08-24 08:18 sysu_mjc 阅读(1356) 评论(0) 推荐(1) 编辑

2011年8月23日

最长公共子序列

摘要: #include<iostream> //poj 2250 Compromise#include<vector>#include<string> using namespace std;vector<string> v1,v2,res;int table[105][105],tag[105][105];int dp(int i,int j){ if(i==-1||j==-1) return 0; if(table[i][j]!=-1) return table[i][j]; else if(v1[i]==v2[j]... 阅读全文

posted @ 2011-08-23 17:19 sysu_mjc 阅读(167) 评论(0) 推荐(0) 编辑

折半插入排序

摘要: #include<iostream>using namespace std;//折半插入排序,关键字的比较次数由于采用了折半查找而减少,但元素的移动次数仍不变const int Maxsize=10;int list[Maxsize];void binasort(int list[],int n){ int i,j,t,low,high,mid; for(i=1;i<n;i++) { t=list[i]; low=0;high=i-1; while(low<=high) { mid=(low+hig... 阅读全文

posted @ 2011-08-23 17:17 sysu_mjc 阅读(197) 评论(0) 推荐(0) 编辑

直接插入排序

摘要: #include <iostream> //直接插入排序using namespace std;void insert_sort(int *x, int n) { int i, j, t; for (i=1; i <n; i++) /*要选择的次数:1~n-1共n-1次*/ { /* 暂存下标为i的数。注意:下标从1开始,原因就是开始时 第一个数即下标为0的数,前面没有任何数,单单一个,认为 它是排好顺序的。 */ t=*(x+i); for (j=i-1; j>=0 && t <*(x+j); j--) /*注意:j=i-1,j--,这里就是下标 阅读全文

posted @ 2011-08-23 17:17 sysu_mjc 阅读(152) 评论(0) 推荐(0) 编辑

希尔排序

摘要: #include<iostream>using namespace std;//当k取1时,就等同于直接插入排序方法,//由于前边大增量的处理,使数组大体有序,因此最后一趟排序移动的记录少,处理速度快void insert_sort(int *x, int n) { int i, j, t,k; k=n/2; while(k>=1) { for (i=k; i <n; i++) { t=*(x+i); for (j=i-k; j>=0 && t <*(x+j); j=j-k) { ... 阅读全文

posted @ 2011-08-23 17:15 sysu_mjc 阅读(122) 评论(0) 推荐(0) 编辑

完全背包之四

摘要: //poj 1252 Euro Efficiency//给出6枚不同面值(在1到100之间)的硬币,通过 加减 凑出1到100的钱数,我们关心的是最少要用到几枚硬币,//最后求出平均值,并找出其中最大值#include<iostream> //完全背包 参考 poj9 3260 The Fewest Coins#include<stdio.h>#include<algorithm>using namespace std; int main() { int cases,euro[10],dp[20000],maxn; cin>>cases; whi 阅读全文

posted @ 2011-08-23 17:13 sysu_mjc 阅读(116) 评论(0) 推荐(0) 编辑

上一页 1 2 3 4 5 6 7 8 9 10 ··· 57 下一页

导航