摘要: 链表实现:struct node{ int v; //边的结束顶点int w; //边的长度 node* next; //指向以同一起点的下一条边的指针}*first[N]; //first[u]指向以u为起始点的第一条边void init(){ memset(first,NULL,sizeof(first));}void add(int u, int v, int w)//添加边{ node* p =new node; p->v = v; p->w = w; p->next = fisrt; first[u] = p;}//使用的时候,找u的邻接点for(no... 阅读全文
posted @ 2011-08-24 20:05 AC_Von 阅读(2782) 评论(0) 推荐(1) 编辑
摘要: 并查集(Union-Find Sets),字面意思理解——合并,查找。给出一个集合,将其合并后用一个点代替一个集合的元素。例如:1 22 34 3合并后就是 2 / \1 3 \ 4主要操作:1、查找。2、合并。查找方法见http://www.cnblogs.com/vongang/archive/2011/07/31/2122763.html合并实现方法如下:void Union(int root1, int root2){ int x = FindSet(root1), y = FindSet(root2); if( x == y ) return ; if( ... 阅读全文
posted @ 2011-08-24 17:42 AC_Von 阅读(277) 评论(0) 推荐(0) 编辑
摘要: 下图说的很清楚,每次找入度为0的点,将其从序列中删掉,同时与它相连的所有点入度减一;实现代码,以HUD_1285为例:#include <iostream>#include <cstdio>using namespace std;const int N = 501;int map[N][N];int into[N], ans[N];void toposort(int x){ int i, j, k; for(i = 1; i <= x; i++) for(j = 1; j <= x; j++) if(map[i][j]) ... 阅读全文
posted @ 2011-08-24 17:25 AC_Von 阅读(243) 评论(0) 推荐(0) 编辑
摘要: 开始没整明白,还想用dfs递归求解,后来看了看解题报告的思路(貌似这题可以用dp),bfs一遍,用一个best[i]存放到i的最优解,最后直接输出best[k]就行。第一次队列数组开小(还是不想用<queue>)了,WA了一次。。。#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 100007;int q[N*10];int best[N];int n, k;void bfs(){ int f = 0, r = 0, x, 阅读全文
posted @ 2011-08-24 11:35 AC_Von 阅读(174) 评论(0) 推荐(0) 编辑
摘要: 代码+注释:#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int N = 100;int s[N];bool used[N];int cmp(int a, int b){ return a > b;}bool dfs(int n, int u, int left, int len){//n是木棍的总数//u是未被拼到长度为len的原始木棍中木棍数目//left是当前正在拼的木棍的剩余长度//len是正在尝试的原始木棍的长度 if(lef 阅读全文
posted @ 2011-08-24 09:59 AC_Von 阅读(206) 评论(0) 推荐(0) 编辑