zoukankan      html  css  js  c++  java
  • POJ 3255_Roadblocks

    题意:

    无向图,求单源次短路,每条边可以走多次。

    分析:

    对于每个点记录最短路和次短路,次短路可以是由最短路+边或者是次短路+边更新而来。在更新每个点的最短路时,相应更新次短路,如果最短路更新了,就令次短路等于原来的最短路,如果没有,就看能否直接更新次短路。

    代码:

    #include<iostream>
    #include<queue>
    #include<cstdio>
    using namespace std;
    #define se second
    #define fi first
    typedef pair<int, int>pii;
    struct edge{int to,w,next;};
    const int maxm = 200010, maxn = 5005, INF = 0x3fffffff;
    edge e[maxm];
    int n,R;
    int head[maxn], d[maxn], d2[maxn];
    int dijkstra()
    {
        priority_queue<pii,vector<pii>,greater<pii> > q;
        q.push(pii(0,1));
        fill(d, d+n+1, INF);
        fill(d2, d2+n+1, INF);
        d[1] = 0;
        while(!q.empty()){
            pii tmp = q.top();q.pop();
            int ne = head[tmp.se];
            while(ne!=-1){
                int v = e[ne].to;
                int di = e[ne].w+tmp.fi;
                int temp =di;
                if(d[v] > di){
                    temp = d[v];
                     d[v]=di;
                     q.push(make_pair(d[v],v));
                }
                if(d2[v] > temp){
                    d2[v] = temp;
                    q.push(make_pair(d2[v],v));
                }
                ne = e[ne].next;
            }
        }
        return d2[n];
    }
    int main (void)
    {
        scanf("%d%d",&n,&R);
        int a, b, di;
        fill(head, head+n+1,-1);
        for(int i = 0; i < 2 * R; i++){
           scanf("%d%d%d",&a,&b,&di);
            e[i].to = b, e[i].w = di;
            e[i].next = head[a];
            head[a] = i++;
            e[i].to = a, e[i].w = di;
            e[i].next = head[b];
            head[b] = i;
        }
        printf("%d
    ",dijkstra());
    }
    

    看discuss有人说是道大水题,可是我感觉还不是很好想,可能还是对算法本身理解不彻底掌握不扎实吧。

  • 相关阅读:
    服务器时间同步
    DataX部署安装
    Mysql ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction 解决方法
    mysql 使用需要注意的问题
    利用mysqldump 将一个表按条件导出数据
    mysql存储引擎分类
    MySQL数据备份之mysqldump使用
    count(1)、count(*)与count(列名)的执行区别
    rsync + sersync 实现实时数据同步
    ipmitool 工具使用
  • 原文地址:https://www.cnblogs.com/Tuesdayzz/p/5758804.html
Copyright © 2011-2022 走看看