• bzoj 2015


    http://www.lydsy.com/JudgeOnline/problem.php?id=2015

    裸最短路(' '     ) 不过我最初以为是mst (' '    ) 

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    
    const int maxn = 100010;
    const int maxe = 100010;
    const int INF = 0x3f3f3f3f;
    
    int n, m, Q;
    
    struct edge {
        int t, d;
        edge* next;
    }e[maxe * 2], *head[maxn]; int ne = 0;
    
    void addedge(int f, int t, int d) {
        e[ne].t = t, e[ne].d = d, e[ne].next = head[f], head[f] = e + ne ++;
    }
    
    struct pr {
        int dis, pos; 
        pr(int a, int b) {
            dis = a, pos = b;
        } 
    };
    
    bool operator < (const pr &a, const pr &b) {
        return a.dis > b.dis;
    }
    
    priority_queue <pr> q;
    int dis[maxn];
    
    void dijkstra(int s) {
        memset(dis, INF, sizeof(dis));
        dis[s] = 0;
        for(int i = 1; i <= n; ++ i) q.push(pr(dis[i], i));
        while(!q.empty()) {
            pr x = q.top(); q.pop();
            if(x.dis != dis[x.pos]) continue;
            for(edge* p = head[x. pos]; p; p = p-> next) {
                if(dis[p-> t] > dis[x. pos] + p-> d)
                    dis[p-> t] = dis[x. pos] + p-> d, q.push(pr(dis[p-> t], p-> t));
            }
        }
    }
    
    int int_get() {
        int x = 0; char c = (char)getchar(); bool f = 0;
        while(!isdigit(c)) {
            if(c == '-') f = 1;
            c = (char)getchar();
        }
        while(isdigit(c)) {
            x = x * 10 + (int)(c - '0');
            c = (char)getchar();
        }
        if(f) x = -x;
        return x;
    }
    
    void read() {
        n = int_get(), m = int_get(); Q = int_get(); 
        for(int i = 1; i <= m; ++ i) {
            int u, v, w;
            u = int_get(), v = int_get(), w = int_get(); 
            addedge(u, v, w); addedge(v, u, w);
        }
    }
    
    void sov() {
        dijkstra(1) ;
        while(Q --) {
            int a, b; 
            a = int_get(), b = int_get(); 
            printf("%d
    ", dis[a] + dis[b]);
        }
    }
    
    int main() {
        //freopen("test.in", "r", stdin);
        read(), sov();
        return 0;
    }
    
  • 相关阅读:
    Spring Boot使用@Scheduled定时器任务
    [TaskList] 省选前板子补完计划
    [模板] 计算几何1(基础): 点/向量/线/圆/多边形/其他运算
    网络流刷题日记
    [模板] 网络流相关/最大流ISAP/费用流zkw
    11/5/2018模拟 Problem C
    11/1/2018模拟 Max
    [模板] 笛卡尔树 && RMQ
    bzoj1010-[HNOI2008]玩具装箱toy
    [模板] 斜率优化
  • 原文地址:https://www.cnblogs.com/ianaesthetic/p/4254702.html
Copyright © 2020-2023  润新知