![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
图论
文章平均质量分 73
South__wind
ACMER
展开
-
poj 1206 Network of Schools
一问,求出缩点后入度为零的点数,二问,缩点后分别求出入度为零和出度为零,取比较大的,当然,如果是强连通图,输出0就好了. #include #include #include using namespace std; int edge[110][110]; int low[110],dfn[110],vis[110],num[110],index[110],in[110],out[原创 2012-08-29 14:27:30 · 432 阅读 · 0 评论 -
poj 1364 King
查分约束,题目的条件a[i]+a[i+1]+...a[i+n]<k,那么可以假设a[1]+a[2]+....a[n]=sum[n];那么a[i]+a[i+1]+...a[i+n]=sum[i+n]-sum[i-1]<=k-1(因为都是整数),然后就构成了一个查分约束系统,在用Bellman_ford 去求单源最短路径,只要不存在负权环那么必定有解。。。yxl,fighting。。。 #i原创 2012-08-30 16:28:53 · 268 阅读 · 0 评论 -
poj 1251 Jungle Roads
n个城市之间铺设光缆,铺设光缆费用很高且各个城市之间铺设光缆的费用不同。如果设计目标是使这n个城市之间的任意两个城市都可以直接或间接通信,并且要使铺设光缆的费用最低,这样的问题就是一个求最小生成树的问题。解决这个问题的方法就是在有n个城市结点、n(n-1)/2条费用不同的边构成的无向连通图中找出最小生成树。 最小生成树的应用相当广泛,是图论中比较基础的算法。解决最小生成树有两种算法原创 2012-08-31 16:16:59 · 868 阅读 · 1 评论 -
hdu 1878 欧拉回路
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1878 如题判断无向图的回路。。 一般有并查集判断。。和深搜和广搜。。。 我用的并查集 #include #include #include using namespace std; int du[1001],father[1001],rank[1001]; int n,m,a原创 2012-08-31 16:34:35 · 2640 阅读 · 0 评论 -
hdu 1151 Air Raid
#include #include int map[121][121]; int vis[121],link[121]; int t,in,s,x,y,ans; int dfs(int i) { for(int j=1;j<=in;j++) { if(map[i][j]==1) { if(vis[j]==0)原创 2012-09-01 20:02:32 · 387 阅读 · 0 评论 -
hdu 3991 Harry Potter and the Present II
最小点覆盖: 先用弗洛伊德算法把每两个城市的最短距离算出来,然后把分配的任务按时间排序,如果完成一个任务后,还可以在规定的时间内完成另一个任务,那么就在这两个任务编号之间连一条边,然后运行匈牙利算法,用任务总数减去得出来的数,当然要在减去1,他自己除外。 #include #include #include using namespace std; struct node{原创 2012-09-01 21:20:02 · 408 阅读 · 0 评论 -
hdu 1269 迷宫城堡
本以强连通的概念来命题,即是否图的任意两点是否可达,也就是判断一副图是否为强连通图,那么只要用tarjan缩点,把相互可达的点放在在一个缩点里,然后判断缩点后是否只有一个点。 #include #include #include using namespace std; struct node{ int v; int next; }edge[100010]; int原创 2012-09-01 20:27:08 · 350 阅读 · 0 评论 -
poj 1301 Jungle Roads
最小生成树的模板题,用prime即可。 #include #include const int inf=100000000; int map[1000][1000]; int mark[10000],road[10000],dis[10000]; int n,v,ans,num,len; char start,end; int prim() { for(in原创 2012-09-01 20:35:19 · 498 阅读 · 0 评论 -
HDU 3018 Ant Trip(一笔画问题)
#include #include #include using namespace std; struct node{ int rank; int father; int count; int degree; }c[100000]; int a[200000],b[200000]; int n,m; int find(int p) { while(p!=c[p原创 2012-08-31 16:48:23 · 730 阅读 · 0 评论 -
hdu 3986 Harry Potter and the Final Battle
很多人认为这是在求次短路,其实不然,这个题只要spfa()一次,先把最短路径求出来,在枚举拆掉最短路的每一条边,每次拆一条边就进行一次spfa(),之后还原先前拆的边,该拆另一条边,记录最小的时间即可。 #include #include #define INF 1000000 #define MAXN 2005 using namespace std; struct原创 2012-09-02 01:31:25 · 553 阅读 · 0 评论 -
hdu 4160 Dolls
最小点集覆盖: 如果一个套娃能够放进另一个套娃里面则在它们之间建立一条边,运行一遍匈牙利算法,用总的套娃数减去用匈牙利得到的匹配数即的答案。 #include #include #include using namespace std; int n; int map[510][510]; struct node{ int w,l,h; }a[510]; i原创 2012-09-02 01:47:25 · 792 阅读 · 1 评论 -
poj 2230 Watchcow
给你一幅连通的图,要求从起点1开始走,要经过每条边刚好两次,并且最终回到1起点,很明显,欧拉回路,dfs输出路径即可。 #include #include #include using namespace std; struct node{ int v; int next; int vis; }g[100000]; int adj[100000]; int n,m,e,a,b原创 2012-08-27 21:30:03 · 249 阅读 · 0 评论 -
poj 1041 John's trip
典型的欧拉回路,先判断图的连通性,在统计每个点的度,无向图欧拉回路中不能存在度为奇数的节点,在递归输出字典序最小的一条路径。 #include #include #include #include using namespace std; int edge[2000][2]; int degree[50],vis[2000],s[2000]; int edgenum,top,max原创 2012-08-28 01:14:08 · 669 阅读 · 0 评论 -
poj 2553 The Bottom of a Graph
题目大意 若节点V所能到达的点{w},都能反过来到达v,那我们称v是sink。 强连通+缩点 就是求极大连通分量,最后统计出度为0的点,排序后输出初度为0的分量包含的每一个点。 不管怎么样都会存在一个出度为0的点,所以说If the bottom is empty, print empty line是没有用的。 看了我半天的题目,出题的真是个2货。。。 #include #i原创 2012-08-28 01:22:16 · 373 阅读 · 0 评论 -
poj 3041 Asteroids
#include using namespace std; int link[510],n,m; bool map[510][510],hav[510]; bool find(int x){ int y,k; for(y=1;y<=n;y++) if(hav[y] && map[x][y]){ hav[y]=false; k=link[y];link原创 2012-08-30 16:25:03 · 263 阅读 · 0 评论 -
poj 3686 The Windy's
一开始把图建错了。结果纠结到现在。 本题需要拆点,把每台机器当成n台使用。 由于每台机器之前加工的玩具无法确定,但是我们可以反过来想。假设当前这个玩具倒数第K加工,那么他和后面加工的玩具总共延误K*MAP[I][J],我们就可以根据这个来建图。然后套KM算法去做。 #include #include #include using namespace std; #defin原创 2012-08-30 16:17:15 · 885 阅读 · 0 评论 -
poj 2195 Going Home
还是KM算法,求最小值的匹配,建图的时候,边都取反就可以转化成求最大值的匹配了。 #include #include # define inf 1000000 using namespace std; int m,n,x,y,ans; int map[101][101],visx[101],visy[101],ly[101],lx[101],link[101]; char s原创 2012-08-30 16:13:24 · 350 阅读 · 0 评论 -
poj 2186 Popular Cows
有向图的强连通+缩点。 题目要求要有多少头牛被所有牛喜欢,那么也就是求缩点以后出度为零的点对应的牛的个数或者所有点的出度都不为零,那么也就是说这个图是强连通图,所有点都可以互相到达,即所有牛都被其他牛喜欢,这时输出n就可以了,并且只有当出度为零的点不存在或只有一个才存在一些牛被其他所有牛喜欢。 #include #include #include using namespace std;原创 2012-08-29 14:52:13 · 250 阅读 · 0 评论 -
poj 3259 Wormholes
bellmen_ford的应用: Source Code Problem: 3259 User: yanxiaoli Memory: 424K Time: 875MS Language: G++ Result: Accepted Source Code #include int dis[500]; int road[5300][3]; in原创 2012-08-29 16:33:40 · 248 阅读 · 1 评论 -
poj 1789 Truck History
Source Code Problem: 1789 User: yanxiaoli Memory: 15840K Time: 563MS Language: C++ Result: Accepted Source Code 最小生成树,直接套(prim)模板就可以了 #include #include int map[2001][2001]; char str原创 2012-08-29 17:08:33 · 224 阅读 · 0 评论 -
poj 2349 Arctic Network
Source Code Problem: 2349 User: yanxiaoli Memory: 2136K Time: 63MS Language: C++ Result: Accepted Source Code #include #include #include #include using namespace std; struct point原创 2012-08-29 16:57:12 · 205 阅读 · 0 评论 -
poj 1125 Stockbroker Grapevine
floyd算法,其实也就是那么三层循环: #include int time[110][110]; int n,num,a,t,tot,index; int main() { while(scanf("%d",&n)&&n) { int i,j,k; for(i=0;i<=n;i++) for(j=0原创 2012-08-29 16:49:52 · 222 阅读 · 0 评论 -
poj 1258 Agri-Net
#include using namespace std; long farm[110][110]; long closeEdge[110]; int n; void createFarm() { for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%d",&f原创 2012-08-30 14:14:31 · 191 阅读 · 0 评论 -
poj 1511 Invitation Cards
单元最短路问题,正向建一次图,反向建一次图,把两次spfa()的结果相加即可。 #include #include struct Node{ long long start; long long end; long long len; long long next; }node[1000010],node1[1000010]; long long dist[1000010原创 2012-08-30 14:20:32 · 207 阅读 · 0 评论 -
poj 1201 Intervals
差分约束系统,对每个约束,则有dis[t]-dis[s]>=c。另外还有一个隐含的约束条件就是0<=dis[i]-dis[i-1]<=1,根据这两个条件来建图,用bellmen_ford和spfa都可以,前者要适当加以剪枝。 #include #include using namespace std; #define MAXINT 1000000 struct EDGE{ in原创 2012-08-30 14:32:55 · 217 阅读 · 0 评论 -
poj 3169 Layout
差分约束的应用 #include #include using namespace std; int map[1000000][3]; int dist[1000000]; int n,ml,md,a,b,d; int bell_ford() { for(int i=2;i<=n;i++) dist[i]=9999999;原创 2012-08-30 14:43:52 · 296 阅读 · 0 评论 -
poj 2400 Supervisor, Supervisee
有n个工人(老板),他们给自己想要的老板(工人)排名。求出所有最佳组合 KM+DFS().求出来最小值后把所有可能搭配DFS一遍就可以得出满足要求的搭配了。 #include #include #define inf 1000000 #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b))原创 2012-08-30 16:08:22 · 655 阅读 · 0 评论 -
poj 2983 Is the Information Reliable?
注意把a+c==b 转化成a+c=b就可以了,其余的和PKU king的思想一样。 #include int n,m,j,a,b,c; int map[200002][3]; int dist[1010]; char s[2]; bool bellman_ford() { for(int i=0;i<=m;i++) dist[i]=99999999;原创 2012-08-30 16:26:53 · 299 阅读 · 0 评论 -
hdu 1054 Strategic Game
匈牙利算法: #include using namespace std; int n,m,p,f,sum; int link[1600]; int vis[1600]; int map[1600][1600]; int dfs(int g) { for(int i=1;i<=map[g][0];i++) { int j=map[g]原创 2012-08-31 01:22:14 · 331 阅读 · 0 评论 -
poj 1386 Play on Words
题意:如果一个单词的最后一个字母和另一个单词的首字母相同,则它们可以连起来,判断所给的单词能否连成一线。 判断图的连通性,可以用dfs或并查集,主要考察欧拉回路的应用。 #include #include #include const int N=26; using namespace std; struct Node{ int in; int out; }degree[N];原创 2012-08-28 00:52:50 · 224 阅读 · 0 评论