摘要:深度优先搜索1.深度优先搜索先辈子图 Gπ=(V,Eπ),其中Eπ={(π[v] ,v): v属于V且π[v]!=NIL}2.加盖时间戳每个顶点v有两个时间戳,当顶点v第一次被发现时(置为灰色),记录第一个时间戳d[v];当顶点第二次被发现时,置第二个时间戳f[v](灰色的都是栈里边的)3.对|V|中顶点的每一个,都对应一个发现事件和完成事件,每一个顶点u都有d[u]<f[u]。这些时间戳为1...
阅读全文
摘要:广度优先搜索(O(V+E)) 1.白色:未发现 灰色:发现和未发现之间 黑色:发现 2.颜色color[u]中 父母π[u]中 顶点源点之间的距离d[u] 队列Q 3.BFS(G,s) { For each vertex u 属于V[G]-{s} Do color[u]=white d[u]=无穷 π[u]=NIL color[s]=gray; d[s]=0; π[s]=NIL; Qß0 Enque(Q,s) While Q!=空 { Do ußdeque(Q) For each v属于adj[u] Do if color[v]=white Then color[v]=gr
阅读全文
摘要:图的表示课后题答案 1.课后题22.1-1 给一个图的邻接表,计算出每个顶点的出度需要多长时间?计算出每个顶点的入度需要多长时间? 出度:O(V+E) 入度:O(VE) 2.课后题22-1-3 求图G=(V,E)的转置图G=(V,ET),也就是将图G中所有边反向 (1)矩阵O(V2) for (i=1; i<=V; i++) for(j=i+1; j<=V; j++) if(A[i][j] && !A[j][i]) { A[i][j]=0; A[j][i]=1; } (2)邻接表O(VE)Allocate V list pointers for GT (Adj
阅读全文
摘要:Bellman-ford 算法1.运行bellman算法后可返回一个布尔值,表明图中是否存在一个从原点可达的负权回路,若存在,无解;不存在,返回最短路径及权值2.此法运用松弛技术不断减少从源点s到v的最短路径权的估计值d[v],直至达到最短路径实际的值σ(s,v),算法返回true当且仅当不包含从原点可达的负权回路BELLMAN-FORD(G,w,s) INITIALIZE-SINGLE-SOURCE(G,s) For i=1 to |V[G]|-1 Do for each edge(u,v)属于E[G] Do relax(u,v,w) For each edge(u,v)属于E[G] ...
阅读全文
摘要:单源最短路径1.边权可以为时间、费用、罚款、损失或者任何其他沿一条路径线性积累的和我们试图将其最小化的某个量2.单源最短路径的变体:(1)单终点最短路径问题:找出每个点到指定终点的最短路径解决方法:把图中每条边反向,可转换为单源最短路径(2)单对顶点最短路问题:对每个顶点u,v找出从u到v的最短路径解决方法:从渐进意义看,目前没有比单源算法更快的算法解决这个问题(3)每对顶点间的最短路径问题:下一节介绍3.最短路径最优子结构对任一带权有向图G=(V,E)所定义的权函数为w:E->R。设p=<v1,v2,…,vk>是从v1到vk的最短路径。对任意i,j其中1<=i<
阅读全文
摘要:给一个数组,求非递减数列的个数,单个数也算一个dp[a[i]]=sum(dp[a[j]]) j<i && a[j]<a[i],意为最后一个数a[i]的非递减数列若用线性方法,每一个a[i],都要去前边找比a[i]小的数,求和。O(N*N)超时用树状数组求解,注意要离散化 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 6 using namespace std; 7 typedef lon
阅读全文
摘要:方法一:树状数组1.树状数组讲解http://www.cnblogs.com/inpeace7/archive/2012/04/11/2441352.html2.离散化 输入a[i] i 1 2 3 4 5 6 7 a[i] 50 60 80 90 20 100 50 p[i] 2 3 4 5 1 6 2把a[i]变为p[i]即为离散化方法:定义结构体,存储a[i]和i,按照a[i]排序,p[node[1].data]=1;int cnt=1;for(i=1;i<n;i++){ if(node[i-1].data=node[i...
阅读全文
摘要:菜鸟一个,说的不好还望指点 去年学的树状数组,现在都忘没了,复习一下。 给你一组数据,例如a[1],a[2],a[3],...a[k],..a[n],求任意一段区间和(比如说从a[i]---a[j]的所有数的和),线性求解,时间复杂度O(n),规模较大时,会TLE。怎么办?思路:以空间换时间。提前存储一些数据的和。怎么存储会节省时间? 方法一:构造一个和数组sum,每次输入第i个数据,就计算一下前i个数的和sum[i]。当求取前i个数和时,直接输出sum[i],O(1)时间复杂度,不很好吗。可是。。。如果我想更新一下第j个数据,那么就得连带着更新sum[j],sum[j+1],...su...
阅读全文
摘要:参考http://blog.csdn.net/jun_sky/article/details/7008625 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 using namespace std; 5 struct node 6 { 7 int x,y,p; 8 char c; 9 }b[70],w[70];10 int in(char c)11 {12 switch(c)13 {14 case 'K':case 'k':return 1
阅读全文
摘要:水题 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 int n,m; 7 8 bool out(int x,int y){if((x>=0 && x<n)&&(y>=0 && y<m))return 0;return 1;} 9 10 int main()11 {12 int start;13 bool visit[12][12];14 char c[
阅读全文
摘要:模拟题,注意两点1.坐标2.L,R后边的数字是向左或向右转几次 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 const int maxx=102; 6 int loc[maxx][maxx],a,b; //loc 地图第ij个位置是第几个机器人 7 char dir[maxx][maxx]; 8 struct OPE //操作 9 { 10 int ind; 11 char op; 12 ...
阅读全文
摘要:模拟能力太水,做了挺长时间还原括号数组,令r=1,num=0,从“(”向前扫,遇到“(”,r--,num++;遇到“)”r++,r为0是num即为解 1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 int n,t,r,num,i,j,b[25]; 6 bool c[100]; 7 scanf("%d",&t); 8 for(int tt=1;tt<=t;tt++) 9 {10 memset(c,0,sizeof(c));11 scanf("%d&quo
阅读全文
摘要:前缀表达式求值,和后缀一样,只不过是从后往前读 1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <stack> 5 #include <algorithm> 6 using namespace std; 7 8 bool change(char s[],bool val[]) 9 {10 stack <bool> nd;11 bool a,b;12 int len=strlen(s),i,j;13 for(i=0;i&l
阅读全文
摘要:表达式求值,注意:输入可能有\t!!!!没有考虑到,re到死代码: 1 #include <iostream> 2 #include <stack> 3 #include <string.h> 4 #include <cstdio> 5 using namespace std; 6 const int maxx=1000; 7 int precede(char c) 8 { 9 switch(c) 10 { 11 case '+': 12 case '-':return 1; 13 case '*'
阅读全文
摘要:1 #include <iostream> 2 #include <stack> 3 #include <string.h> 4 #include <stdio.h> 5 #include <cmath> 6 using namespace std; 7 8 const int maxx=10000; 9 const int inf=0x7fffffff; 10 11 int precede(char c) 12 { 13 switch(c) 14 { 15 case '+': 16 case '-':
阅读全文
摘要:题意:每个月会给出一个财务报告:赢利或者亏空 如果赢利则赢利s,如果亏空则亏空d(12个月都一样,只有赢利s或者亏空d两种情况)每五个月也会给出一个报告(1~5 ,2~6 。。。)一年一共有8次这样的报告,已知这8次都报告亏空问整年情况:如果亏空则输出Deficit,如果赢利,输出整年可能赢利的最大值可能情况(1)ssssd ssssd ss d>4s ===>d>4s 盈利:10s-2d (2)sssdd sssdd ss 2d>3s ===>d>3/2s 盈利:8s-4d (3)ssddd ssddd ss 3d>2s ==...
阅读全文
摘要:贪心。以每个岛屿为圆心,在x轴上求出雷达可能出现的区间,重叠的区间只用一个雷达 1 #include <iostream> 2 #include <algorithm> 3 #include <cmath> 4 #include <stdio.h> 5 using namespace std; 6 typedef double ll; 7 const int maxx=1000; 8 struct PO 9 {10 ll l,r;11 };12 PO point[maxx+5];13 14 bool cmp(PO a,PO b)15 {16 if
阅读全文