摘要: 求最小点覆盖数,即最大匹配数,匈牙利算法。 1 #include 2 #include 3 int map[505][505],vis[505],linker[505];//linker[]记录V2中的点i在V1中所匹配的点x的编号 4 int n,k; 5 int dfs(int x) 6 { 7 for (int i = 1; i <= n; i++) 8 { 9 if (map[x][i] && !vis[i])//x到i有边且i点未被标记10 {11 vis[i] = 1;12 if (!... 阅读全文
posted @ 2013-08-18 20:13 N_ll 阅读(183) 评论(0) 推荐(0)
摘要: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1867 1 #include 2 #include 3 #include 4 const int maxn = 102; 5 const int INF=1 dis[i][k] + dis[k][j])//更新距离34 dis[i][j] = dis[i][k] + dis[k][j];35 }36 }37 }38 }39 int main()40 {41 ... 阅读全文
posted @ 2013-08-18 01:49 N_ll 阅读(187) 评论(0) 推荐(0)
摘要: 计算最大流,EK算法模板题。 1 #include 2 #include 3 #include 4 using namespace std; 5 const int maxn=520; 6 const int maxm=100010; 7 const int INF=1q;34 max_flow = 0;35 for (;;)36 {37 memset(a,0,sizeof(a));38 q.push(s);39 a[s] = INF;//源点容量为无穷40 while(!q.empty())41 ... 阅读全文
posted @ 2013-08-17 23:49 N_ll 阅读(161) 评论(0) 推荐(0)
摘要: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2138注意该图为有向图,1000个点应该最多有1000*999条边。在这跪了一下。。。 1 #include 2 #include 3 #include 4 5 using namespace std; 6 7 const int maxn=1000002; 8 int head[maxn],vis[maxn]; 9 int n,m,cnt;10 struct node11 {12 int u;13 int v;14 int... 阅读全文
posted @ 2013-08-16 15:02 N_ll 阅读(275) 评论(0) 推荐(0)
摘要: (一)递归法 根据n和m的关系,考虑以下几种情况:(1)当n=1时,不论m的值为多少(m>0),只有一种划分即{1}; (2) 当m=1时,不论n的值为多少,只有一种划分即n个1,{1,1,1,...,1}; (3) 当n=m时,根据划分中是否包含n,可以分为两种情况: (a). 划分中包含n的情况,只有一个即{n}; (b). 划分中不包含n的情况,这时划分中最大的数字也一定比n小,即n的所有(n-1)划分。 因此 f(n,n) =1 + f(n,n-1); (4) 当nm时,根据划分中是否包含最大值m,可以分为两种情况: (a). 划分中包含m的情况,即{m, {x1... 阅读全文
posted @ 2013-08-16 10:35 N_ll 阅读(109) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=10941.判断所给关系是否为合法的拓扑序列,若存在环,输出"Inconsistency found after %d relations."但仍继续输入。2.判断是否可以确定序列,若不可以确定,继续输入,若可以确定输出"Sorted sequence determined after %d relations: %s."后,继续输入数据。3.若输入完数据仍不可以确定序列,输出"Sorted sequence cannot be determined.". 1 #include 2 阅读全文
posted @ 2013-08-15 21:26 N_ll 阅读(206) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=1459题意:有一个电路网络,每个节点可以产生、传递、消耗若干电量,有点线连接结点,每个电线有最大传输量,求这个网络的最大消费量。思路:从源点到发电站连边,流量为发电量,从用户到汇点连边,流量为消费量,再根据电线连双向边,求最大流即可。 1 #include 2 #include 3 #include 4 const int N=220; 5 const int INF=1q;14 memset(pre,-1,sizeof(pre));15 pre[s] = 0;16 int k;17 q.push(s);18... 阅读全文
posted @ 2013-08-14 17:14 N_ll 阅读(236) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=3687看题意看了半天没看懂怎么回事,看完Discuss彻底凌乱了。。后来看了题解才懂,就是逆向建图+拓扑排序,建图时要判重边。 1 #include 2 #include 3 int map[202][202]; 4 int n,in[202],ans[202]; 5 int topo() 6 { 7 int i,j; 8 for (i = n; i >= 1; i --) 9 {10 for (j = n; j >= 1; j --)11 {12 if (!i... 阅读全文
posted @ 2013-08-14 15:53 N_ll 阅读(182) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=3083S为起点,E为中点,输出S—>E沿左边走的步数,沿右边走的步数,和最短步数。因为S—>E沿左边走的步数等于E—>S沿右边走的步数,故只需DFS搜索沿右边走的步数,改变起止点即可。然后BFS搜索最少步数。 1 #include 2 #include 3 #include 4 #include 5 6 using namespace std; 7 const int N=55; 8 char map[N][N]; 9 int dir[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};//顺时针方向1 阅读全文
posted @ 2013-08-14 11:26 N_ll 阅读(181) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=3278 1 #include 2 #include 3 #include 4 #define MAX 1000002 5 using namespace std; 6 int vis[MAX],step[MAX]; 7 int dir[3][2] = {{1,1},{1,-1},{2,0}}; 8 queue q; 9 int n,m;10 void bfs(int s)11 {12 vis[s] = 1;13 q.push(s);14 while(!q.empty())15 {16 s =... 阅读全文
posted @ 2013-08-14 11:16 N_ll 阅读(223) 评论(0) 推荐(0)