随笔分类 -  图论

POJ 2375 Cow Ski Area[连通分量]
摘要:题目链接:http://poj.org/problem?id=2375题目大意:一片滑雪场,奶牛只能向相邻的并且不高于他当前高度的地方走。想加上缆车是的奶牛能从低的地方走向高的地方,求最少加的缆车数,是的奶牛可以从任意一个角落到达任意另外的角落解题思路:奶牛可以向相邻的不高于他的地方走,相当于u,v之前连通。若想加上缆车是图成为连通图,数目就是max(root, leave);思路同POJ 1236代码如下:#include#include#includeusing namespace std;#define M 505#define N 2000005struct Edge{ int ... 阅读全文
posted @ 2013-07-30 22:49 crying_Dream 阅读(292) 评论(0) 推荐(0)
POJ 1236 Network of Schools[连通分量]
摘要:题目链接:http://poj.org/problem?id=1236题目大意:给出N台电脑,电脑间单向连通传送文件问题1.网络中最少放几个文件保证所有电脑都能接受到文件问题2.最少向网络中加几条线保证任意放1个文件所有电脑都能接受到解题思路:1.求出强连通分量2.缩点 ,进行重新构图3.如果入度为0,说明没有文件传输到该网络。解决问题1,统计入度为0的个数即可4.一个强连通图的入度和出度都不为0。入度为0相当于根root,出度为0相当于叶子leave.max(root, leave)即为答案。就是需要加的线数,保证整个网络强连通。若连通分量个数为0,那么答案为0代码如下:#include#i 阅读全文
posted @ 2013-07-30 22:48 crying_Dream 阅读(285) 评论(0) 推荐(0)
POj 2186 Popular Cows[连通分量]
摘要:题目大意:给出N头牛,有M种关系u, v。代表u牛崇拜v牛。要求找出有多少头牛被所有牛崇拜着题目链接:http://poj.org/problem?id=2186解题思路:1>求出强连通分量,标记每头牛属于哪个分量内2>进行缩点,计算连通分量的个数x,给出的M组关系重新构图。u,v若属不于一个连通分量内out[belong[u]]++;3>统计x个连通分量出度。如果超过1个的连通分量出度为0,证明两群牛互不崇拜; 如果等于1 答案就是该分量中牛的个数.代码如下:#include#include#includeusing namespace std;#define N 1000 阅读全文
posted @ 2013-07-30 22:46 crying_Dream 阅读(204) 评论(0) 推荐(0)
POJ 1201 Intervals[差分约束]
摘要:#include#include#includeusing namespace std;#define N 1000005#define INF 999999999int head[N], vis[N], queue[N], dis[N], outqueue[N];int n, index;struct Edge{ int v, value, next;}edge[N];void add_Edge(int u, int v, int val){ edge[index].next=head[u]; edge[index].v=v; edge[index].value=va... 阅读全文
posted @ 2013-07-30 22:44 crying_Dream 阅读(247) 评论(0) 推荐(0)
POJ 1364 King[差分约束]
摘要:题目链接:http://poj.org/problem?id=1364题目大意:是否存在一个序列满足给的m组条件si ni oi ki如果oi=gt 表示:S(i+ni)-Si>ki如果oi=lt 表示:S(i+ni)-Si S(i+ni)-Si Si-S(i+ni)#include#includeusing namespace std;#define N 100000#define M 100000#define INF 999999999int dis[N]; //记录距离int vis[N]; //标记是否在队列中int queue[N]; //模拟队列int outqueue[N 阅读全文
posted @ 2013-07-30 22:42 crying_Dream 阅读(267) 评论(0) 推荐(0)
差分约束
摘要:差分约束:解决满足条件的不等式为了保证图连通,引入附加点vs,使图中每个点vi到vs都可达,设弧的权w(vs, vi)=0.对于每个差分约束xj-xi权值为bk;初始化dis[vs]=0, dis[vi]=INF;然后求以vs为源点的最短路径。由于存在负权值,因此可以用Spfa算法求spfa算法:1.将源点加入队列, dis[s]=0, 其他点为无穷大2.将队头取出标记为front,遍历与他相邻的点x;3.如果dis[x]>dis[front]+edge[x].value即源点到x的距离大于源点到front的距离+front到x点的距离,更新dis[x].并且如果x不在队列中将它入队.重 阅读全文
posted @ 2013-07-30 22:40 crying_Dream 阅读(285) 评论(0) 推荐(0)
POJ 2449 Remmarguts' Date[k短路]
摘要:Description"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. "Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Pr 阅读全文
posted @ 2013-07-30 22:40 crying_Dream 阅读(318) 评论(0) 推荐(0)
K短路
摘要:K短路 用dijsktra+A*启发式搜索当点v第K次出堆的时候,这时候求得的路径是k短路。A*算法有一个启发式函数f(p)=g(p)+h(p), 即评估函数=当前值+当前位置到终点的最短距离g(p):当前从s到p点所走的路径长度,h(p)就是点p到目的点t的最短距离。f(p)就是当前路径从s走到p在从p到t的所走距离。步骤:1>求出h(p)。将有向边反向,求出目的点t到所有点的最短距离,用dijkstra算法2>将原点s加入优先队列中3>优先队列取出f(p)最小的一个点p如果p==t,并且出来的次数恰好是k次,那么算法结束否则,如果p出来的次数多余k次,就不用再进入队列中否 阅读全文
posted @ 2013-07-30 22:37 crying_Dream 阅读(1833) 评论(0) 推荐(0)
HDU 1010 Tempter of the Bone &&ZOJ 2110【DFS】
摘要:题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010题目大意:一只狗受到了骨头的诱惑,进了n*m的迷宫,迷宫的大门在第T秒开启。小狗要恰好在T秒到达。并且.只能走一次。解题思路:BFS求最短时间,这里并不适合。因此dfs.dfs有3个状态dfs(i, j, t),表示到达(i, j)花费t秒。这里要有剪枝:剪枝1.目标状态(x1, y1),现在状态dd=(x2, y2) abs(x1-x2)+abs(y1-y2))表示现在状态到达目标状态的距离tt=T-t表示还需要走的时间,if(tt-dd<0||(tt-dd)%2) return ; 阅读全文
posted @ 2013-03-07 18:34 crying_Dream 阅读(215) 评论(0) 推荐(0)
POJ 1562 Oil Deposits【DFS】&&ZOJ 1709
摘要:题目链接:http://poj.org/problem?id=1562题目大意:就是找有多少个油田,在垂直或者水平或者垂直方向上相邻都算作是属于同一个油田。解题思路:如果遇到@的话就进行八个方位搜索,然后搜索到@变成*避免重复搜索。 代码如下:View Code #include<stdio.h>#include<string.h>char map[102][102];int dir[8][2]={{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};int m, n;void dfs 阅读全文
posted @ 2013-03-07 18:31 crying_Dream 阅读(231) 评论(0) 推荐(0)
POJ 2362 && POJ 1011【深搜】
摘要:编辑器加载中... 阅读全文
posted @ 2013-01-27 11:26 crying_Dream 阅读(122) 评论(0) 推荐(0)
POJ 2914 Minimum Cut【最小割】
摘要:DescriptionGiven an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must be removed at least to disconnect the graph into two subgraphs?InputInput contains multiple test cases. Each test case starts wit 阅读全文
posted @ 2012-08-30 00:10 crying_Dream 阅读(182) 评论(0) 推荐(0)
HDU 4263 Red/Blue Spanning Tree【最小生成树原理】
摘要:Problem DescriptionGiven an undirected, unweighted, connected graph, where each edge is colored either blue or red, determine whether a spanning tree with exactly k blue edges exists.InputThere will be several test cases in the input. Each test case will begin with a line with three integers:n m kWh 阅读全文
posted @ 2012-08-25 23:19 crying_Dream 阅读(530) 评论(0) 推荐(0)
POJ 1273 Drainage Ditches【最大流EK算法模板题】
摘要:DescriptionEvery time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch i 阅读全文
posted @ 2012-08-23 21:08 crying_Dream 阅读(237) 评论(0) 推荐(0)
POJ 3041 Asteroids【二分图最大匹配】
摘要:DescriptionBessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. Fortunately, Bessie has a powerful weapo 阅读全文
posted @ 2012-07-31 23:23 crying_Dream 阅读(155) 评论(0) 推荐(0)
POJ 1469 COURSES【最大匹配】
摘要:DescriptionConsider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions: every student in the committee represents a differ 阅读全文
posted @ 2012-07-31 23:19 crying_Dream 阅读(168) 评论(0) 推荐(0)