紧急呼救 !!!!高分求购单源最短路径原代码

duanmingguo 2004-01-08 10:14:49
程序执行结果要用图形显示。
再线等。

...全文
244 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
sharkhuang 2004-01-12
  • 打赏
  • 举报
回复
回溯发
xjshuaishuai 2004-01-12
  • 打赏
  • 举报
回复
要不要sql server 写的呀?
不给不意思,所有300多个网点全部计算一次才用了一分45秒,我公司刚形究的代码
xjshuaishuai 2004-01-12
  • 打赏
  • 举报
回复
#include"stdio.h"
#define MAX 20
#define INFI 9999
main()
{
int cost[MAX][MAX];
int i,n,distan[MAX];
scanf("%d",&n);
input_cost(n,cost);
dijkstra(cost,n,ditan);
for(i=0;i<n;i++)
printf("%d%d\n",i,distan[i]);
}
input_cost(int n,int cost[MAX][MAX])
{ int i,j,v1,v2,w;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cost[i][j]=INFI;
scanf("%d%d%d",&v1,&v2,&w);
while(v1!=-1&&v2!=-1)
{
cost[v1][v2]=w;
scanf("%d%d%d",&v1,&v2,&w);
}
}
dijkstra(int cost[MAX][MAX],int n,int distance[MAX])
{
int s[MAX];
int mindis,dis;
int i,j,v0,u;
printf("Input the source vertex:");
scanf("%d\n",&v0);
for(i=0;j<n;i++)
distan[i]=cost[v0][i];
s[i]=0;
}
s[v0]=1;
for(j=1;j<=n;j++)
mindis=INFI;
for(j=1;j<=n;j++)
if(s[j]==0;&&distan[j]<mindis)
{
u=j;
mindis=distan[j];
}
s[u]=1;
for(j=1;j<n;j++)
if(s[j]==0)
dis=distan[u]+cos[u][j];
distan[j]=(distan[j]<dis)?distan[j]:dis;
}
}
}

fjirie 2004-01-12
  • 打赏
  • 举报
回复
//领接链表, 有向图的

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <list>
#include <map>
using namespace std;

struct Edge
{
int dest; //destination , source is implicit
int weight;
Edge(int d, int w):dest(d), weight(w) {}
};

struct Vertex
{
string name;
list<Edge> *Adj;
int dist; //the distance from source(after running path algorithm)
int prev;
int marked;
};

struct HashItem
{
string name;
int rank;
};

class Graph
{
public:
Graph() : Infinity(100000), NumVertices(0) {}
~Graph();
void AddEdge(string source, string dest, int weight);
void SetDefault(); //set default graph
void SetDefault1();
int Dijkstra(string source); //dijkstra's shorest path algorethm
void print_path(string dest)
{ PrintPath(VertexMap.find(dest)->second.rank); }
private:
int AddNode(string VertexName);
void AddInternalEdge(int source, int dest, int weight);
void ClearData();
void PrintPathRec(int dest);
void PrintPath(int dest);
int Infinity;
int NumVertices;
map<string, HashItem> VertexMap; // dictionary
vector<Vertex> Table; //Graph table
};

Graph::~Graph()
{
for(int i=0; i<NumVertices; i++)
delete Table[i].Adj;
}

//if exsited already, return its rank
//else add it to vertex map and return its rank
int Graph::AddNode(string VertexName)
{
map<string, HashItem>::iterator p;
p = VertexMap.find(VertexName);

if(p!=VertexMap.end()) //case: was found
return (p->second).rank;

//now p is at the end of vertex map
HashItem HashV;
HashV.name = VertexName;
HashV.rank = NumVertices;
VertexMap.insert(pair<string, HashItem>(VertexName, HashV));

Vertex V;
V.name = VertexName;
V.Adj = new list<Edge>;
Table.push_back(V);

return NumVertices++;
}

void Graph::AddInternalEdge(int source, int dest, int weight)
{
Table[source].Adj->push_back(Edge(dest, weight));
}

void Graph::AddEdge(string source, string dest, int weight)
{
AddInternalEdge(AddNode(source), AddNode(dest), weight);
}

void Graph::ClearData()
{
for(int i=0; i<NumVertices; i++)
{
Table[i].dist = Infinity;
Table[i].prev = -1;
Table[i].marked = 0;
}
}

void Graph::PrintPathRec(int dest)
{
if(Table[dest].prev!=-1)
{
PrintPathRec(Table[dest].prev);
cout<<" to ";
}
cout<<Table[dest].name;
}

void Graph::PrintPath(int dest)
{
PrintPathRec(dest);
cout<<" (cost: "<<Table[dest].dist<<" ) "<<endl;
}

void Graph::SetDefault()
{
AddEdge("V0", "V1", 2);
AddEdge("V0", "V3", 1);
AddEdge("V1", "V3", 3);
AddEdge("V1", "V4", 4);
AddEdge("V4", "V6", 6);
AddEdge("V3", "V4", 2);
AddEdge("V3", "V6", 4);
AddEdge("V3", "V5", 8);
AddEdge("V3", "V2", 2);
AddEdge("V6", "V5", 1);
AddEdge("V2", "V0", 4);
AddEdge("V2", "V5", 5);
}

void Graph::SetDefault1()
{
AddEdge("A", "B", 12);
AddEdge("A", "D", 87);
AddEdge("B", "E", 11);
AddEdge("D", "B", 23);
AddEdge("D", "C", 10);
AddEdge("C", "A", 19);
AddEdge("E", "D", 43);
}


struct Path
{
int dest; //w
int cost; //D(w)
Path(int d, int c):dest(d), cost(c) {}
bool operator<(const Path& Rhs) const { return cost < Rhs.cost; }
};

int Graph::Dijkstra(string source)
{
int StartNode = VertexMap.find(source)->second.rank;

int V, W;
priority_queue<Path, vector<Path>, greater<Path> > PQ;

ClearData();
Table[StartNode].dist = 0;
PQ.push(Path(StartNode, 0));
Path VRec(0, 0);

for(int NodesSeen=0; NodesSeen<NumVertices; NodesSeen++)
{
do //find an unvisited vertex
{
if(PQ.empty())
return 1;
VRec = PQ.top();
PQ.pop();
} while(Table[VRec.dest].marked);

V = VRec.dest;
Table[V].marked = 1;

for(list<Edge>::iterator p=Table[V].Adj->begin();
p!=Table[V].Adj->end(); p++)
{
W = p->dest;
int Cvw = p->weight;
if(Cvw < 0)
{
cerr<<"Graph has negative edges"<<endl;
return 0;
}
if(Table[W].dist > Table[V].dist + Cvw)
{
Table[W].dist = Table[V].dist + Cvw;
Table[W].prev = V;
PQ.push(Path(W, Table[W].dist));
}
}
}
return 1;
}

//test....
int main()
{
Graph g;
g.SetDefault1();
if(g.Dijkstra("A"))
g.print_path("C");
}
xmtanglyj 2004-01-12
  • 打赏
  • 举报
回复
8_1.c是什么?哪里找啊?——傻傻地问!
whf2008 2004-01-12
  • 打赏
  • 举报
回复
有没有c语言实现的校园导游咨询程序,校园路径为无向图。谢谢帮忙!任意输入两点信息,输出最短路径。
passion2002 2004-01-11
  • 打赏
  • 举报
回复
c语言:
#include <stdio.h>
#include <8_1.c> /*引入邻接矩阵创建程序*/
typedef enum{FALSE,TRUE} boolean;/*false为0,true为1*/
typedef int dist[m]; /* 距离向量类型*/
typedef int path[m]; /* 路径类型*/

/*------------Dijkstra最短路径算法-----------*/

void spath_dij(mgraph g,int v0,path p,dist d)
{ boolean final[m]; /*表示当前元素是否已求出最短路径*/
int i,k,j,v,min,x;

/* 第1步 初始化集合S与距离向量d */
for (v=0;v<g.n;v++)
{ final[v]=FALSE;
d[v]=g.edges[v0][v];
if (d[v]<FINITY && d[v]!=0) p[v]=v0; else p[v]=-1; /* v无前驱 */
}
final[v0]=TRUE; d[v0]=0; /* 初始时s中只有v0一个结点 */
/* 第2步 依次找出n-1个结点加入S中 */
for (i=1;i<g.n;i++)
{ min=FINITY;
for (k=0;k<g.n;++k) /*找最小边入结点*/
if (!final[k] && d[k]<min) {v=k;min=d[k];} /* !final[k] 表示k还在V-S中 */
printf("\n%c---%d\n",g.vexs[v],min); /*输出本次入选的顶点入距离*/
if (min==FINITY) return;
final[v]=TRUE; /* V加入S*/

/*第3步 修改S与V-S中各结点的距离*/
for (k=0;k<g.n;++k)
if ( !final[k] && (min+g.edges[v][k]< d[k]) )
{ d[k]=min+g.edges[v][k];
p[k]=v;
}
}/* end for */
}
void print_gpd(mgraph g,path p,dist d)
{ /*输出有向图的最短路径*/
int st[20],i,pre,top=-1;
for (i=0;i<g.n;i++)
{ printf("\nDistancd: %7d , path:" ,d[i]);
st[++top]=i;
pre=p[i];
while (pre!=-1) /*从第i个顶点开始向前搜索最短路径上的顶点*/
{ st[++top]=pre;
pre=p[pre];
}
while (top>0)
printf("%2d",st[top--]);
}
}
/*---------- 主程序 ------------*/
void main()
{ mgraph g; /* 有向图 */
path p; /* 路径向量 */
dist d; /* 最短路径向量 */
int v0;
creatmgraph(&g); /*创建有向网的邻接矩阵*/
print(g); /*输出图的邻接矩阵*/
printf("\n");
printf("please input the source point v0:");
scanf("%d",&v0); /*输入源点*/
spath_dij(g,v0,p,d); /*求v0到其他各点的最短距离*/
/*输出V0到其它各点的路径信息及距离*/
print_gpd(g,p,d);
}
bm1408 2004-01-11
  • 打赏
  • 举报
回复
BS 他!
pchia 2004-01-11
  • 打赏
  • 举报
回复
上面好像是delphi的,,你自己翻译一下啦
ChinaPlayer 2004-01-11
  • 打赏
  • 举报
回复
有C语言实现的么?
zchuer 2004-01-09
  • 打赏
  • 举报
回复
type
dim1=array[1..11] of integer;
const
n=5;
c:array[1..n,1..n] of integer=((0,10,maxint,30,100),
(maxint,0,50,maxint,maxint),
(maxint,maxint,0,maxint,10),
(maxint,maxint,20,0,60),
(maxint,maxint,maxint,maxint,0));


var
prev:dim1;
s:array[1..n] of boolean;
dist:array[1..n] of integer;
i,v,j:integer;

procedure Dijkstra(v:integer);
var
u:integer; temp,newdist:integer;
begin
for i:=1 to n do
begin
dist[i]:=c[v,i]; s[i]:=false;
if dist[i]=maxint then
prev[i]:=0
else
prev[i]:=v;
end;
dist[v]:=0;s[v]:=true;
for i:=1 to n-1 do
begin
temp:=maxint;
u:=v;
for j:=1 to n do
if (not s[j]) and (dist[j]<temp) then
begin u:=j;temp:=dist[j]; end;

s[u]:=true;
for j:=1 to n do
if (not s[j]) and (c[u,j]<maxint) then
begin
newdist:=dist[u]+c[u,j];
if newdist<dist[j] then
begin dist[j]:=newdist; prev[j]:=u; end;
end;
end;{for i}
end;{proc Dijkstra}
Begin
v:=1;
Dijkstra(v);
write('i=');
readln(i);
writeln(dist[i]);
while i<>v do
begin
write(i,'<---');i:=prev[i];
end;
writeln(v);

End.
zchuer 2004-01-09
  • 打赏
  • 举报
回复
Public Function shortpath(startno As Integer, endno As Integer) As Single
以开始点,结束点为参数。
Dim result() As Single
Dim result1 As Integer
定义结果点
Dim s1 As Single
Dim min As Single
Dim ii, i, j, aa As Integer
Dim yc() As Boolean
Dim ycd() As Boolean
Dim rs1() As Single
Dim no() As Integer
Dim nopoint As Integer
ReDim yc(1 To maxno) As Boolean
ReDim ycd(1 To maxno) As Boolean
ReDim rs1(1 To maxno) As Single
ReDim result(1 To 2, 1 To maxno) As Single
定义结果,其中result(1,maxno)为结果点,result(2,maxno)为结果长度。
For i = 1 To maxno// maxno为网中最大的节点数。
yc(i) = False //标记已经查过的点。
ycd(i) = False //标记已经作结果点用过的点
rs1(i) = 1E+38 //假设从起点到任一点的距离都为无穷大
Next i
ll = startno //设置开始点。
yc(ll) = True //标记开始点为真。即已经作结果点用过。
j = 0
For aa = 1 To maxno
先从与开始点相连的终点寻找
For i = 1 To indexa1(2, ll) //以与ll点相连的起点的个数循环
result1 = b1(indexa1(1, ll) - i + 1)找出与LL点相连的终点的点号
s1 = c1(indexa1(1, ll) - i + 1) + result(2, ll)找出长度并求和
If yc(result1) = True Then GoTo 200如果以被经查过进行下一个
If ycd(result1) = True Then//如果已经作为结果点判断哪一个长
If rs1(result1) >= s1 Then//如果这一点到起点的长度比现在的路线长,替代
rs1(result1) = s1
result(1, result1) = ll//设置到这点的最短路径的前一点为LL点(精华部分)
result(2, result1) = s1设置到这点的最短路径长度
GoTo 200
Else
GoTo 200
End If
End If
如果上面的条件都不符合则进行下面的语句
ycd(result1) = True
rs1(result1) = s1
result(1, result1) = ll
result(2, result1) = s1
每找到一个点加一,为了下面的判断
j = j + 1
ReDim Preserve no(1 To j) As Integer
从新 定义数组并使其值为当前的点号
no(j) = result1
200 Next I
再从与开始点相连的终点寻找,与上面一样不再标注
For i = 1 To indexb2(2, ll)
result1 = a2(indexb2(1, ll) - i + 1)
s1 = c2(indexb2(1, ll) - i + 1) + result(2, ll)
If yc(result1) = True Then GoTo 300
If ycd(result1) = True Then
If rs1(result1) >= s1 Then
rs1(result1) = s1
result(1, result1) = ll
result(2, result1) = s1
GoTo 300
Else
GoTo 300
End If
End If
ycd(result1) = True
rs1(result1) = s1
result(1, result1) = ll
result(2, result1) = s1
j = j + 1
ReDim Preserve no(1 To j) As Integer
no(j) = result1
300 Next I

设置最小为无穷大,最短路径点为空
min = 1E+38
minpoint = Null
(优化部分)
找出已经查过点中长度最短的点
For i = aa To j
If min > rs1(no(i)) Then
ii = i
min = rs1(no(i))
minpoint = no(i)
End If
Next I
如果没有结果,即起点与终点没有通路退出程序
If min = 1E+38 Then Exit Function
(重点优化)将两点互换,减少循环。
no(ii) = no(aa)
no(aa) = minpoint
标记已经作为结果点判断过
yc(minpoint) = True
ll = minpoint
判断结果点是否等于终点,如果等于则已经找到最短路径
If minpoint = endno Then Exit For
Next aa
返回最短路径长度
Stpath = result(2, endno)
End Function
hongfeeling 2004-01-08
  • 打赏
  • 举报
回复
我只画过树,没画过图.
我是用层次遍历来画树的
duanmingguo 2004-01-08
  • 打赏
  • 举报
回复
拜托,想要分要下点本钱呀,
给我传上来我给50分,
否则我明天就结帐了,
zihu928 2004-01-08
  • 打赏
  • 举报
回复
数据结构上面有啊,关于最小生成树和最短路径问题
yaolan1999 2004-01-08
  • 打赏
  • 举报
回复
數據結構書上有。

69,335

社区成员

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

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