建立一个超级源点0和超级汇点n,求0到n的最短路就行了。
dijstra:
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int INF = 9999999; 8 const int N = 2001; 9 const int M = 100000; 10 int head[N]; 11 int dist[N]; 12 bool visit[N]; 13 int n, e; 14 15 struct Edge 16 { 17 int v, next, w; 18 } edge[M]; 19 20 void addEdge( int u, int v, int w ) 21 { 22 edge[e].v = v; 23 edge[e].w = w; 24 edge[e].next = head[u]; 25 head[u] = e++; 26 } 27 28 void dij( int s ) 29 { 30 memset( visit, false, sizeof(visit) ); 31 for ( int i = 0; i <= n; i++ ) 32 { 33 dist[i] = INF; 34 } 35 dist[s] = 0; 36 for ( int i = 0; i <= n; i++ ) 37 { 38 int minn = INF, u; 39 for ( int j = 0; j <= n; j++ ) 40 { 41 if ( !visit[j] && dist[j] < minn ) 42 { 43 minn = dist[j]; 44 u = j; 45 } 46 } 47 visit[u] = true; 48 for ( int j = head[u]; j != -1; j = edge[j].next ) 49 { 50 int v = edge[j].v, w = edge[j].w; 51 if ( !visit[v] && dist[u] + w < dist[v] ) 52 { 53 dist[v] = dist[u] + w; 54 } 55 } 56 } 57 } 58 59 int main () 60 { 61 int t, s, d; 62 while ( scanf("%d%d%d", &t, &s, &d) != EOF ) 63 { 64 e = n = 0; 65 memset( head, -1, sizeof(head) ); 66 while ( t-- ) 67 { 68 int u, v, w; 69 scanf("%d%d%d", &u, &v, &w); 70 addEdge( u, v, w ); 71 addEdge( v, u, w ); 72 if ( u > n ) n = u; 73 if ( v > n ) n = v; 74 } 75 n++; 76 for ( int i = 0; i < s; i++ ) 77 { 78 int tmp; 79 scanf("%d", &tmp); 80 addEdge( 0, tmp, 0 ); 81 } 82 for ( int i = 0; i < d; i++ ) 83 { 84 int tmp; 85 scanf("%d", &tmp); 86 addEdge( tmp, n, 0 ); 87 } 88 dij(0); 89 printf("%d ", dist[n]); 90 } 91 return 0; 92 }
spfa:
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int INF = 9999999; 8 const int N = 2001; 9 const int M = 100000; 10 int head[N]; 11 int dist[N]; 12 bool visit[N]; 13 int n, e; 14 15 struct Edge 16 { 17 int v, next, w; 18 } edge[M]; 19 20 void addEdge( int u, int v, int w ) 21 { 22 edge[e].v = v; 23 edge[e].w = w; 24 edge[e].next = head[u]; 25 head[u] = e++; 26 } 27 28 void spfa( int s ) 29 { 30 for ( int i = 0; i <= n; i++ ) 31 { 32 dist[i] = INF; 33 visit[i] = false; 34 } 35 dist[s] = 0; 36 visit[s] = true; 37 int q[N], top = 0; 38 q[top] = s; 39 top = ( top + 1 ) % ( n + 1 ); 40 for ( int i = 0; i != top; i = ( i + 1 ) % ( n + 1 ) ) 41 { 42 int u = q[i]; 43 visit[u] = false; 44 for ( int j = head[u]; j != -1; j = edge[j].next ) 45 { 46 int v = edge[j].v, w = edge[j].w; 47 if ( dist[v] > dist[u] + w ) 48 { 49 dist[v] = dist[u] + w; 50 if ( !visit[v] ) 51 { 52 q[top] = v; 53 top = ( top + 1 ) % ( n + 1 ); 54 visit[v] = true; 55 } 56 } 57 } 58 } 59 } 60 61 int main () 62 { 63 int t, s, d; 64 while ( scanf("%d%d%d", &t, &s, &d) != EOF ) 65 { 66 e = n = 0; 67 memset( head, -1, sizeof(head) ); 68 while ( t-- ) 69 { 70 int u, v, w; 71 scanf("%d%d%d", &u, &v, &w); 72 addEdge( u, v, w ); 73 addEdge( v, u, w ); 74 if ( u > n ) n = u; 75 if ( v > n ) n = v; 76 } 77 n++; 78 for ( int i = 0; i < s; i++ ) 79 { 80 int tmp; 81 scanf("%d", &tmp); 82 addEdge( 0, tmp, 0 ); 83 } 84 for ( int i = 0; i < d; i++ ) 85 { 86 int tmp; 87 scanf("%d", &tmp); 88 addEdge( tmp, n, 0 ); 89 } 90 spfa(0); 91 printf("%d ", dist[n]); 92 } 93 return 0; 94 }