将下列程序改成数组方式的,
#include "stdafx.h"
#include "iostream"
#include "vector"
#include "string"
#include "algorithm"
using namespace std;
struct node
{
node(string na) : name(na)
{
}
void join(node* oth, int distance)
{
if (find(conjoint_node.begin(), conjoint_node.end(), oth)
!= conjoint_node.end())
return;
conjoint_node.push_back(oth);
conjoint_dist.push_back(distance);
}
string name;
vector<node*> conjoint_node;
vector<int> conjoint_dist;
};
void _find_max_min_path(node* src, node* dst, vector<node*> curr_path, int curr_dist, vector<node*>* max_path, int* max_dist, vector<node*>* min_path, int* min_dist)
{
for (size_t i = 0; i < src->conjoint_node.size(); ++i)
{
node* pnode = src->conjoint_node[i];
int dist = src->conjoint_dist[i];
if (find(curr_path.begin(), curr_path.end(), pnode) != curr_path.end())
continue;
vector<node*> new_curr_path = curr_path;
int new_curr_dist = curr_dist;
new_curr_path.push_back(pnode);
new_curr_dist += dist;
if (pnode == dst)
{
if (new_curr_dist > *max_dist)
{
*max_path = new_curr_path;
*max_dist = new_curr_dist;
}
if (new_curr_dist < *min_dist)
{
*min_path = new_curr_path;
*min_dist = new_curr_dist;
}
}
else
{
_find_max_min_path(pnode, dst, new_curr_path, new_curr_dist, max_path, max_dist, min_path, min_dist);
}
}
}
void find_max_min_path(node* src, node* dst, vector<node*>* max_path, int* max_dist, vector<node*>* min_path, int* min_dist)
{
vector<node*> curr_path;
curr_path.push_back(src);
*max_dist = INT_MIN;
*min_dist = INT_MAX;
_find_max_min_path(src, dst, curr_path, 0, max_path, max_dist, min_path, min_dist);
}
int main()
{
vector<node*> nodes;
node a("a");
node b("b");
node c("c");
node d("d");
node e("e");
nodes.push_back(&a);
nodes.push_back(&b);
nodes.push_back(&c);
nodes.push_back(&d);
nodes.push_back(&e);
a.join(&b, 8);
a.join(&e, 4);
b.join(&a, 8);
b.join(&c, 5);
b.join(&d, 6);
c.join(&b, 5);
c.join(&d, 3);
c.join(&e, 2);
d.join(&b, 6);
d.join(&c, 3);
d.join(&e, 4);
e.join(&a, 4);
e.join(&c, 2);
e.join(&d, 4);
do
{
vector<node*> max_path;
int max_dist = 0;
vector<node*> min_path;
int min_dist = 0;
node* src = NULL;
node* dst = NULL;
string src_name;
string dst_name;
cout<<"tatal has node : "<<a.name<<", "<<b.name<<", "<<c.name<<", "<<d.name<<", "<<e.name<<endl;
cout<<"begin node : ";
cin>>src_name;
cout<<"end node : ";
cin>>dst_name;
for (size_t i = 0; i < nodes.size(); ++i)
{
if (nodes[i]->name == src_name)
src = nodes[i];
if (nodes[i]->name == dst_name)
dst = nodes[i];
}
if (!src || !dst)
{
cout<<"input error, again"<<endl;
continue;
}
find_max_min_path(src, dst, &max_path, &max_dist, &min_path, &min_dist);
cout<<"max path (distance is " <<max_dist<<") : ";
for (size_t i = 0; i < max_path.size(); ++i)
cout<<max_path[i]->name<<((i == max_path.size() - 1) ? "\n" : "->");
cout<<"min path (distance is " <<min_dist<<") : ";
for (size_t i = 0; i < min_path.size(); ++i)
cout<<min_path[i]->name<<((i == min_path.size() - 1) ? "\n" : "->");
string exit_flag;
cout<<endl<<"continue ? (y/n) : ";
cin>>exit_flag;
if (exit_flag == "y" || exit_flag == "Y")
continue;
else
break;
} while (true);
return 0;
}
测试结果
tatal has node : a, b, c, d, e
begin node : a
end node : b
max path (distance is 16) : a->e->d->c->b
min path (distance is 8) : a->b
continue ? (y/n) : y
tatal has node : a, b, c, d, e
begin node : a
end node : e
max path (distance is 20) : a->b->c->d->e
min path (distance is 4) : a->e
continue ? (y/n) : y
tatal has node : a, b, c, d, e
begin node : c
end node : d
max path (distance is 21) : c->b->a->e->d
min path (distance is 3) : c->d
continue ? (y/n) :
相关推荐
数组的各种输入输出方式
数组的各种输入输出方式 数组的各种输入输出方式 数组的各种输入输出方式 数组的各种输入输出方式
Java创建数组的几种方式总结
下面小编就为大家带来一篇Java创建数组的几种方式总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
C# 拷贝数组的几种方式.
讲述了C#编程过程中数组集中拷贝方式之间的区别。介绍了信号量机制和使用。
VC++数组的传递方式
#include #include //产生随机数所需头文件 using namespace std; using std::cout; using std::cin; int average(int b[10]) //求平均数函数 { int i,sum=0; int aver; for ( i=0;i<10;i++) sum+=b[i]; aver=sum/10; return aver; } int max(int c[10]) //求最大值函数 { int i,max=c[0]; for ( i=1;imax)max=c[i]; return max; } int min(int d[10]) //求最小值函数 { int i,min=d[0]; for ( i=1;i<10;i++) if (d[i]<min)min=d[i]; return min; } int main(int argc, char* argv[]) { int a[10]; unsigned seed; cout<>seed; //输入随机数种子 srand(seed); //将种子传递给rand() for (int i=0;i<10;i++) { a[i]=rand()%100; //产生0~100之间的随机整数 cout<<a[i]<<','; } cout<<endl; cout<<"平均值是:"<<average(a)<<endl; //数组作为参数传递 cout<<"最大值是:"<<max(a)<<endl; cout<<"最小值是:"<<min(a)<>i; return 0; }
JS常用的几种数组遍历方式以及性能分析对比实例详解
本文实例讲述了JS常用的几种数组遍历方式以及性能分析对比。分享给大家供大家参考,具体如下: 前言 这一篇与上一篇 JS几种变量交换方式以及性能分析对比 属于同一个系列,本文继续分析JS中几种常用的数组遍历方式以及各自的性能对比 起由 在上一次分析了JS几种常用变量交换方式以及各自性能后,觉得这种方式挺好的,于是抽取了核心逻辑,封装成了模板,打算拓展成一个系列,本文则是系列中的第二篇,JS数组遍历方式的分析对比 JS数组遍历的几种方式 JS数组遍历,基本就是for,forin,foreach,forof,map等等一些方法,以下介绍几种本文分析用到的数组遍历方式以及进行性能分析对比 第一种:普通


