BZOJ1579 USACO 2009 Feb Gold 3.Revamping Trails Solution

BZOJ1579 USACO 2009 Feb Gold 3.Revamping Trails Solution

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

标题效果:一个N积分m无向图边。它可以是路径k右边缘值变0,确定此时1-n最短路径长度。

Sol:我以为我们考虑分层图,图复制k+1部分,每间0~k一层。代表在这个时候已经过去“自由边缘”文章编号。

层与层之间的边权值为0且为单向由上层指向下层。

这样我们以0层的1点做单源最短路径。每一层的n点的距离最小值即为答案。

仅仅只是这种点数为O(K*N),边数为O(K*M),比較慢。

我的做法是,对每一层使用heap-dijkstra算法由本层的原点更新这一层的最短路长度。然后显然能够用O(m)的复杂度推知下一层的初始最短路长度。

这样的做法显然空间和时间上均存在较大优势。

Code:

#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
 
inline int getc() {
    static const int L = 1 << 15;
    static char buf[L], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, L, stdin);
        if (S == T)
            return EOF;
    }
    return *S++;
}
inline int getint() {
    int c;
    while(!isdigit(c = getc()));
    int tmp = c - '0';
    while(isdigit(c = getc()))
        tmp = (tmp << 1) + (tmp << 3) + c - '0';
    return tmp;
}
 
typedef long long LL;
 
#define N 10010
#define M 50010
int n, m, k;
int head[N], next[M << 1], end[M << 1], len[M << 1];
LL dis[2][N];
bool inpath[N];
 
queue<int> q;
 
void addedge(int a, int b, int _len) {
    static int q = 1;
    len[q] = _len;
    end[q] = b;
    next[q] = head[a];
    head[a] = q++;
}
void make(int a, int b, int _len) {
    addedge(a, b, _len);
    addedge(b, a, _len);
}
 
struct Node {
    int lab, dis;
    Node(int _lab = 0, int _dis = 0):lab(_lab),dis(_dis){}
    bool operator < (const Node &B) const {
        return (dis < B.dis) || (dis == B.dis && lab < B.lab);
    }
};
struct Heap {
    Node a[N];
    int top, ch[N];
    Heap():top(0){}
    void up(int x) {
        for(; x != 1; x >>= 1) {
            if (a[x] < a[x >> 1]) {
                swap(ch[a[x].lab], ch[a[x >> 1].lab]);
                swap(a[x], a[x >> 1]);
            }
            else
                break;
        }
    }
    void down(int x) {
        int son;
        for(; x << 1 <= top; ) {
            son=(((x<<1)==top)||(a[x<<1]<a[(x<<1)|1]))?(x<<1):((x<<1)|1);
            if (a[son] < a[x]) {
                swap(ch[a[son].lab], ch[a[x].lab]);
                swap(a[son], a[x]);
                x = son;
            }
            else
                break;
        }
    }
    void insert(Node x) {
        a[++top] = x;
        ch[x.lab] = top;
        up(top);
    }
    Node Min() {
        return a[1];
    }
    void pop() {
        a[1] = a[top];
        ch[a[top--].lab] = 1;
        down(1);
    }
    void change(int x, int to) {
        int ins = ch[x];
        a[ins].dis = to;
        up(ins);
    }
}H;
 
void Dijkstra(bool d) {
    H.top = 0;
    int i, j;
    memset(inpath, 0, sizeof(inpath));
    for(i = 1; i <= n; ++i)
        H.insert(Node(i, dis[d][i]));
    for(i = 1; i <= n; ++i) {
        Node tmp = H.Min();
        H.pop();
        inpath[tmp.lab] = 1;
        for(j = head[tmp.lab]; j; j = next[j]) {
            if (!inpath[end[j]] && dis[d][end[j]] > dis[d][tmp.lab] + len[j]) {
                dis[d][end[j]] = dis[d][tmp.lab] + len[j];
                H.change(end[j], dis[d][end[j]]);
            }
        }
    }
}
 
int main() {
    n = getint();
    m = getint();
    k = getint();
     
    int i, j;
    int a, b, x;
    for(i = 1; i <= m; ++i) {
        a = getint();
        b = getint();
        x = getint();
        make(a, b, x);
    }
     
    int now = 0, last = 1;
     
    memset(dis, 0x3f, sizeof(dis));
    dis[now][1] = 0;
    Dijkstra(now);
    LL ans = dis[now][n];
     
    while(k--) {
        now ^= 1;
        last ^= 1;
        for(i = 1; i <= n; ++i)
            dis[now][i] = dis[last][i];
        for(i = 1; i <= n; ++i)
            for(j = head[i]; j; j = next[j])
                dis[now][end[j]] = min(dis[now][end[j]], dis[last][i]);
        Dijkstra(now);
        if (ans == dis[now][n])
            break;
        ans = dis[now][n];
    }
     
    printf("%lld", ans);
     
    return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117081.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Dedecms自定义表单后台列表展现方式更改

    Dedecms自定义表单后台列表展现方式更改

  • 小程序 轮播图

    小程序 轮播图轮播图:cssswiper{height:400rpx;}swiper-itemimage{width:100%;height:100%;}.swiper-container{position:relative;margin-top:-300rpx;}.swiper-container.swiper{height:300r…

  • 一些免费的代理服务器「建议收藏」

    一些免费的代理服务器「建议收藏」http://www.cnproxy.com/proxy1.html12.24.45.100:80   24.25.26.82:8024.25.26.128:8024.25.26.131:8024.25.26.136:8024.29.138.66:8024.119.115.228:8062.41.85.113:8063.236.6.200:8064.26.

  • 阻止mouseover冒泡行为_onmousedown是什么意思

    阻止mouseover冒泡行为_onmousedown是什么意思一.onmouseenter、onmouseoveronmouseenter事件在鼠标指针进入到绑定事件的那个元素上时触发。该事件通常与onmouseleave(在鼠标指针离开绑定事件的那个元素上时触发)事件一同使用。onmouseenter事件类似于onmouseover事件。唯一的区别是onmouseenter事件不支持冒泡。二.实例演示onmousemov…

  • zookeeper入门教程_dubbo和Zookeeper详解

    zookeeper入门教程_dubbo和Zookeeper详解zookeeperwatcher架构zookeeper 配置中心分布式ID分布式锁集群搭建数据一致性协议:zab协议Zookeeper Leader选举Observer角色及其配置watcher架构客户端首先将Watcher注册到服务器,同时将Watch对象保存到客户端的Watch管理器中。当Zookeeper服务器监听到的数据发生变化时,服务器会通知客户端,接着客户端的Watch管理器会触发相关的Watcher来回调响应处理逻辑,从而完成整体的数据发布/订阅流程。javaAPIJava

  • 更新kali源「建议收藏」

    更新kali源「建议收藏」新安装的kali系统,在进行软件下载升级的时候会使用kali官方源去下载,在国内访问会比较慢,更换为国内源后,会提升下载速度。1、打开kali源文件sudovim/etc/apt/sources.list

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号