最短路径算法
//面向邻接链表的DJ
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
const int MAX = 0x7fffffff;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int next;
int last;
}head[1001];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
typedef struct
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int next;
int valu;
int dis;
}type;
type p[40001];
int now;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void initm ( int n )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for ( int i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
head[i].next = head[i].last = -1;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void add ( int b, int e, int d )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
p[now].next = -1;
p[now].valu = e;
p[now].dis = d;
if ( head[b].next == -1 )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
head[b].next = now;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
p[ head[b].last ].next = now;
}
head[b].last = now ++;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int used[1001];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int dis[1001];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void initdj ( int s, int n, int *di )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for ( int i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
used[i] = 0;
di[i] = MAX;
}
di[s] = 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int dj ( int s, int t, int n, int *di )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
initdj ( s, n, di );
int i, j;
int now;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for ( i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
type min;
min.dis = MAX;
for ( j=0; j<n; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( min.dis > di[j] && ! used[j] )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
min.dis = di[j];
min.valu = j;
}
}
if ( min.valu == t )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
break;
}
now = min.valu;
used[ min.valu ] = 1;
for ( j=head[now].next; j!=-1; j=p[j].next )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( ! used[ p[j].valu ] && di[ p[j].valu ] > di[now] + p[j].dis )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
di[ p[j].valu ] = di[now] + p[j].dis;
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return di[t];
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int t, n;
int b, e, r;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while ( scanf ( "%d%d", &t, &n ) != EOF )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
initm ( n );
for ( int i=0; i<t; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf ( "%d%d%d", &b, &e, &r );
add ( b-1, e-1, r );
add ( e-1, b-1, r );
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
printf ( "%d\n", dj ( n-1, 0, n, dis ) );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论
![](/images/xml.gif)
阅读排行榜
评论排行榜
|
|