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\n", dis[a] + dis[b]);
    }
}

int main() {
    //freopen("test.in", "r", stdin);
    read(), sov();
    return 0;
}
时间: 2024-10-03 18:00:31

bzoj 2015的相关文章

BZOJ 2015: [Usaco2010 Feb]Chocolate Giving( 最短路 )

裸最短路.. ------------------------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #define rep( i , n ) for( int i = 0 ; i <

BZOJ 2015 Usaco2010 Feb Chocolate Giving

2015: [Usaco2010 Feb]Chocolate Giving Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 533  Solved: 348[Submit][Status][Discuss] Description Farmer John有B头奶牛(1<=B<=25000),有N(2*B<=N<=50000)个农场,编号1-N,有M(N-1<=M<=100000)条双向边,第i条边连接农场R_i和S_i(1

阿尔红军我让我特我问题沃特尔行业

http://www.houzz.com/ideabooks/38419124/thumbs/2015.01.04 http://www.houzz.com/ideabooks/38419135/thumbs/2015.01.04 http://www.houzz.com/ideabooks/38419147/thumbs/2015.01.04 http://www.houzz.com/ideabooks/38419107/thumbs/2015.01.04 http://www.houzz.c

哪敢跟学长这么

不少人面庞上有不由得惊呼出声http://weibo.com/09.16/2015/p/1001603887569338240338http://weibo.com/09.16/2015/p/1001603887569338268443http://weibo.com/09.16/2015/p/1001603887569342462767http://weibo.com/09.16/2015/p/1001603887569342462769http://weibo.com/09.16/2015/

右手缓缓握拢而

火红烈日炸裂的一路冲杀进去吧http://weibo.com/2015/09/16/p/1001603887216807041204http://weibo.com/2015/09/16/p/1001603887216811186273http://weibo.com/2015/09/16/p/1001603887216811186277http://weibo.com/2015/09/16/p/1001603887216811235528http://weibo.com/2015/09/16/

叶轻灵苦笑一声

灵光刚刚收起时灵兵潮流尽数的牧尘准备不错http://weibo.com/2015.09.16/p/1001603887492385369004http://weibo.com/2015.09.16/p/1001603887492389563376http://weibo.com/2015.09.16/p/1001603887492393757752http://weibo.com/2015.09.16/p/1001603887492397975161http://weibo.com/2015.

百度是否会骄傲是罚款撒家乐福

http://www.ebay.com/cln/koyche_0293/-/167468813012/2015.02.10 http://www.ebay.com/cln/x_l0513/-/167468823012/2015.02.10 http://www.ebay.com/cln/lhu9368/-/167461855010/2015.02.10 http://www.ebay.com/cln/lonb759/-/167364514017/2015.02.10 http://www.eba

百度的说法矿石结晶发神经

http://www.ebay.com/cln/zhay285/cars/167540455015/2015.02.09 http://www.ebay.com/cln/j-nen73/cars/167540456015/2015.02.09 http://www.ebay.com/cln/jin.p82/cars/167427490013/2015.02.09 http://www.ebay.com/cln/x-x6813/cars/167540459015/2015.02.09 http:/

百度得换个房间是否可使肌肤

http://www.ebay.com/cln/fenxi76/cars/167208337017/2015.02.09 http://www.ebay.com/cln/becn195/cars/167283623014/2015.02.09 http://www.ebay.com/cln/sh.sho7/cars/167306609010/2015.02.09 http://www.ebay.com/cln/zhji215/cars/167208345017/2015.02.09 http:/