2 3 3 3 1 3 2 1 2 1 2 3 1 1 3 3 3 3 3 1 3 2 1 2 2 2 3 2 1 3 4
Case #1: 2 Case #2: 3
1 #include <bits/stdc++.h> 2 #define pii pair<int,int> 3 using namespace std; 4 const int INF = 0x3f3f3f3f; 5 const int maxn = 200010; 6 struct arc{ 7 int to,w,next; 8 arc(int x = 0,int y = 0,int z = -1){ 9 to = x; 10 w = y; 11 next = z; 12 } 13 }e[1000000]; 14 int head[maxn],d[maxn],tot,n,m,c; 15 int layer[maxn]; 16 void add(int u,int v,int w){ 17 e[tot] = arc(v,w,head[u]); 18 head[u] = tot++; 19 } 20 bool done[maxn]; 21 priority_queue< pii,vector< pii >,greater< pii > >q; 22 int dijkstra(int s,int t){ 23 while(!q.empty()) q.pop(); 24 memset(d,0x3f,sizeof d); 25 memset(done,false,sizeof done); 26 q.push(pii(d[s] = 0,s)); 27 while(!q.empty()){ 28 int u = q.top().second; 29 q.pop(); 30 if(done[u]) continue; 31 done[u] = true; 32 for(int i = head[u]; ~i; i = e[i].next){ 33 if(d[e[i].to] > d[u] + e[i].w){ 34 d[e[i].to] = d[u] + e[i].w; 35 q.push(pii(d[e[i].to],e[i].to)); 36 } 37 } 38 39 } 40 return d[t] == INF?-1:d[t]; 41 } 42 bool hslv[maxn]; 43 int main(){ 44 int kase,tmp,u,v,w,cs = 1; 45 scanf("%d",&kase); 46 while(kase--){ 47 memset(head,-1,sizeof head); 48 memset(hslv,false,sizeof hslv); 49 tot = 0; 50 scanf("%d%d%d",&n,&m,&c); 51 for(int i = 1; i <= n; ++i){ 52 scanf("%d",&tmp); 53 layer[i] = tmp; 54 hslv[tmp] = true; 55 } 56 for(int i = 0; i < m; ++i){ 57 scanf("%d%d%d",&u,&v,&w); 58 add(u,v,w); 59 add(v,u,w); 60 } 61 for(int i = 1; i <= n; ++i){ 62 add(layer[i]+n,i,0); 63 if(layer[i] > 1) add(i,layer[i]-1+n,c); 64 if(layer[i] < n) add(i,layer[i]+n+1,c); 65 } 66 printf("Case #%d: %d\n",cs++,dijkstra(1,n)); 67 } 68 return 0; 69 }