Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27564 Accepted Submission(s): 9564
1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <queue> 5 #include <algorithm> 6 using namespace std; 7 8 const int maxn = 1050; 9 const int INF = 0x7ffffff; 10 11 struct Edge { 12 int from, to, dist; 13 Edge(int u, int v, int d) :from(u), to(v), dist(d) {} 14 }; 15 16 struct HeapNode { 17 int d, u; 18 HeapNode(int dv, int uv) :d(dv), u(uv){} 19 bool operator < (const HeapNode& rhs) const { 20 return d > rhs.d; 21 } 22 }; 23 struct Dijkstra { 24 int n, m; 25 Dijkstra(){}; 26 Dijkstra(int nv, int mv) :n(nv), m(mv){} 27 vector<Edge> edges; 28 vector<int> G[maxn]; 29 bool done[maxn]; //是否已永久标号 30 int d[maxn]; //s到各个点的距离 31 //int p[maxn]; //最短路中的上一条弧 32 void init(int n) { 33 this->n = n; 34 for (int i = 0; i < n; i++) G[i].clear(); 35 edges.clear(); 36 } 37 void AddEdge(Edge e) { 38 edges.push_back(e); 39 m = edges.size(); 40 G[e.from].push_back(m - 1); 41 } 42 void dijkstra(int s) { 43 priority_queue<HeapNode> Q; 44 for (int i = 0; i < n; i++) d[i] = INF; 45 d[s] = 0; 46 memset(done, 0, sizeof(done)); 47 Q.push(HeapNode(0, s)); 48 while (!Q.empty()) { 49 HeapNode x = Q.top(); Q.pop(); 50 int u = x.u; 51 if (done[u]) continue; 52 done[u] = true; 53 for (int i = 0; i < G[u].size(); i++) { 54 Edge& e = edges[G[u][i]]; 55 if (d[e.to] > d[u] + e.dist) { 56 d[e.to] = d[u] + e.dist; 57 //p[e.to] = G[u][i]; 58 Q.push(HeapNode(d[e.to],e.to)); 59 } 60 } 61 } 62 } 63 }; 64 65 int ad[maxn]; 66 int wt[maxn]; 67 int main() 68 { 69 int T,S,D,t, a, b, r,ans,temp; 70 Dijkstra d; 71 while (scanf("%d%d%d", &T, &S, &D) == 3) 72 { 73 ans = INF; 74 d.init(1005); 75 for(int i=0;i<T;i++) 76 { 77 scanf("%d%d%d",&a,&b,&t); 78 d.AddEdge(Edge(a-1,b-1,t)); 79 d.AddEdge(Edge(b-1,a-1,t)); 80 } 81 for(int i=0;i<S;i++) scanf("%d",&ad[i]); 82 for(int i=0;i<D;i++) scanf("%d",&wt[i]); 83 for(int i=0;i<S;i++) 84 { 85 d.dijkstra(ad[i]-1); 86 for(int j=0;j<D;j++) 87 { 88 if(ans>d.d[wt[j]-1]) ans=d.d[wt[j]-1]; 89 } 90 } 91 printf("%d\n",ans); 92 } 93 }