入门SP,用dij+heap 0ms通过。
View Code
1 #define REP(i, n) for (int i = 0; i < (n); i++) 2 #define REP_1(i, n) for (int i = 1; i <= (n); i++) 3 typedef pair<LL, int> PLI; 4 #define PB push_back 5 #define MPR make_pair 6 #define FI first 7 #define SE second 8 9 VPII rel[N]; 10 LL dis[N]; 11 multiset<PLI> HP; 12 13 void input(int n, int s, int t) { 14 REP(i, 1002) rel[i].clear(), dis[i] = linf; 15 HP.clear(); 16 int a, b, d; 17 REP(i, n) { 18 scanf("%d%d%d", &a, &b, &d); 19 rel[a].PB(MPR(b, d)); 20 rel[b].PB(MPR(a, d)); 21 } 22 REP(i, s) { 23 scanf("%d", &a); 24 rel[0].PB(MPR(a, 0)); 25 } 26 REP(i, t) { 27 scanf("%d", &a); 28 rel[a].PB(MPR(1001, 0)); 29 } 30 // puts("OK!"); 31 } 32 33 LL dijHeap(int s, int t) { 34 dis[s] = 0ll; 35 HP.insert(MPR(0ll, s)); 36 while (true) { 37 int id = HP.begin()->SE; 38 // cout << "size " << SZ(HP) << endl; 39 // cout << id << ' ' << HP.begin()->FI << ' ' << dis[id] << endl; 40 if (id == t) return HP.begin()->FI; 41 HP.erase(HP.begin()); 42 REP(i, SZ(rel[id])) { 43 int x = rel[id][i].FI, d = rel[id][i].SE; 44 if (dis[x] > dis[id] + d) { 45 // cout << x << ' ' << dis[x] << endl; 46 if (dis[x] != linf) HP.erase(MPR(dis[x], x)); 47 HP.insert(MPR(dis[x] = dis[id] + d, x)); 48 } 49 } 50 } 51 } 52 53 int main() { 54 // freopen("in", "r", stdin); 55 int n, s, t; 56 while (~scanf("%d%d%d", &n, &s, &t)) { 57 input(n, s, t); 58 cout << dijHeap(0, 1001) << endl; 59 } 60 return 0; 61 }
——written by Lyon