2011年8月22日

冒泡排序

摘要: #include<iostream>using namespace std;const int Maxsize=10;int list[Maxsize];void bubblesort(int list[],int n){ int i,j,tag=1,x; for(i=n-1;i>0&&tag;i--) { tag=0; for(j=0;j<i;j++) if(list[j]>list[j+1]) { x=list[j];list[j]=list[j+1];list[j+1]... 阅读全文

posted @ 2011-08-22 15:10 sysu_mjc 阅读(115) 评论(0) 推荐(0)

快排之二

摘要: #include <iostream>using namespace std;struct node { int index; int num;}list[100000];void QuickSort(int beg,int end){ if(beg<end) { int low=beg,high=end; node pivot=list[beg]; while(low < high) { while(low<high && list[high].num>=pivot.num) ... 阅读全文

posted @ 2011-08-22 15:09 sysu_mjc 阅读(155) 评论(0) 推荐(0)

快排之三

摘要: #include<iostream>#include<time.h>using namespace std;//递归实现快排void QuickSort(int pData[],int left,int right){ int i=left,j=right,key=pData[left]; do { while(i<j&&pData[j]>key) j--; if(i<j) { pData[i]=pData[j]; i++; } while... 阅读全文

posted @ 2011-08-22 15:09 sysu_mjc 阅读(220) 评论(0) 推荐(0)

快排之一

摘要: //递归实现快排#include <iostream>using namespace std;int arr[1000];int part(int beg, int end){ int low=beg,high=end; int pivot=arr[beg]; while(low<high) { while(low<high && arr[high]>=pivot) --high; arr[low]=arr[high]; while(low<high && arr[low]<=pivot) ... 阅读全文

posted @ 2011-08-22 15:08 sysu_mjc 阅读(169) 评论(0) 推荐(0)

简单选择排序

摘要: #include<iostream>using namespace std;void main(){ int n;cin>>n; int i,j,min,list[100],t; for(i=0;i<n;i++) cin>>list[i]; for(i=0;i<n-1;i++) { min=i; for(j=i+1;j<n;j++) if(list[j]<list[min])min=j; //找出最小值 t=list[i];list[i]=list[min];lis... 阅读全文

posted @ 2011-08-22 15:06 sysu_mjc 阅读(129) 评论(0) 推荐(0)

基数排序

摘要: #include <iostream>using namespace std;int maxbit(int data[],int n) //求数据的最大位数{ int i,p,max=0; int *temp;temp=new int[n]; for(i=0;i<n;i++)temp[i]=data[i]; for(i=0;i<n;i++) { p=1; while(temp[i]/10>0) { p++;temp[i]=temp[i]/10; } if(p>... 阅读全文

posted @ 2011-08-22 15:05 sysu_mjc 阅读(184) 评论(0) 推荐(0)

回溯

摘要: #include <iostream> //sicily 1152. 简单的马周游问题#include<stdio.h>using namespace std;int vis[10][10],path[100],rear;int arr[8][2]={{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};void backtrace(int s,int t){ int m,n; for(int i=0;i<8;++i) { m=s+arr[i][0];n=t+arr[i][1]; ... 阅读全文

posted @ 2011-08-22 15:04 sysu_mjc 阅读(180) 评论(0) 推荐(0)

哈希算法之三

摘要: //poj 1200 Crazy Search#include <iostream> //hash#include <string>using namespace std;bool table[20000000];int ans[100],count,curr; //注意有大小写字母char ch[1000000]; //本题中的输入字符串ch相当大,另外,table也要开得很大int main(){ int n,nc,len; memset(ans,-1,sizeof(ans)); scanf("%d%d%s",&n,&nc,ch) 阅读全文

posted @ 2011-08-22 15:03 sysu_mjc 阅读(107) 评论(0) 推荐(0)

哈希算法之四

摘要: //poj 3007 Organize Your Train part II//题意:给定一个字符串,从任意位置把它切为两半,得到两个子串//定义子串1为s1,子串2为s2,子串1的反串为s3,子串2的反串为s4//现在从s1 s2 s3 s4中任意取出两个串组合,问有多少种不同的组合方法//限制: (1) 串Si不能和其反串组合 (2) Si+Sj与Sj+Si是两种组合方式(但未必是不同的组合方式)#include <iostream> //字符串ELFHash 哈希#include <algorithm>#include<string>using nam 阅读全文

posted @ 2011-08-22 15:03 sysu_mjc 阅读(259) 评论(0) 推荐(0)

哈希算法之二

摘要: //poj 3349 Snowflake Snow Snowflakes#include<iostream> //哈希函数,拉链法#include<vector>using namespace std;const int mv=99991;int data[100002][6];vector<int> hash[100002];bool same(int a[6],int b[6]) //a[]的下标j总是从0到6,固定a,而b[]的起始下标i从0到6,绕顺时针(向右)和逆时针旋转{ int tag; for(int i=0;i<6;++i) { .. 阅读全文

posted @ 2011-08-22 15:02 sysu_mjc 阅读(200) 评论(0) 推荐(0)

归并排序

摘要: #include <iostream> //递归using namespace std;int n,list[200],temp[200];void merge_sort(int beg,int end){ if(end==beg) return ; int mid=(beg+end)/2; merge_sort(beg,mid); merge_sort(mid+1,end); int p=beg,q=mid+1,t=beg; while(p<=mid||q<=end) { if(q>end||(p<=... 阅读全文

posted @ 2011-08-22 15:01 sysu_mjc 阅读(184) 评论(0) 推荐(0)

哈希算法之一

摘要: //poj 2503 Babelfish #include <iostream> //ELFhash函数是对字符串的散列#include <string>using namespace std;#define M 1000000//M如果较小则不能很好地避免冲突,解决冲突需要更多的时间,会TLE,比如200000 当然不能过大,会MLE,比如10000000//M取适合的值,既可以避免hash开得太大,同时又可以减少冲突 string hash[M],res[M];int ELFHash(string str) //ELFhash函数{ int h = 0; ... 阅读全文

posted @ 2011-08-22 15:01 sysu_mjc 阅读(164) 评论(0) 推荐(0)

分组背包

摘要: //sicily 1346. 金明的预算方案 //在转移的时候主件有四种转移方法:不加附件,加两附件,加附件一或加附件二#include<iostream> //分组背包问题#include<cstring>using namespace std;struct node{ int v,w;}table[62][10];int n,m,v[62],p[62],q[62],len[62],dp[40000];int main(){ while(cin>>n>>m) { for(int i=1;i<=m;++i) { ... 阅读全文

posted @ 2011-08-22 12:26 sysu_mjc 阅读(177) 评论(0) 推荐(0)

二分查找(2)

摘要: //poj 3273 Monthly Expense/*题意:给你天数n,和每天需要花的钱,让你把这些天分成m份(每份都是连续的天),要求每份的和尽量少,输出这个和。一开始二分的上界为n天花费的总和(相当于分成1份),下界为每天花费的最大值(相当于分成n份),然后二分,每次的mid值为(上界 + 下界)/ 2,然后根据mid值遍历n天花费,对花费进行累加,每当超过mid值 份数++,看看这个mid值能把n天分成几份,如果份数大于m,表示mid偏小,下界 = mid + 1,反之小于等于mid,上界 = mid,然后输出最后的mid值即可,复杂度为 O(nlogM)*/#include < 阅读全文

posted @ 2011-08-22 12:26 sysu_mjc 阅读(182) 评论(0) 推荐(0)

二分查找(1)

摘要: //sicily 2015. A New Year Gift#include<iostream> //二分答案后贪心验证可行性using namespace std;int n,m,arr[1000];bool verify(int c) //对组数c进行二分{ int p=0; for(int i=0;i<n;++i) p+=min(arr[i],c); //如果arr[i]<c,自然是加上arr[i];若arr[i]>=c,因为一共就c组,所以只能取c if(p>=m*c) //c组,每组 m 条项链,返回1说... 阅读全文

posted @ 2011-08-22 12:25 sysu_mjc 阅读(189) 评论(0) 推荐(0)

多重背包之七

摘要: //poj 3260 The Fewest Coins/*题意:John带了n种币值Vi的确定数量Ci的硬币,而shopkeeper的硬币无限多.给出T,求John支付的硬币数目加上售货员找零的硬币数目的最小值。如果无法支付T,输出-1 支付时硬币数量有限制,为多重背包问题. 找零时硬币数量无限制,为完全背包问题*/#include<iostream> //多重背包和完全背包using namespace std; int main() { int n,t,euro[110],num[110],dp[30000],maxn; cin>>n>>t; i... 阅读全文

posted @ 2011-08-22 12:24 sysu_mjc 阅读(179) 评论(0) 推荐(0)

多重背包之六

摘要: #include<iostream> //poj 2392 Space Elevator #include<algorithm>using namespace std;int dp[50000];struct node{ int h,a,c; bool operator<(const node& o) { return a<o.a; }}ans[405];int k;int main(){ cin>>k; for(int i=0;i<k;++i) cin>>ans[i].h>>ans[i].a>> 阅读全文

posted @ 2011-08-22 12:23 sysu_mjc 阅读(126) 评论(0) 推荐(0)

多重背包之五

摘要: #include<iostream> //多重背包,poj 1742 Coins,TLE,可能是卡在常数上using namespace std;int n,m,a[250],c[250],dp[100005],weigh[50000];int main(){ while(scanf("%d%d",&n,&m)&&n) { for(int i=1;i<=n;++i) scanf("%d",&a[i]); int l=1; for(int i=1;i<=n;++i) { ... 阅读全文

posted @ 2011-08-22 12:23 sysu_mjc 阅读(152) 评论(0) 推荐(0)

多重背包之三

摘要: #include<iostream> //POJ 1276 Cash Machine,参照poj 1742 Coins 1040K 16MS using namespace std;int cash,n,ni[15],di[15],b[100005][2]; //b的定义参照poj 1742int main(){ while(cin>>cash>>n) { for(int i=1;i<=n;++i) cin>>ni[i]>>di[i]; if(cash==0||n==0) { ... 阅读全文

posted @ 2011-08-22 12:22 sysu_mjc 阅读(118) 评论(0) 推荐(0)

多重背包之四

摘要: #include<iostream> //poj 1742 Coinsusing namespace std;int v[105],c[105],b[100005][2]; int main(){ int n,m,i,j,count,num; while(scanf("%d%d",&n,&m),n&&m) { memset(b,0,sizeof(b)); for(i=1;i<=n;i++) scanf("%d",&v[i]); for(i=1;i<=n;i++) ... 阅读全文

posted @ 2011-08-22 12:22 sysu_mjc 阅读(97) 评论(0) 推荐(0)

多重背包之二

摘要: #include<iostream> //多重背包,POJ 1276 Cash Machine 652K 47MS using namespace std;int cash,n,ni[15],di[15],dp[100005];int main(){ while(cin>>cash>>n) { for(int i=1;i<=n;++i) cin>>ni[i]>>di[i]; if(cash==0||n==0) { cout<<"0\n";continue; ... 阅读全文

posted @ 2011-08-22 12:21 sysu_mjc 阅读(130) 评论(0) 推荐(0)

堆排序

摘要: #include<iostream> //小顶堆using namespace std;int list[100];void heappass(int list[],int i,int m) //i是根结点的编号,m是以i根结点的子树的最后一个结点编号,调整这棵子树为堆{ int j=2*i,x=list[i]; while(j<=m) //这里j==m的情况(只有一个左孩子)也应考虑进去 { if(j<m&&list[j]>list[j+1]) //若有两个孩子结点,则要list[j]是其中的较小... 阅读全文

posted @ 2011-08-22 12:20 sysu_mjc 阅读(102) 评论(0) 推荐(0)

多重背包之一

摘要: //sicily 1077. Cash Machine#include<iostream> //多重背包#include<cstring>using namespace std;int cash,n,num[20],d[20],dp[100010];int main(){ while(cin>>cash>>n) { for(int i=0;i<n;++i) cin>>num[i]>>d[i]; if(cash==0||n==0) { cout<<"0\n"; c... 阅读全文

posted @ 2011-08-22 12:20 sysu_mjc 阅读(152) 评论(0) 推荐(0)

KMP算法之四

摘要: #include <iostream> //poj 2752 Seek the Name, Seek the Fameusing namespace std;char A[500000];int m,P[500000],res[500000];void get_next(){ P[1]=0; int j=0; for(int i=2;i<=m;++i) { while(j>0&&A[j+1]!=A[i]) j=P[j]; if(A[j+1]==A[i]) j=j+1; ... 阅读全文

posted @ 2011-08-22 12:19 sysu_mjc 阅读(172) 评论(0) 推荐(0)

KMP算法之二

摘要: //sicily5 1282. Computer Game#include <iostream> //KMP算法#include<stdio.h>using namespace std;int A[1000000],B[80000]; //A是主串,B是子串,查询B串在A串的哪些地方出现int n,m,next[80000]; //n,m分别是A,B的串长度,A,B的下标是从1开始的void get_next(){ next[1]=0; int j=0; for(int i=2;i<=m;++i) { while(j>0&... 阅读全文

posted @ 2011-08-22 12:18 sysu_mjc 阅读(127) 评论(0) 推荐(0)

KMP算法之三

摘要: //poj 2406 Power Strings #include <iostream> //KMP算法using namespace std;char B[2000000]; int m,next[2000000]; void get_next(){ next[1]=0; int j=0; for(int i=2;i<=m;++i) { while(j>0&&B[j+1]!=B[i]) j=next[j]; if(B[j+1]==B[i]) j=j+1; ... 阅读全文

posted @ 2011-08-22 12:18 sysu_mjc 阅读(175) 评论(0) 推荐(0)

KMP算法之一

摘要: #include <iostream> //KMP算法是一种改进的字符串匹配算法,复杂度为O(n)using namespace std;char A[500000],B[500000]; //A是主串,B是子串,查询B串在A串的哪些地方出现int n,m,next[500000]; //n,m分别是A,B的串长度,注意A,B的下标是从1开始的void get_next(){ next[1]=0; int j=0; for(int i=2;i<=m;++i) { while(j>0&&B[j+1]!=B[i]) ... 阅读全文

posted @ 2011-08-22 12:17 sysu_mjc 阅读(161) 评论(0) 推荐(0)

DP之八

摘要: //sicily 1166. Computer Transformat#include<iostream> //DP+高精度using namespace std;#define M 50int Bit(int p){ if(p==0) return 1; int bit=0; while(p!=0) { p/=10; bit++; } return bit;}class Longint //高精度{public: Longint() { for(int ... 阅读全文

posted @ 2011-08-22 12:15 sysu_mjc 阅读(163) 评论(0) 推荐(0)

DP之九

摘要: //sicily 1685. Missile#include<iostream> //DP, O(n^2)的时间复杂度,n<=1000,数据规模较小using namespace std;int main(){ int n,h[1002],dp[1002][2]; //dp[i][0],表示以第i个数作为结束,此时第i个数在顺序上是偶("the even missile to destroy"),而dp[i][1]则表示第i个数在顺序上是奇("the odd missile to destroy") while(cin>>n 阅读全文

posted @ 2011-08-22 12:15 sysu_mjc 阅读(126) 评论(0) 推荐(0)

DP之六

摘要: //sicily 1197. Hotel#include<iostream> //DP,含通用符的字符串的最长匹配#include<string>#include<cstring>using namespace std;string str[10010]; int f[60][60]; //f[i][j]=1,表示一个字符串的0-i子串与另一个字符串的0-j子串匹配,f[i][j]=0则是不匹配int dp(int i,int j,int id) //返回f[i][j],表示字符串str[0][0-i]与字符串str[id][0-j]是否匹配{ ... 阅读全文

posted @ 2011-08-22 12:13 sysu_mjc 阅读(158) 评论(0) 推荐(0)

DP之七

摘要: //poj 1157 LITTLE SHOP OF FLOWERS#include <iostream> using namespace std;int ans[105][105],d[105][105];const int least=INT_MIN;int dp(int i,int j){ if(d[i][j]!=least) return d[i][j]; int m=least; for(int k=i;k<=j;++k) { if(dp(i-1,k-1)+ans[i][k]>m) ... 阅读全文

posted @ 2011-08-22 12:13 sysu_mjc 阅读(121) 评论(0) 推荐(0)

DP之五

摘要: //sicily 1274. Pascal's Travels/*题意:在一个N×N的底盘上,每一格有一个非负整数,表示在那一格可以向右或向下走几步。问每次只能向右或向下走,从左上角的格子走到右下方的格子有多少种不同的方案。dp[i][j]表示到从左上角到(i,j)的方案数dp[i][j]+=(dp[i][u])+(dp[v][j]),1<=u<j, 1<=v<i,如果(i,u)能走到(i,j), (v,j)能走到(i,j)*/#include<iostream> //DP#include<cstring>using names 阅读全文

posted @ 2011-08-22 12:12 sysu_mjc 阅读(124) 评论(0) 推荐(0)

DP之三

摘要: //sicily 1345. 能量项链#include<iostream> //DP#include<cstring>using namespace std;int n,num[210][2],dp[210][210];int main(){ while(cin>>n) { for(int i=1;i<=n;++i) cin>>num[i][0]; for(int i=1;i<=n;++i) //样例中,num[1-8]=(2,3),(3,5),(5,10),(10,2),(2,3),(3,5),(5,1... 阅读全文

posted @ 2011-08-22 12:11 sysu_mjc 阅读(242) 评论(0) 推荐(0)

DP之四

摘要: //sicily 1763. 传球游戏#include<iostream> //DP#include<cstring>using namespace std;int main(){ int n,m,dp[100][100]; while(cin>>n>>m) { memset(dp,0,sizeof(dp)); dp[1][0]=1; //初始化,一开始球在 1 号手里 for(int j=1;j<=m;++j) //dp[i][j]表示球经传递 j 次后落在第 i 个同学的方... 阅读全文

posted @ 2011-08-22 12:11 sysu_mjc 阅读(110) 评论(0) 推荐(0)

DP之一

摘要: //sicily 1564. HOUSING/*对一个数分解成几个因子之和,因子数目不限,但必须不小于5,求有多少种组合方式比如 m(17) = 7 (namely 17, 5 + 12, 6 + 11, 7 + 10, 8 + 9, 5 + 5 + 7, 5 + 6 + 6)为避免重复(5 + 5 + 6, 5 + 6 + 5 and 6 + 5 + 5 are counted only once.),规定因子的序列是上升的于是可以用 dp[i][j] 来表示对 i 分解且第一个因子为 j 的组合数。状态转移方程: dp[i][j]+=dp[i-j][k]; 这里 5<=j<=i 阅读全文

posted @ 2011-08-22 12:10 sysu_mjc 阅读(155) 评论(0) 推荐(0)

DP之二

摘要: //sicily 1037. Decorations//两个字符串ch[i],ch[j],长度都一样为len,如果ch[i][1..len-1]=ch[j][0..len-2],则表示连接,ch[i]->ch[j]//把每个输入的字符串当作顶点,字符串之间若是连接则建立一条边,由此得到初始图,//问题转化成求 顶点数 l-len+1 的路径总数,可以用DP求解#include<iostream> //DP#include<cstring>using namespace std;int n,l,m,len,cnt[602][602],dp[602][602];cha 阅读全文

posted @ 2011-08-22 12:10 sysu_mjc 阅读(139) 评论(0) 推荐(0)

DFS之四

摘要: //sicily 1158. Pick numbers#include<iostream>using namespace std;int m,n,table[20][20],res;void dfs(int s,int x,int y){ s+=table[x][y]; if(x==m&&y==n&&s>0) { if(res==-1) res=s; else res=min(res,s); } if(x<m) dfs(s,x+1,y); if(y<n) ... 阅读全文

posted @ 2011-08-22 12:09 sysu_mjc 阅读(117) 评论(0) 推荐(0)

DFS之三

摘要: //sicily 1039 Phone Home#include<iostream> //求图的色数,即使各相邻顶点的颜色不相同所需的最小色数.数据量小,直接枚举+DFS#include<stdio.h>#include<cmath>#include<cstring>using namespace std;struct node{ double x,y;}point[20];int n,cnt[20][20],color[20],num,suc;void dfs(int i){ for(int c=1;c<=num;++c) { int f 阅读全文

posted @ 2011-08-22 12:08 sysu_mjc 阅读(141) 评论(0) 推荐(0)

DFS之二

摘要: //sicily 1002. Anti-prime Sequences#include<iostream> //DFS,给出n,m,d,元素的取值从n到m,判断是否存在一个排列,使得在序列中任意(2,3,...d)个数的和都不是素数,如果有则输出最小的那个序列#include<stdio.h>#include<cstring>using namespace std;int n,m,d,arr[1010];bool prime(int s){ for(int i=2;i*i<=s;++i) if(s%i==0) return false; ... 阅读全文

posted @ 2011-08-22 12:08 sysu_mjc 阅读(115) 评论(0) 推荐(0)

BFS之五(双向bfs)

摘要: #include <iostream> //poj 2243 Knight Moves 参照poj 1915#include <deque>using namespace std;int dir[2][8]={{-2,-1,1,2,2,1,-1,-2},{-1,-2,-2,-1,1,2,2,1}};int visited[10][20],path[10][20];struct node{ int x,y,c; bool operator==(const node& other) { return x==other.x&&y==other.... 阅读全文

posted @ 2011-08-22 12:07 sysu_mjc 阅读(175) 评论(0) 推荐(0)

DFS之一

摘要: //poj 1426 Find The Multiple #include<iostream> //求1个10进制数,只能由1或0组成,能整除nusing namespace std;int n,flag;int num[200]={1};int mod(int len){ int s=0; for(int i=0;i<=len;++i) { s=s*10+num[i]; if(s>=n) s%=n; } return s;}void dfs(int len){ if(len>99) ... 阅读全文

posted @ 2011-08-22 12:07 sysu_mjc 阅读(142) 评论(0) 推荐(0)

BFS之四(双向bfs和康托压缩)

摘要: /*双向BFS就是从起点 和 终点 同时开始搜索。由起点搜索(BSF1)得到的点标记为1,由终点搜索(BFS2)得到的点标记为2。当某一时刻BFS1搜索到了已经标记为2的点(或BFS2搜到了1号点),说明发生相遇,那么答案就是由bfs1和bfs2分别求得的两段路径长度的和。简单的分析:设每次的BFS,总的搜索状态数是r^L(r是搜索的分支数,L是搜索层数)。而采取双向BFS算法,那么,从前往后、从后往前,分别需要搜索L/2层,合起来就是2*(r^(L/2))这要比一般的BFS快的多。可以看到,双向BFS不过就是在while循环中套了两个节点扩展模块,一个是qu1,一个是qu2二者轮流执行、轮流 阅读全文

posted @ 2011-08-22 12:05 sysu_mjc 阅读(474) 评论(0) 推荐(0)

BFS之三(单向bfs和康托压缩)

摘要: //poj 1077 Eight#include <iostream> //单向bfs和康托压缩#include<string>using namespace std;bool visited[1000000];int fac[]={1,1,2,6,24,120,720,5040,40320,362880}; //9!表int cantor(int arr[]) { int temp,num=1; //当排列为1 2 3 4 5 6 7 8 9时康托值等于1 for(int i=0;i<9;++i) { t... 阅读全文

posted @ 2011-08-22 12:01 sysu_mjc 阅读(192) 评论(0) 推荐(0)

BFS之一

摘要: //poj 1166 The Clocks#include<iostream> //BFS 51808K 516MS #include<string>using namespace std;string str[10]={" ","ABDE","ABC","BCEF","ADG","BDEFH","CFI","DEGH","GHI","EFHI"};// A 对应 stat 阅读全文

posted @ 2011-08-22 11:58 sysu_mjc 阅读(147) 评论(0) 推荐(0)

BFS之二

摘要: //poj 1252 Euro Efficiency//给出6枚不同面值(在1到100之间)的硬币,通过 加减 凑出1到100的钱数,我们关心的是最少要用到几枚硬币,//最后求出平均值,并找出其中最大值#include<iostream> //BFS最短路搜索#include<stdio.h>#include<algorithm>#include<queue>#include<cstring>using namespace std;int euro[20],f[200],vis[200];int main(){ int cases; 阅读全文

posted @ 2011-08-22 11:58 sysu_mjc 阅读(153) 评论(0) 推荐(0)

AC自动机

摘要: #include <iostream> //poj 1204 Word Puzzlesusing namespace std ;struct Node { Node *next[26]; Node *fail; int des; //标记要查找的单词最后一个字母出现的地方 Node():des(-1),fail(NULL) { memset(next,NULL,sizeof(next)); } int heigh;}*root,*q[100000];char table[1... 阅读全文

posted @ 2011-08-22 11:57 sysu_mjc 阅读(165) 评论(0) 推荐(0)

0-1背包之四

摘要: //sicily 1221. 数字游戏//m个回合里,每个回合在n个数中选择一个ai,然后剩下的数都减去bi,求选中的ai之和的最大值//0-1背包,每个数的体积都为1,背包容量为 m //显然,如果选中的m个数已固定的话,擦去这m个数的顺序对结果有影响,很明显为最大化,应该先擦掉bi小的数,//所以事先要排好序,按bi从大到小排,靠后dp选中的数bi较小#include<iostream> //0-1背包#include<algorithm>using namespace std;struct node{ int a,b;}ans[202];bool cmp(co.. 阅读全文

posted @ 2011-08-22 11:44 sysu_mjc 阅读(127) 评论(0) 推荐(0)

0-1背包之二

摘要: #include<iostream> //POJ 3211 Washing Clothes#include<map>#include<vector>#include<string>using namespace std;int main(){ int m,n,p,s[12],dp[200000],sum; //因为不必记录路径,所以dp数组用一维的就行 int i,j,k; string color; while(cin>>m>>n&&m&&n) { map<string ,int&g 阅读全文

posted @ 2011-08-22 11:43 sysu_mjc 阅读(138) 评论(0) 推荐(0)

0-1背包之三

摘要: #include <iostream> //sicily 1146 采药using namespace std;const int max_m=105,max_w=10005; int main() { int c,m,v[max_m],w[max_m],dp[max_w]; cin>>c>>m; for(int i=1;i<=m;++i) cin>>w[i]>>v[i]; for(int i=1;i<=m;++i) for(int j=c;j>=w[i];--j) dp[j]=max... 阅读全文

posted @ 2011-08-22 11:43 sysu_mjc 阅读(83) 评论(0) 推荐(0)

0-1背包之一

摘要: #include <iostream> #include<iomanip> using namespace std;const int max_n=100,max_w=10000; int m[max_n][max_w],v[max_n],w[max_n],x[max_n]; //m[i][j]是背包容量为j,可选择物品为i,i+1,…,n时的最优值,即装入背包物品的总价值最大 x[]记录选择哪个物品int c,n,i,j;void init(){ cout<<"请输入物品个数和背包容量:"; cin>>n>>c; 阅读全文

posted @ 2011-08-22 11:42 sysu_mjc 阅读(140) 评论(0) 推荐(0)

拓扑排序之六

摘要: //poj 1270 Following Orders//给出一个字母表和一些字母对(c1,c2)表示c1<c2//求出所有满足要求的排列,并按照字典序输出#include<iostream> //拓扑排序 + dfs#include<algorithm>#include<string>using namespace std;int data[30],side[30][30],vis[30],len,path[30],in[30];void topo(int r){ if(r==len) { for(int i=0;i<len;++i) ... 阅读全文

posted @ 2011-08-22 09:18 sysu_mjc 阅读(125) 评论(0) 推荐(0)

拓扑排序之五

摘要: //poj 3272 Cow Traffic//求出由入度为0的源点到汇点的所有路径中使用最频繁那条边总共使用的次数//f[i] 为入度为0的点到结点i的路径条数, g[i]为结点i到N的路径条数//枚举每个边(s为始点,t为终点),则所有边的f[s]*g[t]的最大值即为答案#include<iostream> //拓扑排序#include<deque>using namespace std;int n,m,table[5002][5002],side[50002][2];int f[5002],g[5002],in[5002],out[5002];deque< 阅读全文

posted @ 2011-08-22 09:17 sysu_mjc 阅读(115) 评论(0) 推荐(0)

拓扑排序之四

摘要: //poj 3687 Labeling Balls#include <iostream> //逆拓扑排序,从重到轻逐一确定using namespace std;int topo[201][201],_in[201],ans[201];int main(){ int t,n,m,heavy,light; cin>>t; while(t--) { cin>>n>>m; memset(topo,0,sizeof(topo)); memset(_in,0,sizeof(_in)); while(m-... 阅读全文

posted @ 2011-08-22 09:16 sysu_mjc 阅读(141) 评论(0) 推荐(0)

拓扑排序之三

摘要: //poj 1094 Sorting It All Out#include<iostream> //拓扑排序using namespace std;int table[26][26],path[26],in[26],ans[26]; //in表示入度数int main(){ char ch[4]; int n,m,a,b,flag; while(cin>>n>>m&&n) { memset(table,0,sizeof(table)); memset(ans,0,sizeof(ans)); f... 阅读全文

posted @ 2011-08-22 09:15 sysu_mjc 阅读(108) 评论(0) 推荐(0)

拓扑排序之二

摘要: //sicily 1424. 奖金#include<iostream> //拓扑排序#include<vector>using namespace std;int n,m,a,b;int in[10002],path[10002],add[10002];vector<int> table[10002]; //如果是邻接矩阵则会超内存int main(){ cin>>n>>m; while(m--) { cin>>a>>b; table[b].push_back(a); //b->a, b指向a ... 阅读全文

posted @ 2011-08-22 09:14 sysu_mjc 阅读(105) 评论(0) 推荐(0)

拓扑排序之一

摘要: //拓扑排序:若G包含有向边(U,V),则在序列中U出现在V之前,即该序列使得图中所有有向边均从左指向右。//如果图是有回路的,就不存在这样的序列。//首先选择一个无前驱的顶点(即入度为0的顶点,图中至少应该有一个这样的顶点,否则肯定存在回路),//然后从图中移去该顶点以及由其发出的所有有向边,如果图中还存在无前驱的顶点,则重复上述操作,直到操作无法进行。//如果图不为空,说明图中存在回路,无法进行拓扑排序;否则移出的顶点的顺序就是对该图的一个拓扑排序。//poj 2367 Genealogical tree#include<iostream> //简单拓扑排序using nam. 阅读全文

posted @ 2011-08-22 09:12 sysu_mjc 阅读(156) 评论(0) 推荐(0)

导航