定义了Point类忘了用了结果代码变成了粪山
其中涉及贪婪算法,轮盘赌法,以及看不懂自己写的什么了
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <time.h>
#include<fstream>
#include<sstream>
using namespace std;
//created by jzyghsh
#define MAXPATH 9999999
struct Point {
//city class
string name;
double x, y;
int num;
};
void init();
double distance(Point a, Point b);
double greedyPath(vector<Point> v);
bool isIn(string str, vector<string> list);
void nextCity();
int findNumOfCity(string name);
string RWS(vector<string> visitList);
void update();
int n;//city number
int m;//ant number
int alpha = 1, beta = 2;
double rou = 0.5;
vector<Point> cities;//city group (unchaged)
vector<vector<double>> dist;//distance list (unchaged)
vector<vector<double>> message;//message level on every road
vector<vector<string>> visitedCities;//abandon list
vector<double> p;
vector<double> C;
double minPath = FLT_MAX;
vector<string> bestPath;
int nextCityNum;
int main() {
#ifndef ONLINE_JUDGE
freopen("E:\\txts\\wi29.txt", "r", stdin);
#endif
int times = 0;
int i;
double difference = 0.0;
vector<int> count;
init();
while (times<=100) {
nextCity();
update();
times++;
}
nextCity();
cout << "共生成" << times * m << "只蚂蚁" << endl;
cout << "最终路径为:";
for (i = 0; i < bestPath.size(); i++) {
cout << bestPath[i] << "->";
}
cout << "最终路径长度为:" << minPath << endl;
return 0;
}
void nextCity() {
int i, j, k, begin, first;
string nextVisit;
int notVisit,nowVisit;
double up, pi, totalUp = 0, ci = 0;
C.clear();
for (i = 0; i < m; i++) {
notVisit = n - visitedCities[i].size();
ci = 0;
C.push_back(ci);
vector<string> visitList;
first = 0;
while (notVisit > 0) {
visitList.clear();//available visit list
totalUp = 0;
p.clear();
for (j = 0; j < n; j++) {
if (visitedCities[i].back() == cities[j].name) {
nowVisit = j;
if (first == 0) {
begin = j;
first = 1;
}
break;
}
}
for (j = 0; j < n; j++) {
if (!isIn(cities[j].name, visitedCities[i])) {
//cout << cities[j].name << "not in" << endl;
up = pow(message[nowVisit][j], alpha) * pow((1 / dist[nowVisit][j]), beta);//i有问题
totalUp += up;
p.push_back(up);
visitList.push_back(cities[j].name);
//cout << visitList.back() << " ";
}
}
//cout << endl;
//created by jzyghsh
//cout << p.size() << endl;
for (j = 0; j < p.size(); j++) {
p[j] /= totalUp;
}
nextVisit = RWS(visitList);
visitedCities[i].push_back(nextVisit);
//cout << "add" << visitList[nextVisit] << endl;
for (k = 0; k < n; k++) {
if (nextVisit == cities[k].name) {
nextCityNum = k;
}
}
C[i] += dist[nowVisit][nextCityNum];
//cout << cities[nowVisit].name << " " << cities[nextCityNum].name << endl;
notVisit--;
}
//cout << endl;
C[i] += dist[nextCityNum][begin];
//cout << cities[nextCityNum].name << " " << cities[begin].name << endl;
}
for (i = 0; i < m; i++) {
cout << "蚂蚁" << i << "爬过的路径为:";
visitedCities[i].push_back(visitedCities[i][0]);
for (j = 0; j < visitedCities[i].size(); j++) {
cout << visitedCities[i][j] << "->";
}
cout << "路径长度为:" << C[i] << endl;
if (C[i] < minPath) {
minPath = C[i];
bestPath = visitedCities[i];
}
}
}
string RWS(vector<string> visitList) {
//select a city
double q, area = 0, temp;
string t;
int i, j;
q = rand()/double(RAND_MAX);//0~1 double number
//cout << "q=" << q;
for (i = 0; i < visitList.size()-1; i++) {
for (j = i; j < visitList.size() - 1; j++) {
if (p[i] > p[i + 1]) {
temp = p[i];
p[i] = p[i + 1];
p[i + 1] = temp;
t = visitList[i];
visitList[i] = visitList[i + 1];
visitList[i + 1] = t;
}
}
}
for (i = 0; i < p.size(); i++) {
area += p[i];
if (q < area) {
//cout << "choose" << i << endl;
nextCityNum = i;
return visitList[i];
}
}
return visitList[0];
}
void update() {
int i, j;
int cityA = 0, cityB = 0;
for (i = 0; i < n; i++) {
// message fade
for (j = 0; j < n; j++) {
message[i][j] *= (1 - rou);
}
}
for (i = 0; i < m; i++) {
//message adding
for (j = 0; j < visitedCities[i].size()-1; j++) {
cityA = findNumOfCity(visitedCities[i][j]);
cityB = findNumOfCity(visitedCities[i][j + 1]);
message[cityA][cityB] += 1 / C[i];
//cout << cities[cityA].name << "to" << cities[cityB].name << endl;
}
//cout << endl;
}
for (i = 0; i < m; i++) {
//randomly put ants again
int ran;
visitedCities[i].clear();
ran = rand() % n;
visitedCities[i].push_back(cities[ran].name);
cout << "蚂蚁" << i << "初始在城市" << visitedCities[i].back() << endl;
}
}
int findNumOfCity(string name) {
int i;
for (i = 0; i < cities.size(); i++) {
if (name == cities[i].name) {
return i;
}
}
return NULL;
}
void init() {
int i = 0, j = 0;
double Cnn,t0;
cin >> n >> m;
for (i = 0; i < n; i++) {
Point p;
cin >> p.name >> p.x >> p.y;
p.num = i;
cities.push_back(p);
}
for (i = 0; i < n; i++) {
//init distance list
vector<double> v;
dist.push_back(v);
for (j = 0; j < n; j++) {
dist[i].push_back(distance(cities[i], cities[j]));
}
}
Cnn = greedyPath(cities);
t0 = m / Cnn;
cout << "用贪婪算法得到的初始路径长度为:" << Cnn << endl;
for (i = 0; i < n; i++) {
//init message list
vector<double> v;
message.push_back(v);
for (j = 0; j < n; j++) {
if (i != j) {
message[i].push_back(t0);
}
else {
message[i].push_back(0);
}
}
}
for (i = 0; i < m; i++) {
//randomly put ants
int ran;
vector<string> str;
ran = rand() % n;
str.push_back(cities[ran].name);
visitedCities.push_back(str);
str.clear();
cout << "蚂蚁" << i << "初始在城市" << visitedCities[i].back() << endl;
}
}
double distance(Point a,Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool isIn(string str,vector<string> list) {
int i = 0, length = list.size();
for (i = 0; i < length; i++) {
if (str == list[i]) {
return true;
}
}
return false;
}
double greedyPath(vector<Point> v) {
//greedy method get Cnn
double res = 0, nowPath, minPath;
int count = 0, begin, end;
int i = 0, j = 0;
vector<string> checked;
string choseCity;
int choseCityNum;
srand((unsigned)time(NULL));
begin = rand() % n;
end = begin;
checked.push_back(v[begin].name);
choseCityNum = begin;
while (count < v.size()-1) {
minPath = MAXPATH;
for (i = 0; i < n; i++) {
if (i != begin) {
nowPath = distance(v[begin], v[i]);
if (nowPath < minPath && !isIn(v[i].name,checked)) {
minPath = nowPath;
choseCity = v[i].name;
choseCityNum = i;
}
}
}
checked.push_back(choseCity);
begin = choseCityNum;
res += minPath;
count++;
}
checked.push_back(checked[0]);
cout << "贪婪算法路径为:";
for (i = 0; i < checked.size(); i++) {
cout << checked[i] << "->";
}
cout << endl;
res += dist[begin][end];
//cout << begin << " " << end;
return res;
}
文章浏览阅读9.1k次,点赞6次,收藏9次。hive 中有case when 的语法是:case when 条件1 then 结果1when 条件2 then 结果2when 条件3 then 结果3......else 结果x end那如果被查询的行同时符合条件1和条件3呢?结果会是出现“结果1”还是“结果3”呢?根据测试,是符合结果1,原因是语句先“碰见” when 条件1 then 结果1这一句。如果语句改为:se..._hive case when then
文章浏览阅读232次。今天内容1.信息收集(收集目标所有可以收集的信息) 工具 客户端 网页端域名信息(子域名)站点信息端口信息敏感信息2.扫描探测(awvs xray)漏洞的入口点——————————————————PPT:域名解析过程:用户–>浏览器输入baidu.com -->浏览器DXS服务器缓存–>系统缓存dns服务器缓存C://windows/system32/drivers/etc/host–>dns服务器(发送请求)whois查询备案域名划分子域名_中北网络域名
文章浏览阅读707次。一、学习内容本次课学习了函数的基本知识,需要大家对如下知识点进行总结:1. 函数定义的基本格式,函数定义和函数原型(声明)的区别何在?2. 函数的调用方式有哪几种3. 什么是形参,什么是实参,函数调用时的参数传递机制是什么?二、实验内容1.定义一个判断素数的函数isprime(int n),利用该函数输出1000以内的所有素数,每行10个,最后输出一共有多少个素数。(每列对齐)2.求两个正整数的最..._调用fun函数求最大公约数
文章浏览阅读3.8k次,点赞56次,收藏33次。在使用mybatis-plus开发需求的时候会发现对于大部分的业务场景来说都会使用到join来进行联表查询,但是mybatis-plus封装的 mapper 不支持 join,如果需要支持就需要自己手动去实现,给大家推荐一个好用的插件(Mybatis-Plus-Join(简称 MPJ)是一个 Mybatis-Plus的增强工具,在 MyBatis-Plus 的基础上只做增强不做改变,为简化开发、提高效率而生。_mybatisplus一对多
文章浏览阅读106次。基于JAVA学生信息管理系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署。springboot基于springboot和vue的酒店管理系统。springboot基于SpringBoot的自助旅游导航系统。springboot基于JSP的企业办公管理系统设计与实现。JSP宠物食品店系统的设计与实现sqlserver。ssm基于Java的幼儿早教系统软件的设计与实现。ssm基于vue的健康餐饮管理系统的设计与实现。ssm基于JAVA的求职招聘网站的设计与实现。
文章浏览阅读113次。一. 基本步骤 1.1环境准备 开始前,请确认gcc g++开发类库是否装好,默认已经安装。 注:等待linux下载更新功能准备好了 重启系统 在执行下载安装命令,如执行命令没有问题可以继续往下走 1. 最小Ubuntu安装插件 1. 需要安装 sudo apt-get install build-essen..._snail mock
文章浏览阅读103次。Pro LINQ:Language Integrated Query in C# 2008MS Press - Introducing Microsoft LINQLINQ for Visual C# 2005 (07年6月出版)LINQ for VB 2005 (07年6月最新PDF文字版)Manning:LINQ in ActionPro C# 2008..._c++ c# vb.net
文章浏览阅读1.1k次。confluence企业wiki搭建部署_ata confluence
文章浏览阅读830次。今天在创建SpringCloud项目过程中遇到了一个坑:当我将SpringCloud项目架子搭好之后,启动Eureka的时候报错(具体的错误提示忘记截图了),然后对问题摸索了好久之后才发现是SpringBoot与SpringCloud对应的版本问题。由于我项目中SpringBoot项目的版本用的是2.2.X,而SpringCloud的版本用的是 Greenwich.SR2所以造成了报错导致Eur..._springboot 2.1.1 对于的cloud
文章浏览阅读467次。无论是工作中还是生活里都离不开电脑,因为电脑可以用来编辑成我们需要的文件,还可以存储_磁盘恢复 csdn
文章浏览阅读8.2k次。网速测试大师(SpeedTest Master)致力于为全球用户提供快速专业的网络测速服务。【最新功能】5G测速、Ping 测试、游戏Ping、一键设备检测。网速测试大师,是您的手机管家,wifi管家,随时随地的测速专家【全新工具】重磅推出史上最强工具箱:Ping测试,游戏Ping,跟踪路由。。。轻松解决任何网络问题【一键测速】只需30秒,快速、准确获得网速测试结果【全网测试】支持各大运营商测速;..._苹果手机测速软件
文章浏览阅读7.7k次,点赞24次,收藏115次。近两三年,少儿编程教育迅速崛起,成了 STEM 教育的主要代表。缘起少儿编程这个概念在国内兴起,总有个三四年了。2016 年,曾经有人问:“儿童学习编程是不是为了将来做'程序猿'?”。我当时给的回答是: 编程说白了就是用一种简单的符号语言描述一种解决方案来解决实际问题。编出程序的效果取决于两个方面:1、对于实际业务问题的了解;2、对算法和数据的掌控。 这两者的基..._python编程少儿课程课后评价