随笔分类 -  图论 || 最小生成树

摘要:一开始做图形遍历的题都是用链表做的,这次用数组体会到了方便但就是有点浪费。不过题目给的内存限制已经足够了。View Code 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<queue> 5 #include<iostream> 6 7 using namespace std; 8 9 typedef struct10 {11 int v;12 int step; 13 }Point;14 Point P[21];15 int visit[21 阅读全文
posted @ 2012-05-24 22:32 zhongya 阅读(172) 评论(0) 推荐(0)
摘要:View Code 1 #include<stdio.h>//求出最小生成树之后,再求出第n-m大的值就行了 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #define N 501 6 #define Maxint 9999999 7 8 double c[N][N],x[N],y[N],dist[N]; 9 int s[N], closest[N],n;10 11 int cmp(const void *a,const void *b) 12 { 13 retur 阅读全文
posted @ 2012-04-29 16:15 zhongya 阅读(207) 评论(0) 推荐(0)
摘要:PE了几次,还好及时地发现,最后一句话没好好理解,原来空行还可以这么弄View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #define N 101 6 7 int father[N], citys; 8 double x[N], y[N], dis_sum; 9 typedef struct10 {11 int x,y; 12 double w;13 }edge;14 edge e[N*N+1];15 16 in 阅读全文
posted @ 2012-04-20 20:52 zhongya 阅读(193) 评论(0) 推荐(0)
摘要:题目意思很简单计算出所修电线的最小值,很明显Kruskal算法比Prim算法要简单。View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #define N 505 6 7 int father[N],n,row,dis_sum; 8 typedef struct 9 {10 int x,y;11 int w;12 }edge;13 edge e[N*N/2];14 int cmp(const void *a,con 阅读全文
posted @ 2012-04-20 20:48 zhongya 阅读(254) 评论(0) 推荐(0)
摘要:题目的本意应该是叫你求出最小生成树的最大边,和边的个数,最后把每条边的连接情况打印出来。View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 #define N 15001 6 7 int father[1001];//按照模板敲的 8 int p[N], n, m; 9 typedef struct 10 {11 int x, y;12 int w; 13 }edge;14 edge e[N];15 16 int 阅读全文
posted @ 2012-04-19 23:29 zhongya 阅读(194) 评论(0) 推荐(0)
摘要:此算法在POJ上跑超时了,用了3s+;具体的思路和Prim算法一样View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 755 5 6 int rank[N],father[N]; 7 int dis_sum, x[N], y[N]; 8 typedef struct 9 {10 int x, y;11 int w; 12 }edge;13 edge e[N*N];14 15 int cmp(const void *a,const void *b 阅读全文
posted @ 2012-04-19 18:51 zhongya 阅读(261) 评论(0) 推荐(0)
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 760 5 #define Maxint 99999999 6 7 double lowcost[N], c[N][N]; 8 double x[N], y[N]; 9 int towns, m, s[N], closest[N],path[N][N];10 11 double distance(int i,int j)//用double为了防止int数据存不下12 {13 return (x 阅读全文
posted @ 2012-04-19 17:07 zhongya 阅读(185) 评论(0) 推荐(0)
摘要:题目大意是给你几组数据,表示每个人之间的信息传递的时间,要求所用时间最短的人的编号和所用的最短时间。可以用图论中的Floyd算法,经过Floyd算法后在邻接矩阵当中的就有了每个人的信息传递时间及编号;接下来要做的是从邻接矩阵中查找信息。View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int cost[101][101],n; 6 void Floyd() 7 { 8 int i,j,k; 9 for(k=1; k<=n; k++)10 for(i=1 阅读全文
posted @ 2012-04-10 22:37 zhongya 阅读(237) 评论(0) 推荐(0)
摘要:题目大意:给你几种货币,并给出货币之间的汇率,问你是否存在有一种货币经过兑换后,可以盈利赚钱,这就是所谓的炒外汇吧!嘿嘿!View Code #include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>int main(){ int i, j, k, ncases, n, ans=1, flag; char name[30][300]; char from[31], to[31]; double rate, cur[31][31]; while(scanf(" 阅读全文
posted @ 2012-04-10 22:25 zhongya 阅读(162) 评论(0) 推荐(0)