随笔分类 - 图论--最短路 最小生成树
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1282感觉这题就比较有意思了 ,虽说是看了别人的代码,我依旧卡了好几天,原因1.我理解错题意了 原因2.网上题解没有一个把这题说明白的,也没有解释题意的。。。每条路径上的警察的最大值不超过K 而不是总和不超过K容易想到用二维spfa来更新 不过k太大了可以想一下floyd的原本思想 是dp[i][j] = dp[i][j]+dp[j][k],经过j做中转之后的最短路 也就是每两个结点 都会有N次更新这里把更新顺序做了一下改变 以c从小到大排序
阅读全文
摘要:http://poj.org/problem?id=1860模板提 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 using namespace std;10 #define INF 0xfffffff11 double dis[110],v;12 int n,m;13 struct node14 {15 int r,c;16 double r1,c1,r2,c2;17 }p[110];18 int bell_ford(int s)19 {20...
阅读全文
摘要:1160算是模版了 没什么限制 结束输出就行了 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 #define N 1010 9 #define M 1501010 struct node11 {12 int x,y,len;13 }p[M];14 int fa[N];15 int find(int x)16 {17 if(fa[x]!=x)18 fa[x] = find(fa[x]);19 return fa[x];20 }2...
阅读全文
摘要:1450水题 最长路 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 int n,m;10 vectored[510];11 int vis[510],dis[510],w[510][510];12 void spfa(int s,int e)13 {14 int i;15 queueq;16 q.push(s);17 vis[s] = 1;18 while(!q.empty())19 {2...
阅读全文
摘要:1934水题 RE了N久 后来发现是无向图 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 #define N 2000010 10 #define INF 0xfffffff 11 struct node 12 { 13 int u,v,next; 14 double w; 15 }ed[Nq; 42 dis[s] = 1; 43 p[s] = 1; 44 q.push(s);...
阅读全文
摘要:1205简单题 有一些小细节 两个站可能不相连 但是可以走过去 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 using namespace std; 10 #define INF 0xfffffff 11 vectored[310]; 12 double v1,v2,x[310],y[310],w[210][210],dis[210]; 13 int n,vis[210],pa[210],o[210]; 14 void spfa(int s...
阅读全文
摘要:1930简单二维 标记一下是上坡还是下坡 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 #define N 1001010 #define INF 0xfffffff11 int dis[N][2],vis[N];12 int n;13 struct node14 {15 int u,v,next,d;16 }ed[N*20];17 int t,head[N];18 void init()19 {20 t = 0;21 ...
阅读全文
摘要:1210简单模版题 敲个spfa还得瞟下模版。。 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 vectored[910];10 #defi...
阅读全文
摘要:1837被数据结构部分打击的不行了 换地 刷点简单的 图论第一题 floyd水过 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 int w[310][310];10 #define INF 0xfffffff11 mapf;12 struct node13 {14 char s[22];15 int id;16 }p[310];17 bool cmp(node a,node b)18 {19 return str...
阅读全文
摘要:最近脑子有点乱 老是不想清楚就啪啪的敲 敲完之后一看 咦。。样例都过不去 仔细一想 这样不对啊刚开始就写了一SPFA 最后发现边跟点的关系没处理好 删了。。写dfs。。还是没转化好 开始搜解题方法 看到别人都说最小环 解最小环的方法跟我想的差不多 就是边转化点没处理好又想了想 开个二维数组标记下 转化点 然后就枚举每条边的两个点间的最短距离 要先删了这条边 找个最小的就好了dijk求最短路 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: fence6 5 */ 6 #include 7 #include 8 #include 9 #i...
阅读全文
摘要:最短路复杂度估计错误 以为SPFA是N*m的 用了dij超时 用SPFA直接跑就好了O(k*e) K 一般为2,3; 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: butter 5 */ 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 using namespace std; 13 #define M 1500 14 #define P 850 15 #define N 550 16 #define INF 0xfffff...
阅读全文
摘要:floyd之后 枚举找最小就行 会有重边View Code 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: comehome 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<algorithm>10 #include<stdlib.h>11 #define INF 0x3f3f3f12 using namespace std;13 int w[110][110];14 int main()15 {1
阅读全文
摘要:dfs用来求这个节点属于哪一块 用floyd求出各点间的最短距离 再求出每个节点可达到的最远距离MAX[i]以及这两个块中的最长距离a,b. 枚举当两个节点不在一块中dis = max{(dis[i][j]+MAX[i]+MAX[j]),a,b),在求出dis中最小的。View Code 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: cowtour 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<algorithm
阅读全文
摘要:http://poj.org/problem?id=3026BFS求出任意两个A或A和S的最短距离 然后最小生成树求和把所有A和S存起来求BFS任意两点 搜超时了 之后改了改过了View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 #define INF 0x3f3f3f 8 struct node 9 { 10 int
阅读全文
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2493题意:对于一个给定的无向图,求其中有一条边的边权可以减半的情况下 从 A 点到 B 点的最短路。 做法:可以用spfa求出 A 到所有点的最短路 dis 和 B 到所有点的最短路 dis1 。然后枚举所有边(u,v),找出最小的dis[ u ] + w(u,v) / 2 + dis1[ v ] 即可,如果A、B不可达,输出“No solution”。 View Code 1 #include <iostream> 2 #inc
阅读全文
摘要:http://poj.org/problem?id=2240简单题 1A 只是把结点变成了字符串 用map调下就好 数据量不大 floyd300+ms货币之间的转换 问转换完是否能获利View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #include<map> 5 using namespace std; 6 double w[100][100]; 7 int main() 8 { 9 int i,j,k,n,m,kk =0 ;10 char str
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=3367题意是让找出最大的森林 也就是符合它所要求的最大的边和 它要求每个连通块最多有一个圈就是一个环 可以用并查集判祖先1 如果两个节点是一个祖先 就是一个连通块如果有圈 肯定不能合并 不然就两个圈了 就是这条边不要2 不是一个祖先 如果是全都没圈直接合并 有一个有的合并加标记为此块有圈View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #include<algorithm
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4396二维最短路 以这个点和到这个点的路径数作为一个点来标记View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #include<queue> 5 #define INF 0xfffffff 6 using namespace std; 7 struct node 8 { 9 int v,w,next;10 }men[200011];11 struct mode12
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4360从昨天下午纠结到现在 交了20多次 。。官方解题报告1001简单的图论题每条边除了有边权以外,还有一个字母标记。标记可以是“LOVE”里面任意字符。每个点,要拆成四个点,分别代表到达该点的标记为L,O,V,E的最短路。第一步就是求最短路,直接spfa就可以了。trick在于,至少要找到一个LOVE串,在只有一个节点的时候,有几条自环,至少必须走LOVE四条自环。此时,必须另外加一个节点表示开始节点。还有一个trick就是距离可能超过int。1 2 1314520 L1 2 1314520 O1 2 13
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=3339最短路+01背包以耗油量为V 以pow为价值View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 using namespace std; 5 #define INF 0x3f3f3f 6 int w[101][101],p[101],pi[101],f[10001]; 7 int main() 8 { 9 int i,j,k,n,m,t,a,b,c,v;10 scanf(&
阅读全文