zrq495
www.zrq495.com
摘要: 动态规划。注意输入数据中有空格。代码如下: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 5 using namespace std; 6 7 int len[1000][1000]; 8 9 int main()10 {11 int i, j, alen, blen;12 char s1[1000], s2[1000];13 while(gets(s1))14 {15 gets(s2);16 alen=strlen(s1);17 ... 阅读全文
posted @ 2012-08-10 19:21 zrq495 阅读(161) 评论(0) 推荐(0) 编辑
摘要: 线段树。代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #define max 2000002 4 5 using namespace std; 6 7 typedef struct 8 { 9 int l, r;10 int val;11 }node;12 13 node t[4*max];14 15 void build(int l, int r, int rt)16 {17 t[rt].l=l;18 t[rt].r=r;19 if (l == r)20 {21 scanf(... 阅读全文
posted @ 2012-08-09 21:15 zrq495 阅读(150) 评论(0) 推荐(0) 编辑
摘要: 线段树。代码如下: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define max 50002 5 6 using namespace std; 7 8 typedef struct 9 {10 int l, r;11 int val;12 }node;13 14 node w[4*max];15 16 void pushup(int rt)17 {18 w[rt].val=w[rt<<1].val+w[rt<<1|1].val;19 }20 21 v 阅读全文
posted @ 2012-08-09 21:14 zrq495 阅读(171) 评论(0) 推荐(0) 编辑
摘要: 判断有无负环。。最短路径之 spfa算法bellman-ford算法spfa算法代码:队列的数组开小了,一直RE。。。。也可以用循环队列。View Code 1 #include<iostream> 2 #include<cstring> 3 #define MAX 0xfffffff 4 5 using namespace std; 6 7 int map[502][502]; 8 int que[1000000]; 9 10 int spfa(int n)11 {12 int h, t;13 int dis[502];14 int i, num[502], v... 阅读全文
posted @ 2012-08-08 11:11 zrq495 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 题意表示看不懂,英语太差。。。 看了别人的题意,大意是在一群人中散布传言,要求时间最短。先输入消息传递的人数n, 接下来n行, 每行第一个数m,然后m组关系(编号和时间)。flody 算法。代码如下: 1 #include<iostream> 2 #include<cstring> 3 #define MAX 9999999 4 5 using namespace std; 6 7 int map[102][102]; 8 9 void floyd(int n)10 {11 int i, j, k;12 for (k=1; k<=n; k++)13 for... 阅读全文
posted @ 2012-08-07 19:00 zrq495 阅读(132) 评论(0) 推荐(0) 编辑
摘要: 最短路.dijkstra 算法.代码如下: 1 #include<iostream> 2 #include<cstring> 3 #define MAX 9999999 4 5 using namespace std; 6 7 int n, m; 8 int map[202][202]; 9 int vis[202]; //标记是否更新 1为更新10 int dir[202]; //到出发点的距离11 12 void dij(int s)13 {14 int i, j;15 int pos, min;16 memset(vis, 0, sizeof(v... 阅读全文
posted @ 2012-08-07 16:13 zrq495 阅读(432) 评论(0) 推荐(0) 编辑
摘要: 最小生成树。prim算法。要问while(scanf("%d", &n)) 和 while(~scanf("%d", &n))、while(scanf("%d", &n)==1)、while(~scanf("%d", &n)!=EOF) 的区别有多大??答 :前者TLE ,后者 15MS。。。。 不信可以试试。。以后再也不敢简写了。。。代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #include<cst 阅读全文
posted @ 2012-08-06 21:23 zrq495 阅读(171) 评论(0) 推荐(0) 编辑
摘要: 最小生成树。Prim 算法。代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int map[102][102]; 8 9 int prim(int n)10 {11 int i, j, min, sum=0;12 int pos, vis[102], d[102];13 memset(vis, 0, sizeof(vis));14 for (i=1; i<=n; i++)15 d[i]=map[1][i]... 阅读全文
posted @ 2012-08-06 20:10 zrq495 阅读(186) 评论(0) 推荐(0) 编辑
摘要: 最小生成树.Kruskal 算法代码: 1 #include<iostream> 2 #include<cstdlib> 3 #include<cstdio> 4 5 using namespace std; 6 7 typedef struct 8 { 9 int a, b;10 int d;11 int flag;12 }node;13 14 int set[102];15 node w[5002];16 17 int cmp(const void *a, const void *b)18 {19 node *pa=(node *)a;20 no... 阅读全文
posted @ 2012-08-06 20:08 zrq495 阅读(106) 评论(0) 推荐(0) 编辑
摘要: 给定一个带权的无向连通图,如何选取一棵生成树,使树上所有边上权的总和为最小,这叫最小生成树.求最小生成树的算法(1)克鲁斯卡尔算法图的存贮结构采用边集数组,且权值相等的边在数组中排列次序可以是任意的.该方法对于边相对比较多的不是很实用,浪费时间.(2)普里姆算法图的存贮结构采用邻接矩阵.此方法是按各个顶点连通的步骤进行,需要用一个顶点集合,开始为空集,以后将以连通的顶点陆续加入到集合中,全部顶点加入集合后就得到所需的最小生成树 .下面来具体讲下:克鲁斯卡尔算法方法:将图中边按其权值由小到大的次序顺序选取,若选边后不形成回路,则保留作为一条边,若形成回路则除去.依次选够(n-1)条边,即得最小生 阅读全文
posted @ 2012-08-06 20:05 zrq495 阅读(161) 评论(0) 推荐(0) 编辑
摘要: 给定一个带权的无向连通图,如何选取一棵生成树,使树上所有边上权的总和为最小,这叫最小生成树.求最小生成树的算法(1)克鲁斯卡尔算法图的存贮结构采用边集数组,且权值相等的边在数组中排列次序可以是任意的.该方法对于边相对比较多的不是很实用,浪费时间.(2)普里姆算法图的存贮结构采用邻接矩阵.此方法是按各个顶点连通的步骤进行,需要用一个顶点集合,开始为空集,以后将以连通的顶点陆续加入到集合中,全部顶点加入集合后就得到所需的最小生成树 .下面来具体讲下:克鲁斯卡尔算法方法:将图中边按其权值由小到大的次序顺序选取,若选边后不形成回路,则保留作为一条边,若形成回路则除去.依次选够(n-1)条边,即得最小生 阅读全文
posted @ 2012-08-06 20:02 zrq495 阅读(103) 评论(0) 推荐(0) 编辑
摘要: 最小生成树。Kruskal 算法:其中应用并查集。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 5 using namespace std; 6 7 typedef struct 8 { 9 int a, b;10 int d;11 }node;12 13 node w[5002];14 int set[102];15 16 int cmp(const void *a, const void *b)17 {18 node *x=(node *)a;19 node *y=(node 阅读全文
posted @ 2012-08-06 19:57 zrq495 阅读(392) 评论(0) 推荐(0) 编辑
摘要: WA 了好几次,只保留一位数不够。代码如下: 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 long long a[10001]; 7 8 int main() 9 {10 long long n, i, t;11 a[0]=1;12 for (i=1; i<=10000; i++)13 {14 t=a[i-1]*i;15 while(t%10 == 0)16 t/=10;17 a[i]=t%1... 阅读全文
posted @ 2012-08-05 21:44 zrq495 阅读(182) 评论(0) 推荐(0) 编辑
摘要: 2009=287*7=41*49=41*7*7。所以当n>=41时,n!%2009=0。n<41时,用公式:(a*b)%c = ((a%c) * (b%c)) % c。代码如下: 1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 long long n, i ,s; 8 while(cin >> n) 9 {10 if (n == 0)11 {12 cout << "1" << endl;13 conti... 阅读全文
posted @ 2012-08-04 21:33 zrq495 阅读(176) 评论(0) 推荐(0) 编辑
摘要: 并查集简单应用。和HDU 1232 一样。代码如下: 1 #include<iostream> 2 3 using namespace std; 4 5 int set[1010]; 6 int cnt; 7 8 int find(int a) 9 {10 int x=a;11 while(x != set[x])12 x=set[x];13 return x;14 }15 16 void merge(int a, int b)17 {18 int x=find(a);19 int y=find(b);20 if (x!=y... 阅读全文
posted @ 2012-08-04 21:19 zrq495 阅读(140) 评论(0) 推荐(0) 编辑