题解:最短路~
|
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 |
#include <cstdio>#include <utility>#include <cstring>#include <queue>using
namespace std;const
int N=20005;const
int INF=9999999;typedef
pair<int,int>seg;priority_queue<seg,vector,greater >q;int
d[N],u[N],v[N],w[N],head[N],next[N],a,b,c,m,n,k,p,cnt,min;bool
vis[N];void
build(){ memset(head,-1,sizeof(head)); for(int
e=1;e<=m;e++){ scanf("%d%d%d",&u[e],&v[e],&w[e]); n=n<u[e]?u[e]:n; n=n<v[e]?v[e]:n; u[e+m]=v[e]; v[e+m]=u[e]; w[e+m]=w[e]; next[e]=head[u[e]]; head[u[e]]=e; next[e+m]=head[u[e+m]]; head[u[e+m]]=e+m; }}int
Dijkstra(int
src){ memset(vis,0,sizeof(vis)); for(int
i=0;i<=n;i++)d[i]=INF; d[src]=0; q.push(make_pair(d[src],src)); while(!q.empty()){ seg now=q.top(); q.pop(); int
x=now.second; if
(vis[x]) continue; vis[x]=1; for(int
e=head[x];e!=-1;e=next[e]) if(d[v[e]]>d[x]+w[e]){ d[v[e]]=d[x]+w[e]; q.push(make_pair(d[v[e]],v[e])); } }}int
main(){ while(~scanf("%d%d%d",&m,&k,&p)){ build(); for(int
e=2*m+1;e<=2*m+k;e++){ scanf("%d",&v[e]); w[e]=0; u[e]=2*m+k+1; next[e]=head[u[e]]; head[u[e]]=e; } Dijkstra(2*m+k+1); int
min=INF; for(int
i=0;i<p;i++){ scanf("%d",&a); min=d[a]<min?d[a]:min; } printf("%d\n",min); } return
0;} |
HDU 2066 一个人的旅行,布布扣,bubuko.com
原文:http://www.cnblogs.com/forever97/p/3611242.html