
acm之路--图论
文章平均质量分 80
synapse7
这个作者很懒,什么都没留下…
展开
-
HDU 1102/POJ 2421 Constructing Roads(MST&Prim优化)
Constructing Roadshttp://acm.hdu.edu.cn/showproblem.php?pid=1102http://poj.org/problem?id=2421DescriptionThere are N villages, which are numbered from 1 to N, and you should build some原创 2013-08-06 15:30:46 · 1099 阅读 · 0 评论 -
UVa 11686 Pick up sticks (BFS拓扑排序)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2733算是一种模板题吧。。两个发现:1. 重复初始化queue相当费时间!2. 能不用iterator就不用!完整代码:/*0.732s*/#includeusi原创 2014-02-12 17:56:54 · 1722 阅读 · 0 评论 -
UVa 10048 Audiophobia(最短路&Floyd)
10048 - AudiophobiaTime limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=116&page=show_problem&problem=989Consider yourself lucky! Consider you原创 2013-08-05 21:48:18 · 993 阅读 · 0 评论 -
HDU 2066 一个人的旅行(最短路&Dijkstra)
一个人的旅行http://acm.hdu.edu.cn/showproblem.php?pid=2066Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Problem Description虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里原创 2013-08-08 23:12:44 · 1096 阅读 · 0 评论 -
最短路模板(二)——用拓扑排序解决有向无环图中的最短路
测试数据:8 135 4 0.354 7 0.375 7 0.285 1 0.324 0 0.380 2 0.263 7 0.391 3 0.297 2 0.346 2 0.403 6 0.526 0 0.586 4 0.93测试结果:5 to 0 : 0.735 to 1 : 0.325 to 2 : 0.625 t原创 2014-02-16 13:50:11 · 2421 阅读 · 0 评论 -
POJ 1330 Nearest Common Ancestors (LCA&Tarjan算法)
http://poj.org/problem?id=1330算法见这篇文章完整代码:/*63ms,1752KB*/#include#include#includeusing namespace std;const int mx = 10005;vector son[mx];int fa[mx], query[mx];bool vis[mx], has原创 2014-02-08 12:52:33 · 919 阅读 · 0 评论 -
UVa 10000 Longest Paths (单源最长路 - floyd or 拓扑排序)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=941由于n很小,floyd算法写起来方便,先用这个A了一下:/*0.162s*/#includeusing namespace std;const int mx = 105;i原创 2014-02-16 14:22:23 · 2073 阅读 · 0 评论 -
最短路模板:使用priority_queue实现的Dijkstra算法
#include#include#include#include#include#includeusing namespace std;const int mx = 10005;typedef pair P; ///first是最短距离,second是顶点编号struct edge{ int to, cost;};vector G[mx];int disTo[mx]原创 2014-02-15 01:12:26 · 4876 阅读 · 0 评论 -
FZU 2112 Tickets (连通分量&欧拉通路)
http://acm.fzu.edu.cn/problem.php?pid=2112思路:要使“整个图”有欧拉通路,至多有2个度为奇数的点。所以每多出2个度为奇数的点,我们就用一条线连起来(也就是多买一张票)。(注意,因为图的所有点的度数之和为偶数,所以不可能存在奇数个奇数度数的点)完整代码:/*484ms,596KB*/#include#incl原创 2014-03-02 21:23:16 · 1164 阅读 · 0 评论 -
POJ 1127 Jack Straws (线段不规范相交&&图的连通性&&Floyd-Warshall算法)
http://poj.org/problem?id=1127/*16ms,376KB*/#include#includestruct P{ int x, y; P(int x = 0, int y = 0): x(x), y(y) {} P operator + (P p) { return P(x + p.x, y + p.y); } P operator原创 2014-02-15 01:50:22 · 1155 阅读 · 0 评论 -
二分图最大匹配——匈牙利算法
复杂度均为O(VE)DFS实现,适用于稠密图:#includeusing namespace std;const int mx = 105;vector G[mx];int match[mx]; ///match表示匹配的对象编号bool vis[mx];bool dfs(int i){ vis[i] = true; for (int j = 0; j < G[i].原创 2014-03-05 23:19:58 · 903 阅读 · 0 评论 -
Codeforces Beta Round #87 (Div. 2) / 116B Little Pigs and Wolves (简单匹配)
http://codeforces.com/problemset/problem/116/B开始还打算打匈牙利的。。结果看到了这句话:“there will be at most one wolf adjacent to each little pig”也就是说,对于每头狼,若它周围有猪,就++cnt因为不会出现两头狼吃同一只猪的情况。完整代码:/*30ms原创 2014-03-09 10:35:15 · 1407 阅读 · 0 评论 -
Codeforces Round #237 (Div. 2) / 404C Restore Graph (构造最短路径树)
http://codeforces.com/contest/404/problem/C思路:我们构造一颗最短路径树就行了。若能够构造,边数必然为n-1(样例1的边数可以是两条)。如何构造?从距离为1的点开始,逐渐往下加边,生成一颗k叉树。若在中间生成了大于k的叉,则输出-1。完整代码:/*265ms,4436KB*/#includeusing namesp原创 2014-03-20 09:28:28 · 1741 阅读 · 0 评论 -
LCA问题的Tarjan离线算法 + POJ 1470
树的最近公共祖先(Lowest Common Ancestor)问题是树结构上最经典的问题之一。给一棵树T,每个询问形如:“点u和点v 的公共祖先是哪个点?”,此问题的答案被记为LCA(u, v)。LCA问题的算法分为在线和离线(Tarjan算法)两种,前者要求在回答后一个问题之前必须给出前一个问题的输出,而离线问题允许在读入所有询问之后一次性给出所有问题的答案。LCA问题的应用很多,例如原创 2014-02-08 11:50:22 · 1713 阅读 · 0 评论 -
DFS相关的一些模板
/*请从main开始看*//*SampleInput:6 8 70 52 42 31 20 13 43 50 21 40 01 12 23 34 45 54 2 20 12 32 10 0*/#include#include#include#include#includeusing namespace std;const int原创 2013-08-21 02:05:23 · 1092 阅读 · 0 评论 -
UVa 10305 Ordering Tasks (拓扑排序模板)
10305 - Ordering TasksTime limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1246模板:/*0.022s*/#includeusi原创 2013-12-02 00:27:56 · 1438 阅读 · 0 评论 -
UVa 10034 Freckles (MST & 稠密图的O(V^2)的Prim算法)
10034 - FrecklesTime limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=116&page=show_problem&problem=975纯模板题。完整代码:/*0.019s*/#inclu原创 2014-01-11 15:52:48 · 1284 阅读 · 0 评论 -
POJ 1258 Agri-Net(MST)
Agri-Nethttp://poj.org/problem?id=1258Time Limit: 1000MSMemory Limit: 10000KDescriptionFarmer John has been elected mayor of his town! One of his campaign promises was to bring原创 2013-08-06 09:39:09 · 1143 阅读 · 0 评论 -
POJ 1679 The Unique MST(次小生成树&Kruskal)
The Unique MSThttp://poj.org/problem?id=1679DescriptionGiven a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connect原创 2013-08-06 23:46:44 · 1232 阅读 · 0 评论 -
POJ 3522 Slim Span(MST)
Slim Spanhttp://poj.org/problem?id=3522Time Limit: 5000MSMemory Limit: 65536KDescriptionGiven an undirected weighted graph G, you should find one of spanning trees specified as fol原创 2013-08-07 11:21:14 · 1003 阅读 · 0 评论 -
POJ 1125 Stockbroker Grapevine(最短路&Floyd)
Stockbroker Grapevinehttp://poj.org/problem?id=1125Time Limit: 1000MSMemory Limit: 10000KDescriptionStockbrokers are known to overreact to rumours. You have been contracted to原创 2013-08-10 11:04:38 · 880 阅读 · 0 评论 -
HDU 1538 A Puzzle for Pirates (博弈&海盗分赃问题)
A Puzzle for Pirateshttp://acm.hdu.edu.cn/showproblem.php?pid=1538Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Problem DescriptionA bunch原创 2013-10-04 13:09:06 · 1522 阅读 · 2 评论 -
UVa 10004 Bicoloring (DFS&二分图)
10004 - BicoloringTime limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=945In 1976 the ``Four Color Map Theorem" w原创 2013-09-27 09:46:56 · 802 阅读 · 0 评论 -
BFS相关的一些模板
/*请从main开始看*//*SampleInput:6 8 30 52 42 31 20 13 43 50 21 52 42 54 2 10 12 32 1*/#include#include#include#include#includeusing namespace std;const int maxn = 1000002;li原创 2013-08-21 22:11:31 · 897 阅读 · 0 评论 -
POJ 1780 Code (十进制格雷码&欧拉回路)
Codehttp://poj.org/problem?id=1780Time Limit: 1000MSMemory Limit: 65536KDescriptionKEY Inc., the leading company in security hardware, has developed a new kind of safe. To原创 2013-10-23 23:55:53 · 1172 阅读 · 0 评论 -
UVa 200 Rare Order (拓扑排序)
200 - Rare OrderTime limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=136题意:给一列单词,这些单词是按一种未知的字典顺序排列的,要求输出这些字母原创 2013-11-18 12:28:07 · 2036 阅读 · 0 评论 -
UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法)
572 - Oil DepositsTime limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=513http://poj.org/problem?id=1562http原创 2013-08-03 17:42:08 · 1103 阅读 · 0 评论 -
HDU 2544 最短路 (SSSP & O(V^2)的Dijkstra算法)
http://acm.hdu.edu.cn/showproblem.php?pid=2544模板题。完整代码:/*15ms,276KB*/#include#includeconst int mx = 101;int n, m, dis[mx][mx], disTo[mx];bool vis[mx];void dij(int st){ int i,原创 2014-01-14 08:58:44 · 1384 阅读 · 0 评论 -
MST模板:使用priority_queue实现的Prim算法
测试数据:8 164 5 .354 7 .375 7 .280 7 .161 5 .320 4 .382 3 .171 7 .190 2 .261 2 .361 3 .292 7 .346 2 .403 6 .526 0 .586 4 .93测试结果:1.81代码:#include#include#原创 2014-02-16 00:05:11 · 1720 阅读 · 6 评论