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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
| //Dijkstra
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
template<typename T>
void Swap(T& a,T& b){T t=a;a=b;b=t;}
template<typename T>
int read(T& w)
{
char r=getchar();bool f=false;
if(r==-1)return -1;
for(;(r<48||r>57)&&r!='-';r=getchar());
if(r=='-')r=getchar(),f=true;
for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
if(f)w=-w;
return 1;
}
const int maxn=103;
const int maxm=10003;
int n,m;
struct EDGE
{
int to,nxt,w;
void init(int too,int nxtt,int ww)
{
to=too;nxt=nxtt;w=ww;
}
}edge[maxm<<1];
int ek=0;
int node[maxn];
struct p
{
int d,id;
p(){d=id=0;}
p(int idd,int dd)
{
d=dd,id=idd;
}
};
void addEdge(int from,int too,int ww)
{
edge[++ek].init(too,node[from],ww);
node[from]=ek;
}
bool operator<(const p& a,const p& b)
{
return a.d<b.d;
}
int to[maxn];
//小根堆
const int maxnode=maxn;
template<typename T>
struct HEAP
{
T ary[maxnode];
int f;
HEAP(){f=0;}
void clear(){f=0;}
bool empty(){return f==0;}
void push(const T& w)
{
ary[++f]=w;
for(int k=f;k!=1&&ary[k]<ary[k/2];k/=2)
{
Swap(ary[k],ary[k/2]);
Swap(to[ary[k].id],to[ary[k/2].id]);
}
}
void adjust(int k,int dd)
{
if(dd<ary[k].d)ary[k].d=dd;
for(;k!=1&&ary[k]<ary[k/2];k/=2)
{
Swap(ary[k],ary[k/2]);
Swap(to[ary[k].id],to[ary[k/2].id]);
}
}
T top() {return ary[1];}
void pop()
{
ary[1]=ary[f--];
for(int k=1,son=k*2;son<=f;k=son,son=k*2)
{
if(son+1<=f&&ary[son+1]<ary[son])
++son;
if(ary[son]<ary[k])
Swap(ary[son],ary[k]),
Swap(to[ary[son].id],to[ary[k].id]);
else break;
}
}
};
HEAP<p> q;
int s,t;
typedef unsigned int uint;
const uint uinf=4294967295u;
uint d[maxn];
bool ok[maxn];
void Dijkstra()
{
memset(d+1,255,n*sizeof(uint));
memset(ok+1,0,n*sizeof(bool));
memset(to+1,0,n*sizeof(int));
q.clear();
if(!to[s])
to[s]=q.f+1;
q.push(p(s,0));
for(int i=1;i<=n&&!q.empty();++i)
{
p tmp;
do
{ tmp=q.top();
q.pop();
to[tmp.id]=0;
}while(ok[tmp.id]&&!q.empty());
ok[tmp.id]=true;
d[tmp.id]=tmp.d;
for(int j=node[tmp.id];j;j=edge[j].nxt)
{
int v=edge[j].to;
if(d[tmp.id]!=uinf&&!ok[v])
if(d[tmp.id]+edge[j].w<d[v])
{
if(!to[v])
to[v]=q.f+1,q.push(p(v,d[tmp.id]+edge[j].w));
else q.adjust(to[v],d[tmp.id]+edge[j].w);
}
}
if(tmp.id==t)break;
}
}
void input()
{
ek=0;
memset(node+1,0,n*sizeof(int));
int x,y,l;
for(int i=1;i<=m;++i)
{
read(x),read(y),read(l);
if(x!=y)
{
addEdge(x,y,l);
addEdge(y,x,l);
}
}
s=1,t=n;
}
int main()
{
freopen("hdu2544_1.in","r",stdin);
//freopen("hdu2544.out","w",stdout);
while(read(n),read(m),n!=0)
{
input();
Dijkstra();
printf("%u\n",d[t]);
}
return 0;
}
|