
图论
一只会旅行的猫
这个作者很懒,什么都没留下…
展开
-
【最小生成树】hdu 1863 畅通工程
分析:prim算法(添加边)#include#includeusing namespace std;struct Road{ int x,y,vau;}road[5000];bool comp(const struct Road &A,const struct Road &B){ if(A.vau<B.vau) return 1; else return原创 2013-05-03 20:58:16 · 1512 阅读 · 0 评论 -
【最大流(dinic)】poj 1273 Drainage Ditches
http://poj.org/problem?id=1273分析:最大流的dinic实现模板(邻接矩阵)#include #include #include #include using namespace std;const int NM=205;const int MAX=0xfffffff;int a[NM][NM],level[NM],n,m;inlin原创 2014-02-18 12:35:59 · 503 阅读 · 0 评论 -
【最大流(EK)】poj 1149 PIGS
http://poj.org/problem?id=1149题意:有M个猪圈、N个顾客,每个顾客有A把钥匙;只能在有钥匙的猪圈购买最多B只猪,顾客购买过后,可以对打开的猪圈内的猪重新分配;求最多可以卖出多少头猪?分析:经过大神的总结,得以建立以下图:1)在原先的图中添加源点(0)和汇点(n+1);2)源点连接的是每个猪圈的第一个顾客,如果一个顾客是多个猪圈的第一名买家,则将权值合并原创 2014-02-17 00:44:28 · 577 阅读 · 0 评论 -
【最短路径-Floyd】hdu 2066 一个人的旅行
http://acm.hdu.edu.cn/showproblem.php?pid=2066#include#include#include#includeusing namespace std;#define MAX 0x3fffffffint a[1001][1001],mmax;bool c1[1001],c2[1001];void Floyd(){ int原创 2013-08-05 23:38:51 · 712 阅读 · 0 评论 -
【最短路径-Dis】hdu 2680 Choose the best route
http://acm.hdu.edu.cn/showproblem.php?pid=2680注意:单向边,逆向保存#include#include#includeusing namespace std;const int NM=1005;const int MAX=0xfffff;int a[NM][NM],p[NM],n,m;bool flag[NM];void Di原创 2013-08-05 23:45:56 · 613 阅读 · 0 评论 -
【最短路径-Dis】hdu 2145 zz's Mysterious Present
http://acm.hdu.edu.cn/showproblem.php?pid=2145题意:m个人,n个城市,k条路径;zz在p城市。相同的到达时间,看最短距离的长度(更长);如果时间+距离都相同,礼物就给编号最大的孩纸分析:细心处理ing,为什么总在我以为过不了的时候过了,也不知道这神一样的bug是怎么调试出来的~~#include#includeusing names原创 2013-08-10 21:25:22 · 702 阅读 · 0 评论 -
【最短路径-Floyd/Bellman_Ford】hdu 1217 Arbitrage
http://acm.hdu.edu.cn/showproblem.php?pid=1217分析:因为图中存在负权边,所以不能用dis#include#include#include#include#includeusing namespace std;const int NM=32;int main(){ int i,j,k,x,y,n,m,t; string原创 2013-08-08 00:27:42 · 580 阅读 · 0 评论 -
【BFS+Prim】poj 3026 Borg Maze
http://poj.org/problem?id=3026题意:从S开始找到一条到所有外星人A的通路,使得该通路总长度最短。分析:暴搜找出所有A/S之间的边,然后用prim把所有最短的边连接起来。#include #include #include #include using namespace std;const int NM=105;/*k:外星人的个数,原创 2014-03-26 20:53:18 · 479 阅读 · 0 评论 -
【Bellman_Ford】poj 1860 Currency Exchange
http://poj.org/problem?id=1860题意:A和B交换率相等(双向图),每次交换需要小费(可以交换多次),问经过一系列交换后(最后需要换回最开始的那种货币),资金能否增长?分析:寻找正权回路#include #include #include #include #include using namespace std;const int原创 2014-03-27 19:29:32 · 658 阅读 · 0 评论 -
【Dijkstra+DFS(记忆化)】hdu 1142 A Walk Through the Forest(外:hdu 1428)
http://acm.hdu.edu.cn/showproblem.php?pid=1142#include#include#includeusing namespace std;const int MAX=0x3fffffff;const int NM=1005;int n;int a[NM][NM];int p[NM],f[NM],flag[NM];vo原创 2013-08-10 18:38:07 · 541 阅读 · 0 评论 -
【最短路径-Dis】hdu 1311 Relative Relatives
http://acm.hdu.edu.cn/showproblem.php?pid=1311分析:原创 2014-04-12 14:19:18 · 715 阅读 · 0 评论 -
二分图各项
1.二分图:二分图又称作二部图,是图论中的一种特殊模型。 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图。百科:http://baike.baidu.com/link?url=iImhnK5sz4CE4b6C4ml0FrZjoLq076y原创 2013-12-29 20:06:25 · 521 阅读 · 0 评论 -
【二分图匹配(最小路径覆盖)】hdu 1151 Air Raid
http://acm.hdu.edu.cn/showproblem.php?pid=1151题意:有n个路口,m条路(单向、不成环),有一些伞兵降落到路口,求最少数量的伞兵可以访问所有路口分析:二分图DAG图(无回路有向图)的最小路径覆盖 = 节点数 - 最大匹配数#include#include#includeconst int NM=130;int a[130][原创 2013-12-04 15:35:59 · 642 阅读 · 0 评论 -
【并查集+字典树】poj2513 Colored Sticks
http://poj.org/problem?id=2513分析:形成欧拉通路,即:无向图每个点的度数为偶数或有2个奇数,Trie+并查集欧拉回路的定义;图G的一个回路,若它恰通过G中每条边一次,则称该回路为欧拉(Euler)回路,具有欧拉回路的图称为欧拉图(简称E图)。类似:NYOJ42 一笔画问题#include#include#includeusing name原创 2013-04-28 20:13:54 · 686 阅读 · 0 评论 -
【染色法+二分图匹配】hdu 2444 The Accomodation of Students
http://acm.hdu.edu.cn/showproblem.php?pid=2444题意:把学生分为两组,每组内的学生互相不认识,如果不能分成这样的两组,直接输出”No“;否则,从A组和B组中各选一个学生,放到一个寝室内(这两个学生相互认识),求最大能分几个寝室分析:即判断是否形成二分图(黑白染色法)+二分图匹配黑白染色法:#include #include #原创 2013-12-29 19:14:49 · 585 阅读 · 0 评论 -
【最大流(EK)】hdu 3549 Flow Problem
http://acm.hdu.edu.cn/showproblem.php?pid=3549分析:最大网络流的EK算法#include #include #include #include using namespace std;const int NM=20;const int MAX=0xfffffff;int a[NM][NM],pre[NM],n,m,ans原创 2014-02-15 18:45:19 · 578 阅读 · 0 评论 -
【二分图匹配(最小顶点覆盖)】hdu 1498 50 years,50 colors
http://acm.hdu.edu.cn/showproblem.php?pid=1498题意:有n*n个不同颜色的气球,每次只能扎破同一行或者同一列相同颜色的气球,问在k次操作后哪种颜色的气球还剩下分析:需要用最少的次数扎破同一颜色的气球,把行和列看成二分图的两组集合,进行最大匹配,转化成求最小顶点覆盖#include #include #include #inclu原创 2014-01-06 14:43:59 · 592 阅读 · 0 评论 -
【最短路径-Dis】hdu 3790 最短路径问题
http://acm.hdu.edu.cn/showproblem.php?pid=3790分析:首先判断路径长短,在路径长短相同时,才判断花费#include#include#includeusing namespace std;const int NM=1005;const int MAX=0x3fffffff;int a[NM][NM],b[NM][NM],n,m;b原创 2013-08-06 12:34:29 · 667 阅读 · 0 评论 -
【最短路径-Floyd】hdu 2112 HDU Today
http://acm.hdu.edu.cn/showproblem.php?pid=2112注意:起终点相同,map保存出现的地点#include#include#include#includeusing namespace std;const int NM=155;const int MAX=0x3fffffff;int a[NM][NM];mapm1;int ma原创 2013-08-06 14:41:10 · 565 阅读 · 0 评论 -
【最短路径-Dis】hdu 1596 find the safest road
http://acm.hdu.edu.cn/showproblem.php?pid=1596分析:乘积,注意初始化即可#include#includeusing namespace std;const int NM=1005;const int MAX=0;double a[NM][NM],p[NM];int n; bool flag[NM]; void Dis(i原创 2013-08-06 18:09:03 · 449 阅读 · 0 评论 -
【最短路径-Floyd+路径】hdu 1385
1.floyd-warshall算法如:hdu 1385for(i=1;i<=N;i++)//字典序神马的,注意-1的时候 for(j=1;j<=N;j++) pass[i][j]=j; //floyd-warshall算法 for(k=1;k<=N;k++)//注意:要先枚举媒介结点,即:k for(i=1;i<=N;i++) for(j=1;j<=N;j原创 2013-08-10 20:17:26 · 324 阅读 · 0 评论 -
nyoj 127&&nyoj 170
http://acm.nyist.net/JudgeOnline/problem.php?pid=127分析:cayley定理:即过n个有标志顶点的树的数目等于n^(n-2)。百科#include #include using namespace std;const int NM=10003;int main(){ int j,T,ans,num; sc原创 2013-09-27 21:02:38 · 617 阅读 · 0 评论 -
【匈牙利算法】hdu 1083 Courses
http://acm.hdu.edu.cn/showproblem.php?pid=1083分析:二分图匹配,一个学生代表一门课程#include#include#includeconst int NM=305;bool a[NM][NM],flag[NM];int link[NM],n,m;bool Find(int x){ int i; for(i=1;i<=n;原创 2013-08-08 11:41:37 · 658 阅读 · 0 评论 -
【二分图匹配】hdu 4160 Dolls
http://acm.hdu.edu.cn/showproblem.php?pid=4160题意:每一个doll有三个属性:wi,li,hi,如果di、dj满足的wi 分析:n-二分图的最大匹配#include #include using namespace std;const int NM=505;int a[NM][NM],link[NM],flag[NM],n;原创 2013-12-04 14:24:56 · 534 阅读 · 0 评论 -
【二分图匹配】hdu 1281 棋盘游戏
http://acm.hdu.edu.cn/showproblem.php?pid=1281分析:匹配棋盘的行和列(两个“车”不能放在同一行或同一列),算出最大匹配值,然后依次去掉每一个点判断是否可放棋子的数量减少,得出关键点#include #include using namespace std;const int NM=105;int link[NM],flag原创 2013-12-31 11:44:58 · 568 阅读 · 0 评论 -
【染色法】hdu 3478 Catch
http://acm.hdu.edu.cn/showproblem.php?pid=3478题意:#include #include #include #include using namespace std;const int NM=100005;vectorve[NM];int col[NM],n;int Color(int v) { int j,t,k;原创 2013-12-31 11:55:27 · 640 阅读 · 0 评论 -
匈牙利算法
百科:http://baike.baidu.com/view/501092.htm名词:匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名。匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法。简介:设G=(V,E)是一个无向图。如顶点集V可分割为两个互不相交的子集V转载 2013-08-08 11:50:10 · 733 阅读 · 0 评论 -
【二分图匹配(最大独立集)】hdu 1068 Girls and Boys
http://acm.hdu.edu.cn/showproblem.php?pid=1068题意:找没有关系的人的最大集合分析:求最大独立集 二分图最大独立集 = 节点数 - 二分图最大匹配/2(关系是双向的)#include#include#includeconst int NM=1005;int a[NM][NM],link[NM],flag[NM],n;原创 2013-12-04 14:12:01 · 679 阅读 · 0 评论 -
【二分图匹配(最小顶点覆盖)】hdu 1150 Machine Schedule(外:hdu 1054 Strategic Game)
题意:给定k个工作,有两台机器,A机器有n种工作状态,B机器有m种工作状态,对于每一个工作,既可以被A机器Ai模式完成又可以被B机器Bj模式完成,每一次转换模式需要重启机器,求完成所有工作至少需要转换多少次模式。分析:将A以及B的不同模式作为二分图的两个部分,工作作为边,则该题转化为求解最小顶点覆盖来求解。原理:最小顶点覆盖:包含二分图的X,Y部的部分顶点的一个集合,使得所有的边至少有一个原创 2013-12-04 14:03:04 · 707 阅读 · 0 评论 -
【二分匹配】hdu 4185 Oil Skimming
http://acm.hdu.edu.cn/showproblem.php?pid=4185题意:有一个能取1*2(水平和竖直方向)油井的机器,问能最多取多少个’##‘分析:给每一个‘#’标号,‘#’可以与周围四个‘#’匹配,建立二分图,然后直接求最大匹配#include #include #include using namespace std;const int原创 2014-01-03 12:14:19 · 538 阅读 · 0 评论 -
【染色法】hdu 4751 Divided Groups
http://acm.hdu.edu.cn/showproblem.php?pid=4751分析:完全图-补图#include #include #include using namespace std;const int NM=105;int a[NM][NM],col[NM],n;bool Color(){ int i,j,t; memset(col,-1,s原创 2014-01-03 12:27:45 · 469 阅读 · 0 评论 -
【BFS/Dijkstra】hdu 1548 A Strange Lift
#include#includeconst int NUM=0xfffff;int a[205][205],f[205],d[205];int main(){int n,i,j,x,y,m,tt,MIN;while(scanf("%d",&n),n!=0){for(i=1;i{a[i][i]=0;for(j=1;ja[i][j]=a[j][i]=原创 2013-06-22 14:26:00 · 637 阅读 · 0 评论