随笔分类 - POJ
摘要:刚开始用的多重背包写的结果果然超时,后来用别人告诉我的把它用二进制压缩一下,这里附上资料:解题思路:题目给价值为1~6的六种大理石的个数若干,要求我们判断是否能够把石头平分成相等的价值。我的思路是这样的:将大理石的重量看成和价值相等,那么总容量等于总价值数sum,那么如果总容量为sum/2时能装的最大价值也为sum/2,那么说明能拆分也两份相等的价值。注意:此题用背包要压缩,否则会超时。背包九讲的第三讲中提到了压缩方法,我贴出来:P03:多重背包问题每种物品有一个固定的次数上限题目有N种物品和一个容量为V的背包。第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i]。求解将哪些物品
阅读全文
摘要:思路很简单,给你n个数和一个sum要求从n个数中选出所有加和为sum的数值串:肯定是先用DFS找出所有的情况然后再删掉重复的;这里我从课件里学到一种判断重复的方法,把核心代码附上:void findSum (int sum, int iEnd, int numSkipped) { if (iEnd == listlength) return; int newsum = sum - list[iEnd]; //取第iEnd个数 if ((numSkipped != list[iEnd])&&(newsum >= 0)) { used[iEnd] = 1; //标记第iEnd
阅读全文
摘要:求周长,一开不知道怎么求,最后钻到POJ讨论区内,看到一种方法,就是‘X"周围全换成0,最后只要求0的个数就好了嗯,这种方法真好,反正我们想起来,膜拜啊!这题过了从网上搜了下发现可以用BFS做,想想也是,本身用那一种都行View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #include<ctype.h> 6 7 int map[25][25],f[25][25],visit[25][25],
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #include<ctype.h> 6 7 char map[101][101]; 8 int visit[101][101],m,n; 9 int dx[] = {0,1,-1, 0,1, 1,-1,-1}; 10 int dy[] = {1,0, 0,-1,1,-1,-1, 1};11 12 void DFS(int x,int y)13 {14
阅读全文
摘要:题目的本意应该是叫你求出最小生成树的最大边,和边的个数,最后把每条边的连接情况打印出来。View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 #define N 15001 6 7 int father[1001];//按照模板敲的 8 int p[N], n, m; 9 typedef struct 10 {11 int x, y;12 int w; 13 }edge;14 edge e[N];15 16 int
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 760 5 #define Maxint 99999999 6 7 double lowcost[N], c[N][N]; 8 double x[N], y[N]; 9 int towns, m, s[N], closest[N],path[N][N];10 11 double distance(int i,int j)//用double为了防止int数据存不下12 {13 return (x
阅读全文
摘要:1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<iostream> 5 6 using namespace std; 7 8 char map[30][30]; 9 int vis[30]; 10 int dir[4][2] = {1,0,-1,0,0,-1,0,1}; //搜索的四个方向,这里用数组表示,为了下面表示方便11 int ans, row, col, loc; //loc表示搜索的深度。12 13 int Inmap(int nr,in
阅读全文
摘要:1 #include<stdio.h> 2 3 int num[21][21][21] = {0}; 4 5 int w(int a,int b,int c) 6 { 7 if (a<=0 || b<=0 || c<=0) return 1; 8 if(a>20 || b>20 || c>20) return w(20,20,20); 9 if(num[a][b][c]) return num[a][b][c];10 11 if(a<b && b<c) return num[a][b][c] = w(a,b,c-1)+
阅读全文
摘要:1 #include<stdio.h> 2 int w[21][21][21]; 3 int main() 4 { 5 int a,b,c; 6 7 for(a=0; a<21; a++) 8 for(b=0; b<21; b++) 9 for(c=0; c<21; c++)10 {11 if(a<=0||b<=0||c<=0)12 w[a][b][c] = 1;13 else if(a<b && b<c)14 w[a][b][c] = w[a][b][c-...
阅读全文
摘要:1 #include<iostream> 2 #include<queue> 3 #define MAXV 100000 4 using namespace std; 5 int sign[100001],len[100001]; //sign是用来做标记的,len用来走到重点需步长 6 int main() 7 { 8 int N, K, y; 9 cin >> N >> K;10 queue<int>Q;11 Q.push(N);12 len[N] = 0;13 while(!Q.empty())14 {15 ...
阅读全文

浙公网安备 33010602011771号