将下列程序改成数组方式的,

jfszd 2009-01-19 08:57:17
// vs2005 Compile

#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) :

...全文
225 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
islife 2009-01-21
  • 打赏
  • 举报
回复
看了楼主的贴子,如果这个程序就是你需要的,那为什么非要改成数组形式呢?

用STL不好吗?还是因为你编译不过去?

STL的程序是肯定能用到以前的程序中的,

比如以前的函数要输入值是 char*, 那把vector<char> str, &str[0]传入就可以了
还有,如果传入 int d[4], 可以用vector<int> d(4), 然后传&d[0]就可以

你的数组也可以传到vector里来,int d[4], vector<int > vec(d,d+3)
hearoequal 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 hhyttppd 的回复:]
我猜你是想用二维数组存储结点间的关系。
数据结构变了,算法也得变,所以你最好还是重写,不是改改就行了的。
[/Quote]


说的对…………
我试着改写了下
用的指针数组
内存处理上有问题

它原来是用的向量的指针
向量内部是变量拷贝的
处理起来很不一样
我觉得要按照算法重写才行
纯粹把vector改写成数组行不通
waizqfor 2009-01-21
  • 打赏
  • 举报
回复
呵呵 解决了啊 兄弟替你高兴了 正想来看看 UP 18L
qqxiaoyatou 2009-01-21
  • 打赏
  • 举报
回复
ls强大 都牛人啊
hhyttppd 2009-01-21
  • 打赏
  • 举报
回复

#define N 6 //元素数
#define COST_MAX 1000 //最大int值

//查找最短路径
void Dijkstra(unsigned int graph[][N], int vBegin, int vEnd, unsigned int distance[N], int previous[N])
{
int i, j;
int selected; //当前选择项
unsigned int most_shortest, dis; //最短路径长度
int Unselected[N]; //尚未选择的项

selected = vBegin;

//初始化distance为最大值
for( i = 0; i < N; ++i)
{
distance[i] = COST_MAX;
previous[i] = -1;
Unselected[i] = 0;
}

distance[vBegin] = 0; //与自反距离为0
previous[selected] = selected;

//查找最短距离
for( i = 0; i < N; ++i)
{
//如果此点已被选入,则排除
if(Unselected[ i ] == -1) continue;

most_shortest = COST_MAX;

//查找并选择路径里的最短路径
for( j = 0; j < N; ++j )
{
if(Unselected[ j ] == -1) continue;

if(graph[ selected ][ j ] < most_shortest)
{
most_shortest = graph[ selected ][ j ];
selected = j;
}
}
Unselected[selected] = -1;

//对每条从preselected的边,重新计算另一顶点路径长度
for( j = 0; j < N; ++j)
{
if(graph[selected][j] == COST_MAX) continue;

dis = distance[selected] + graph[selected][j];

if(distance[j] > dis){
distance[j] = dis;
previous[j] = selected;
}
}

if(selected == vEnd) break;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
char* vetex[] = {"A","B","C","D","E","F"};
unsigned int distance[N];
int previous[N];

int begin = 0, end = 3;

unsigned int cost[N][N] =
{
{0, 6, 3, COST_MAX, COST_MAX, COST_MAX},
{6, 0, 2, 5, COST_MAX, COST_MAX},
{3, 2, 0, 3, 4, COST_MAX},
{COST_MAX, 5, 3, 0, 2, 3},
{COST_MAX, COST_MAX, 4, 2, 0, 5},
{COST_MAX, COST_MAX, COST_MAX, 3, 5, 0}
};

Dijkstra(cost, begin, end, distance, previous);

//输出最短路径
printf("the shortest path (%s -> %s)is[%d]:", vetex[begin], vetex[end], distance[end]);
printf("%S", vetex[end]);
while(previous[end] != begin)
{
end = previous[end];
printf("%S", vetex[end]);
}
printf("%S\n", vetex[begin]);

for(int i = 0; i < N; i++){
printf("the cost from %s to %s is %d\n", vetex[begin], vetex[i], distance[i]);
}

return 0;
}


hhyttppd 2009-01-21
  • 打赏
  • 举报
回复
Dijkstra算不能用来找最长路径。
而且你给的好像结果不对。
dreamsdark 2009-01-21
  • 打赏
  • 举报
回复
我试着改了一下,实在太麻烦了。基本上就是重新写了一遍
jfszd 2009-01-20
  • 打赏
  • 举报
回复
我要的程序和下面的差不多,但是这上程序是有向的,我要的是无向的,而且是求最短路径和最长路径,哪个高手帮忙吧
#i nclude "stdio.h"
#define BIG 9999 //无穷大
//Dijkstra算法函数,求给定顶点到其余各点的最短路径
//参数:邻接矩阵、顶点数、出发点的下标、结果数组
void Dijkstra(int Cost[][6],int n,int v0,int Distance[])
{
int s[6];
int mindis,dis;
int i,j,u;
//初始化
for(i=0;i<n;i++) {
Distance[i]=Cost[v0][i];
s[i]=0;
}
s[v0]=1; /*标记v0*/
//在当前还未找到最短路径的顶点中,
//寻找具有最短距离的顶点
for(i=1;i<n;i++) {//每循环一次,求得一个最短路径
mindis=BIG;
for(j=0;j<n;j++) //求离出发点最近的顶点
if(s[j]==0&&Distance[j]<mindis) {
mindis=Distance[j];
u=j;
} // if语句体结束,j循环结束
for(j=0;j<n;j++) //修改递增路径序列(集合)
if(s[j]==0) { //对还未求得最短路径的顶点
//求出由最近的顶点直达各顶点的距离
dis=Distance[u]+Cost[u][j];
//如果新的路径更短,就替换掉原路径
Distance[j]=(Distance[j]<dis)?
Distance[j]:dis;
} // if语句体结束,,j循环结束
s[u]=1; /* 标记最短路径已经求得*/
} // i循环结束
}
//主函数
void main()
{
//给出有向网的顶点数组
char *Vertex[6]={"V1","V2","V3","V4","V5","V6"};
//给出有向网的邻接矩阵
int Cost[6][6]={{0,BIG,5,30,BIG,BIG},
{2,0,BIG,BIG,8,BIG},
{BIG,15,0,BIG,BIG,7},
{BIG,BIG,BIG,0,BIG,BIG},
{BIG,BIG,BIG,4,0,BIG},
{BIG,BIG,BIG,10,18,0},
};
int Distance[6]; //存放求得的最短路径
int i;
//调用Dijkstra算法函数,求顶点V1到其余各点的最短路径
//参数:邻接矩阵、顶点数、出发点的下标、结果数组
Dijkstra(Cost,6,0,Distance);
for(i=0;i<6;i++)
printf("%s---->%s %d\n",
Vertex[0],Vertex[i],Distance[i]);
}

hhyttppd 2009-01-20
  • 打赏
  • 举报
回复
我猜你是想用二维数组存储结点间的关系。
数据结构变了,算法也得变,所以你最好还是重写,不是改改就行了的。
hai040 2009-01-20
  • 打赏
  • 举报
回复
把vector改成数组,编译,再改编译不过的地方
hhyttppd 2009-01-20
  • 打赏
  • 举报
回复
UP
fibbery 2009-01-20
  • 打赏
  • 举报
回复
严谨的说其他的地放基本不用动。
fibbery 2009-01-20
  • 打赏
  • 举报
回复
你把vector换成数组,将使用到的vector的方法换成自己的方法,其他的地方不用动。
waizqfor 2009-01-20
  • 打赏
  • 举报
回复
留个位置 帮着顶顶
jfszd 2009-01-20
  • 打赏
  • 举报
回复
当初没想那么多,惭愧啊
sagegz 2009-01-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jfszd 的回复:]
这是一个高手给写好的,但是我想改成数组方式的,所以请各位帮忙,程序的要求是:
根据图的内容能够求出a,b,c,d,e任意两点间的最长路径和最短路径,
在求路径的过程中,同样的点只能使用一次。下面是图的链接
http://hiphotos.baidu.com/jiafengshuang/abpic/item/a06f8fff238b512f5d60080d.jpg
[/Quote]
那当初为什么不让高手写成数组的呢?
hearoequal 2009-01-20
  • 打赏
  • 举报
回复
mark一下
jfszd 2009-01-19
  • 打赏
  • 举报
回复
jfszd 2009-01-19
  • 打赏
  • 举报
回复
jfszd 2009-01-19
  • 打赏
  • 举报
回复
这是一个高手给写好的,但是我想改成数组方式的,所以请各位帮忙,程序的要求是:
根据图的内容能够求出a,b,c,d,e任意两点间的最长路径和最短路径,
在求路径的过程中,同样的点只能使用一次。下面是图的链接
http://hiphotos.baidu.com/jiafengshuang/abpic/item/a06f8fff238b512f5d60080d.jpg

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧