POJ2387(Dijkstra)
对于Dijkstra想做个总结,于是2387来了许多遍
第一次,很久以前的,那个时候还没认真想过Dij,我居然把循环都做完了,而不是找到目标点就跳出。觉得学的时候自己好多东西没认真想。
1
/**/
/*****************************
2
Source Code
3![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
4
Problem: 2387 User: Torres
5
Memory: 4164K Time: 94MS
6
Language: C++ Result: Accepted
7
******************************/
8
#include
<
iostream
>
9
using
namespace
std;
10
#define
MAX 1005
11
const
int
oo
=
10000000
;
12
int
dist[MAX][MAX];
13
int
close[MAX];
14
bool
used[MAX];
15![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
16
int
main()
17![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
{
18
int N,T,i,j;
19
int s,e,len;
20
scanf("%d%d",&T,&N);
21
memset(dist,0x7f,sizeof(dist));
22
memset(used,false,sizeof(used));
23
memset(close,0x7f,sizeof(close));
24![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(i=1;i<=T;i++)
{
25
scanf("%d%d%d",&s,&e,&len);
26
if(dist[s][e]>len)
27
dist[s][e]=dist[e][s]=len;
28
}
29
for(i=2;i<=N;i++)close[i]=dist[1][i];
30
used[1]=true;
31![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(i=2;i<=N;i++)
{
32
int min=0;int mlen=oo;
33
for(j=2;j<=N;j++)
34
if(!used[j]&&close[j]<mlen)
35
min=j,mlen=close[j];
36
used[min]=true;
37
for(j=2;j<=N;j++)
38![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
if(!used[j]&&dist[min][j]<oo)
{
39
int temp=dist[min][j]+close[min];
40
if(temp<close[j])
41
close[j]=temp;
42
}
43
}
44
printf("%d\n",close[N]);
45
return 0;
46
}
47![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
48
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
2
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
3
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
4
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
5
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
6
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
7
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
8
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
9
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
10
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
11
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
12
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
13
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
14
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
15
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
16
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
17
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
18
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
19
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
20
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
21
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
22
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
23
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
24
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
25
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
26
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
27
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
28
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
29
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
30
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
31
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
32
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
33
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
34
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
35
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
36
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
37
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
38
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
39
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
40
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
41
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
42
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
43
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
44
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
45
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
46
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
47
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
48
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
第二次用链表了内存降下来了
1
Source Code
2![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
3
Problem:
2387
User: Torres
4
Memory: 488K Time: 110MS
5
Language: G
++
Result: Accepted
6![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
7
Source Code
8
#include
<
iostream
>
9
#include
<
algorithm
>
10
#include
<
vector
>
11
using
namespace
std;
12
#define
Nmax 1005
13
#define
oo 0x7fffffff
14![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
15![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
typedef
struct
node
{
16
int end,len;
17![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
node(int a=0,int b=oo):end(a),len(b)
{};
18
}
node;
19![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
20
vector
<
node
>
dist[Nmax];
21![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
22
int
main()
23![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
{
24
int T,N,i,j;
25
int s,e,l;
26
scanf("%d%d",&T,&N);
27![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(i=1;i<=T;i++)
{
28
scanf("%d%d%d",&s,&e,&l);
29
dist[s].push_back(node(e,l));
30
dist[e].push_back(node(s,l));
31
}
32
vector<bool>used(N+1,false);
33
vector<int>closed(N+1,oo);
34
used[1]=true;
35
int len=dist[1].size();
36
for(i=0;i<len;i++)
37
if(closed[dist[1][i].end]>dist[1][i].len)
38
closed[dist[1][i].end]=dist[1][i].len;
39![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(i=1;i<N;i++)
{
40
int min=0,minlen=oo;
41
for(j=2;j<=N;j++)
42![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
if(!used[j]&&closed[j]<minlen)
{
43
min=j;
44
minlen=closed[j];
45
}
46
used[min]=true;
47
for(j=0;j<dist[min].size();j++)
48
if(!used[dist[min][j].end]&&minlen+dist[min][j].len<closed[dist[min][j].end])
49
closed[dist[min][j].end]=minlen+dist[min][j].len;
50
}
51
printf("%d\n",closed[N]);
52
return 0;
53
}
54![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
55
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
2
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
3
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
4
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
5
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
6
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
7
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
8
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
9
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
10
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
11
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
12
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
13
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
14
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
15
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
16
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
17
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
18
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
19
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
20
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
21
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
22
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
23
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
24
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
25
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
26
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
27
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
28
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
29
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
30
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
31
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
32
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
33
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
34
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
35
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
36
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
37
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
38
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
39
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
40
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
41
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
42
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
43
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
44
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
45
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
46
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
47
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
48
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
49
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
50
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
51
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
52
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
53
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
54
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
55
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
第三次用了priority_queue
1
Source Code
2![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
3
Problem:
2387
User: Torres
4
Memory: 496K Time: 63MS
5
Language: G
++
Result: Accepted
6![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
7
Source Code
8
#include
<
iostream
>
9
#include
<
vector
>
10
#include
<
algorithm
>
11
#include
<
queue
>
12
using
namespace
std;
13![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
14
const
int
Nmax
=
1005
;
15
const
int
oo
=
0x7fffffff
;
16![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
17![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
typedef
struct
node
{
18
int end,len;
19![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
node(int a=0,int b=oo):end(a),len(b)
{};
20![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
bool operator<(const node &a)const
{
21
return this->len>a.len;
22
}
23
}
node;
24![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
25
vector
<
node
>
dist[Nmax];
26![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
27
int
main()
28![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
{
29
int N,M,i,j;
30
int s,e,l;
31
scanf("%d%d",&M,&N);
32![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(i=1;i<=M;i++)
{
33
scanf("%d%d%d",&s,&e,&l);
34
dist[s].push_back(node(e,l));
35
dist[e].push_back(node(s,l));
36
}
37![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
38
vector<int>closed(N+1,oo);
39
vector<bool>used(N+1,false);
40
priority_queue<node>minclosed;
41
int sz=dist[1].size();
42
43![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(i=0;i<sz;i++)
{
44
closed[dist[1][i].end]=dist[1][i].len;
45
minclosed.push(dist[1][i]);
46
}
47
used[1]=true;
48![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
49![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(i=1;i<N;i++)
{
50
while(used[minclosed.top().end])
51
minclosed.pop();
52
node ntemp=minclosed.top();
53![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
if(ntemp.end==N||ntemp.len==oo)
{closed[N]=ntemp.len;break;}
54
minclosed.pop();
55
used[ntemp.end]=true;
56![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
57
sz=dist[ntemp.end].size();
58![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
59![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(j=0;j<sz;j++)
{
60
int en=dist[ntemp.end][j].end;
61
int le=ntemp.len+dist[ntemp.end][j].len;
62![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
if(!used[en]&&le<closed[en])
{
63
closed[en]=le;
64
minclosed.push(node(en,le));
65
}
66
}
67
68
}
69
printf("%d\n",closed[N]);
70
return 0;
71
}
72![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
73
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
2
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
3
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
4
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
5
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
6
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
7
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
8
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
9
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
10
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
11
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
12
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
13
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
14
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
15
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
16
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
17
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
18
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
19
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
20
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
21
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
22
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
23
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
24
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
25
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
26
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
27
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
28
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
29
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
30
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
31
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
32
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
33
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
34
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
35
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
36
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
37
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
38
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
39
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
40
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
41
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
42
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
43
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
44
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
45
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
46
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
47
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
48
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
49
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
50
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
51
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
52
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
53
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
54
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
55
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
56
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
57
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
58
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
59
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
60
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
61
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
62
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
63
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
64
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
65
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
66
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
67
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
68
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
69
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
70
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
71
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
72
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
73
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
第四次用了heap
1
Source Code
2![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
3
Problem:
2387
User: Torres
4
Memory: 496K Time: 63MS
5
Language: G
++
Result: Accepted
6![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
7
Source Code
8
#include
<
iostream
>
9
#include
<
vector
>
10
#include
<
algorithm
>
11
using
namespace
std;
12![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
13
const
int
Nmax
=
1005
;
14
const
int
oo
=
0x7fffffff
;
15![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
16![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
typedef
struct
node
{
17
int end,len;
18![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
node(int a,int b):end(a),len(b)
{};
19![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
bool operator<(const node &a)const
{
20
return this->len>a.len;
21
}
22
}
node;
23![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
24
vector
<
node
>
dist[Nmax];
25![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
26
int
main()
27![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
{
28
int N,M,i,j;
29
int s,e,l;
30
scanf("%d%d",&M,&N);
31![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(i=1;i<=M;i++)
{
32
scanf("%d%d%d",&s,&e,&l);
33
dist[s].push_back(node(e,l));dist[e].push_back(node(s,l));
34
}
35![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
36
vector<int>closed(N+1,oo);
37
vector<bool>used(N+1,false);
38
vector<node>minclosed;
39
int sz=dist[1].size();
40
41![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(i=0;i<sz;i++)
{
42
closed[dist[1][i].end]=dist[1][i].len;
43
minclosed.push_back(dist[1][i]);
44
}
45
used[1]=true;
46
make_heap(minclosed.begin(),minclosed.end());
47![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
48![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(i=1;i<N;i++)
{
49![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
while(used[minclosed.front().end])
{
50
pop_heap(minclosed.begin(),minclosed.end());
51
minclosed.pop_back();
52
}
53
pop_heap(minclosed.begin(),minclosed.end());
54
node ntemp=minclosed.back();
55![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
if(ntemp.end==N||ntemp.len==oo)
{closed[N]=ntemp.len;break;}
56
minclosed.pop_back();
57
used[ntemp.end]=true;
58![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
59
sz=dist[ntemp.end].size();
60![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
61![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
for(j=0;j<sz;j++)
{
62
int en=dist[ntemp.end][j].end;
63
int le=ntemp.len+dist[ntemp.end][j].len;
64![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
if(!used[en]&&le<closed[en])
{
65
closed[en]=le;
66
minclosed.push_back(node(en,le));
67
push_heap(minclosed.begin(),minclosed.end());
68
}
69
}
70
}
71
printf("%d\n",closed[N]);
72
return 0;
73
}
74![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
75
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
2
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
3
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
4
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
5
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
6
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
7
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
8
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
9
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
10
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
11
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
12
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
13
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
14
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
15
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
16
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
17
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
18
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
19
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
20
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
21
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
22
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
23
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
24
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
25
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
26
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
27
![](http://img.e-com-net.com/image/product/44df1c303f404ee184fdd8904c682a7f.gif)
![](http://img.e-com-net.com/image/product/f6242890d71a40ab957c0cf10b55aac8.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
28
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
29
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
30
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
31
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
32
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
33
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
34
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
35
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
36
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
37
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
38
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
39
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
40
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
41
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
42
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
43
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
44
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
45
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
46
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
47
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
48
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
49
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
50
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
51
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
52
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
53
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
54
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
55
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
56
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
57
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
58
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
59
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
60
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
61
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
62
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
63
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
64
![](http://img.e-com-net.com/image/product/16676c75a1f24c2286f9cfe3320175c2.gif)
![](http://img.e-com-net.com/image/product/b5abf8d594ba49098bdc3fa165ae14f7.gif)
![](http://img.e-com-net.com/image/product/4ed387f41cd5427898b953ccc4b441ea.gif)
65
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
66
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
67
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
68
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
69
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
70
![](http://img.e-com-net.com/image/product/4407806dd42e464c873774a6e93bb38f.gif)
71
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
72
![](http://img.e-com-net.com/image/product/58b5517de74d4214832479bcb515bf73.gif)
73
![](http://img.e-com-net.com/image/product/91d69a26e7ce47d4bd97bff91bc3e9d1.gif)
74
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)
75
![](http://img.e-com-net.com/image/product/696412be261e44caa983ccc5cd2a1540.gif)