• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LyonLys
愿意在角落唱沙哑的歌 再大声也都是给你 请用心听 不要说话 Contact me via E-mail: lyon.lys@gmail.com
博客园    首页    新随笔    联系   管理    订阅  订阅

hdu 2066 一个人的旅行 (dij+heap)

Problem - 2066

  入门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

posted @ 2013-03-11 10:43  LyonLys  阅读(165)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3