sf
#include <stdio.h>
#include <time.h>
#include <stdlib.h> #define MAXN 150 //最大节点数
#define INF ((1<<31)-1) //无穷大数 int dist[MAXN][MAXN]; //记录两节点之间的距离
int parent[MAXN][MAXN]; //记录节点a到b路径中b的父节点 double random_d() //生成0~1间的随机数
{
return (double)rand()/RAND_MAX;
} int random_i(int m) //生成0~m间的随机数
{
return (int)(random_d()*(m-) + 0.5);
} void initial(int n) //初始化dist数组和parent数组
{
int a, b;
for(a = ; a < n; a++)
{
for(b = ; b < n; b++)
{
if(a != b)
{
dist[a][b] = INF; //初始阶段距离未知暂时定为无穷大
parent[a][b] = -;
}else{ //此时a等于b
dist[a][b] = ; //节点a到节点a自身的距离是0
parent[a][b] = a;
}
}
}
} void floyd(int n) //floyd算法,计算两点之间的最短距离
{
int a, b, k;
for(k=; k<n; k++)
{
for(a=; a < n; a++)
{
for(b = ; b < n; b++)
{
if(dist[a][k] == INF || dist[k][b] == INF) continue;
if(dist[a][b] > dist[a][k] + dist[k][b])
{
dist[a][b] = dist[a][k] + dist[k][b];
parent[a][b] = parent[k][b];
}
}
}
}
} void generate_graph(int n) //随机生成一张连通图
{
int i, j, v;
initial(n); //进行初始化
for(i = ; i<n; i++) //保证连通性,让0号节点与其他所有点相连
{
dist[][i] = dist[i][] = random_i() + ; //保证算法起作用,加大权值
parent[][i] = ;
parent[i][] = i;
}
for(i=; i<n; i++ )
{
for(j=i+; j<n; j++)
{
v = random_i() ; //随机生成权值
if(v != )
{
dist[i][j] = dist[j][i] = v;
parent[i][j] = i;
parent[j][i] = j;
}
}
}
} void show_graph(int n) //展示节点之间的连接关系及对应的权值
{
int i, j;
for(i=; i<n; i++)
{
for(j=i+; j<n; j++)
if(dist[i][j] != INF) printf("Direct distance between %d and %d: %d\n", i, j, dist[i][j]);
}
} void show_route(int a, int b) //显示a到b的距离最短的路径
{
if(a == b){
printf("%d", b);
}else{
show_route(a, parent[a][b]);
printf("-%d", b);
}
} int main()
{
int a, b;
srand(time(NULL)); //初始化随机数发生器种子 generate_graph(); //随机生成一张连通图
show_graph(); //展示节点之间的连接关系及对应的权值
floyd(); //floyd算法,计算两点之间的最短距离
printf("Please input starting point and end point: "); //提示用户输入两个节点,两个输入之间用逗号隔开,并保证节点编号小于100
scanf("%d %d", &a, &b); //读取用户输入
printf("The distance is %d\n", dist[a][b]); //输出最短距离
printf("The way is ");
show_route(a, b); //输出最短路径
printf("\n");
//printf("%d\n", INF);
return ;
}
sf的更多相关文章
- maven 加入json-lib.jar 报错 Missing artifact net.sf.json-lib:json-lib:jar:2.4:compile
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</art ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法
使用Hibernate manytoone属性关联主表的时候,如果使用JSONArray把pojo对象转换成json对象时,很容易出现循环的异常.解决的办法就是, 在转换json对象时忽略manyto ...
- Json_异常_net.sf.json.JSONException: JSONObject["solution"] not found.
net.sf.json.JSONException: JSONObject["solution"] not found. 没有这个元素造成的. 问题代码: JSONObject j ...
- 使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject
先是报 java.lang.NoClassDefFoundError: net/sf/json/JSONObject 这个错误, 打开项目属性找到java build path中的libaries,找 ...
- Net.Sf.Json java Object to JsonObject
public class People{ private String name; public void setName(String name){ this.name = name; } publ ...
- spark mllib配置pom.xml错误 Multiple markers at this line Could not transfer artifact net.sf.opencsv:opencsv:jar:2.3 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org
刚刚spark mllib,在maven repository网站http://mvnrepository.com/中查询mllib后得到相关库的最新dependence为: <dependen ...
- 获取<img src="sdf.jpg" Big="sf.jpg">中的big的值
原代码: <img src="sdf.jpg" Big="sf.jpg" onclick="getsrc($(this).attr(" ...
- java.lang.ClassNotFoundException: net.sf.json.JSONArray,java.lang.NoClassDefFoundError: net/sf/json/JSONArray jetty跑项目遇到的问题
2016-05-18 15:44:25 ERROR Dispatcher.error[user:|url:]:L38 - Dispatcher initialization failed Unable ...
- 【pom.xml 依赖】使用net.sf.json-lib-2.4-jdk15.jar所需要的其他依赖架包 以及其一直在pom.xml报错的问题
特此声明: json-lib-2.4-jdk15.jar仅它本身不够,必须如下的几个依赖架包都有才能使用!!! 首先 将.json-lib-2.4-jdk15.jar以及其相关的依赖架包的正确配置给出 ...
- net.sf.json 时间格式的转化
后台代码 //后台代码 response.setCharacterEncoding("UTF-8"); JsonConfig jsonConfig = new JsonConfig ...
随机推荐
- NPOI导出为Excel文件
1.添加引用 2.将ExcelRender.cs和SqlHelper.cs两个类拷贝到App_Code文件夹下 3.写后台代码 eg:根据部门和日期导出成绩表 /// <summary> ...
- java多线程并发例子
public static void main(String[] args) { for(Thread t:getThreads()){ t.start(); } } public static Th ...
- GetProcessIdOfThread在WinXP及之前操作系统的替代实现
还是学习VLD2.X版本看到的: 在Windows XP及之前的操作系统没有提供GetProcessIdOfThread的API,这里给出了一个替代的实现方式: 头文件: #if _WIN32_WIN ...
- 仿淘宝TAB切换搜索框
<div class="search"> <div id="searchBox"> <ul class="tab-bar ...
- JS中定义类的方法<转>
转载地址:http://blog.csdn.net/sdlfx/article/details/1842218 PS(个人理解): 1) 类通过prototype定义的成员(方法或属性),是每个类对象 ...
- 稠密图(邻接矩阵),并查集,最短路径(Dijkstra,spfa),最小生成树(kruskal,prim)
全部函数通过杭电 1142,1162,1198,1213等题目测试. #include<iostream> #include<vector> #include<queue ...
- 如何在IIS 中配置应用程序(Convert to Application)?
1.打开IIS 2.选择待操作的虚拟目录 3.鼠标右键,点击"Convert to Application” 4.点击connect as 5.选中Specific user,并点击Set ...
- collection set
http://blog.csdn.net/humingfiy/article/details/7946408 Collection:List.SetMap:HashMap.HashTable 如何在它 ...
- 每天一道题:LeetCode
本人是研二程旭猿一枚,还有半年多就要找工作了,想想上一年度面试阿里的算法工程师挂了,心有不甘啊,主要还是准备不足,对一些常见的算法问题没有去组织准备,为了明年找一份好的实习,就从现在开始,好好准备吧, ...
- ceph for openstack快速部署实施
for el6 即centos6 1,添加ceph 官方yum源 [root@ruiy ~]# cat /etc/yum.repos.d/ceph.repo[ceph-noarch]name=Ceph ...