2013年6月2日
摘要: 先输入顶点个数n,然后输入每条边的数据。顶点序号从0开始,最后一行为-1 -1 -1,表示输入结束。#include#include#include#include#include#include#includeusing namespace std;const int INF=1000000;const int maxn=20;int n;int edge[maxn][maxn];int s[maxn];int dist[maxn];int path[maxn];void dijkstra(int v){ for(int i=0;i0;j--) printf("%d... 阅读全文
posted @ 2013-06-02 20:34 ∑求和 阅读(244) 评论(0) 推荐(0) 编辑
2013年6月1日
摘要: 链接:http://poj.org/problem?id=1789题意:懒得写。思路:要让分式值最大,分母值就要最小。把卡车类型理解为无向网中的顶点,所求最佳方案即为求最小生成树,那个求和就是权值。每两个顶点间边的权值为对应两种卡车编码之间的距离,故先求出邻接矩阵。#include#include#include#include#include#include#includeusing namespace std;const int INF=1000000;const int maxn=2000+5;const int codel=7+2;int n,sumw;int edge[maxn][m 阅读全文
posted @ 2013-06-01 21:26 ∑求和 阅读(182) 评论(0) 推荐(0) 编辑
摘要: 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586题意:有n个适配器,不同的适配器价格不同,并给出两个适配器之间如果连接所需的网线价格,要用网线使它们连通,求所需最小费用。思路:计算建立网络的最小费用,是个最小生成树问题。注意在构造有向网时,每条边的权值为网线的价格加上网线所连接的两个适配器的价格。由于只要求最小生成树的权值而不求选择的边,所以把lowcost数组和nearvex数组合并,lowcost[i]=-1仍表示i在集合T内。#include#include#include#include#include# 阅读全文
posted @ 2013-06-01 16:34 ∑求和 阅读(296) 评论(0) 推荐(0) 编辑
摘要: n,m表示顶点数和边数。lowcost[]存放顶点集合T'内各顶点到顶点集合T内各顶点权值最小的边的权。nearvex[]记录顶点集合T'内各顶点距离顶点集合T内那个顶点最近。nearvex[i]为-1时,表示顶点i属于集合T.#include#include#include#include#include#include#includeusing namespace std;const int INF=1000000;const int maxn=21;int n,m;int edge[maxn][maxn];int lowcost[maxn];int nearvex[max 阅读全文
posted @ 2013-06-01 12:52 ∑求和 阅读(377) 评论(0) 推荐(0) 编辑
2013年5月31日
摘要: 链接:http://poj.org/problem?id=1861题意:给n个路由器,注意标号为1到n,m表示这n个路由器之间的连接关系数,要连接这些路由器,且必须保证最长的单根网线的长度是所有方案中最小的。输出连接方案中最长网线的长度和所用网线的根数以及每根网线所连路由器的编号。思路:没有直接要求求最小生成树,可以知道,所选网线条数必是n-1。另外,可以证明,对于一个图的最小生成树来说,它的最大边满足在所有生成树的最大边里最小。由于kruscal算法是先选长度较小的边,所以长度最大的边必然是最后选定的一条边。还有,poj和zoj上的这道题Sample Output有错误。#include#i 阅读全文
posted @ 2013-05-31 22:31 ∑求和 阅读(248) 评论(0) 推荐(0) 编辑
摘要: 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1203题意:给出n个城市的坐标,计算连接这n个城市所需路线总长度的最小值。思路:先求出每两个城市间的距离,也就是边权,然后用kruscal来求最小生成树,得出最小值。注意输出格式,要求每两个输出之间要有一个空行。但是最后的一个输出后面不能有空行,所以判断是不是第一个输出,不是的话,先打一个空行。一开始写wa了,找了很久错误没找出来,后面发现是cmp()函数写的有问题。一开始我是直接把上次写的kruscal()示例程序中的cmp()函数粘贴了过来。没想问题,结果,呵呵 阅读全文
posted @ 2013-05-31 12:59 ∑求和 阅读(229) 评论(0) 推荐(0) 编辑
2013年5月30日
摘要: #include#include#include#include#include#include#includeusing namespace std;const int maxn=10;struct ArcNode{ int to; struct ArcNode *next;};int n,m;//顶点个数,边数ArcNode* List[maxn];//各顶点边链表的表头指针int count[maxn];//各顶点入度char output[100];//输出内容void TopSort(){ int top=-1; ArcNode* temp; bool ... 阅读全文
posted @ 2013-05-30 21:34 ∑求和 阅读(191) 评论(0) 推荐(0) 编辑
摘要: 其中n和m分别表示图的顶点数和边数。#include#include#include#include#include#include#include#includeusing namespace std;const int maxn=11;const int maxm=20;int n,m;struct Edge{ int u,v,w;} edges[maxm];class set{public: void makeset(int n) { for(int i=0; irank[y]) { father[y]=x; ... 阅读全文
posted @ 2013-05-30 21:25 ∑求和 阅读(161) 评论(0) 推荐(0) 编辑
2013年5月29日
摘要: 好久都没有刷题了,好久都没有写博客了。面壁反思一下。明天体育考试完后就会有时间了,健美操,终于可以和你说拜拜。真是人生一大幸事啊。再然后,过个十多天又要期末考,得开始复习,忧伤。随机过程真的是完全都不懂得状况,其他的几门也是勉勉强强。不能把acm作为成绩烂的理由,更何况,acm也没搞出什么名堂来。这点才是最最伤心。现在又到了怎么睡都睡不饱的状态了,就想躺床上挺尸。今天早上6点钟被热醒了,这长沙该死的天气。想当初,高考填志愿的时候,我信誓旦旦不报长沙的学校,主要是因为天气原因。结果,诶,命中注定啊。等下又要去跳操,还有英语作文。哎。。。 阅读全文
posted @ 2013-05-29 20:05 ∑求和 阅读(163) 评论(0) 推荐(0) 编辑
2013年5月21日
摘要: 链接:http://poj.org/problem?id=2243题意:给定棋盘上的两个位置a,b,计算马从a跳到b最少需要多少步。思路:要注意象棋中是马走日。然后就用bfs求最小步数。#include#include#include#include#include#include#include#includeusing namespace std;int flag[9][9];map m;int sx,sy,ex,ey,mins;int dir[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};struct po 阅读全文
posted @ 2013-05-21 20:36 ∑求和 阅读(205) 评论(0) 推荐(0) 编辑