码迷,mamicode.com
首页 > 其他好文 > 详细

Poj 1151 Invitation Cards dijkstra 堆优化

时间:2014-07-22 22:54:33      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   for   re   c   

很裸的最短路,不过节点数和边数都是1e6,直接dij肯定是不行了,稀疏图用heap优化一下就好

o(╯□╰)o注意STL里面的优先队列是优先级大的(值大的)在前面的,一开始没注意WA了好几发,哎,太粗心了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef long long LL;
const int maxn = 1000000 + 5;
const LL INF = 10000000005LL;
int u[maxn],v[maxn],w[maxn];
long long d[maxn];
int first[maxn],nxt[maxn];
bool vis[maxn];
int n,m;

void add(int _u,int id) {
    nxt[id] = first[_u];
    first[_u] = id;
}

struct Node {
    long long a;
    int b;
    Node(long long a,int b):a(a),b(b) {}
    bool operator < (const Node &x) const {
        return a > x.a;
    }
};

//优先队列优化的dijkstra
void dijkstra(int *v) {
    memset(vis,0,sizeof(vis));
    for(int i = 1;i <= n;i++) d[i] = INF;
    d[1] = 0;
    priority_queue<Node> q;
    q.push(Node(d[1],1));
    while(!q.empty()) {
        Node now = q.top(); q.pop();
        int x = now.b;
        if(vis[x]) continue;
        vis[x] = true;
        for(int i = first[x];i != 0;i = nxt[i]) {
            if(d[v[i]] > d[x] + w[i]) {
                d[v[i]] = d[x] + w[i];
                q.push(Node(d[v[i]],v[i]));
            }
        }
    }
}

int main() {
    int T; scanf("%d",&T);
    for(int kase = 1;kase <= T;kase++) {
        memset(first,0,sizeof(first));
        memset(nxt,0,sizeof(nxt));;
        long long ans = 0;
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= m;i++) {
            scanf("%d%d%d",&u[i],&v[i],&w[i]);
            add(u[i],i);
        }
        dijkstra(v);
        for(int i = 1;i <= n;i++) ans += d[i];
        memset(first,0,sizeof(first));
        memset(nxt,0,sizeof(nxt));
        for(int i = 1;i <= m;i++) {
            add(v[i],i);
        }
        dijkstra(u);
        for(int i = 1;i <= n;i++) ans += d[i];
        cout << ans << endl;
    }
    return 0;
}

  

Poj 1151 Invitation Cards dijkstra 堆优化,布布扣,bubuko.com

Poj 1151 Invitation Cards dijkstra 堆优化

标签:blog   os   io   for   re   c   

原文地址:http://www.cnblogs.com/rolight/p/3849131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!