• 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;
    }
    
  • 相关阅读:
    MySQL安装失败,提示需安装MicroSoft Visual C++ 2013 Redistributable
    Selinium登录系统cookies的重复使用
    脚本绕开验证码,自动执行的方法
    Firebug显示停用状态
    web自动化测试中绕开验证码登陆的方式
    java使用poi包将数据写入Excel表格
    读取config配置
    定位元素的等待方法
    jxl读取Excel表格数据
    php中的魔术常量
  • 原文地址:https://www.cnblogs.com/ianaesthetic/p/4254702.html
Copyright © 2020-2023  润新知