Java教程

洛谷 Problem P1629 - 邮递员送信

本文主要是介绍洛谷 Problem P1629 - 邮递员送信,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

洛谷 Problem P1629 - 邮递员送信

原题地址

题目类型:最短路径、Dijkstra
题意:

在一个有向图中,给定一个起点,对于其余的每个点进行从起点走到该点再返回起点的操作,求最终经过的总路程。

分析:

首先可以想到用 Dijkstra 算法求出从起点到各个点的最短距离,但是到达每个点后还要返回,这个路径应该也是最
短的,但因为是有向图我们无法直接求得。但是可以重新反向建图,即将原来的边反向连,然后再用 Dijkstra 算法来求最短路。此时求得的最短路就是所有点走向起点的最短路径。

代码
static int[] head1;
static int[] head2;
static Edge[] edge1;
static Edge[] edge2;
static int idx1;
static int idx2;
public static void solve() throws IOException {
    int n = nextInt();
    int m = nextInt();
    
    init(n, m);
    
    for (int i = 0; i < m; i++) {
        int from = nextInt();
        int to = nextInt();
        int dis = nextInt();
        addEdge1(new Edge(from, to, dis));
        addEdge2(new Edge(to, from, dis));
    }
    
    int[] dis1 = dijkstra1(1, n, m);
    int[] dis2 = dijkstra2(1, n, m);
    long ans = 0;
    for (int i = 2; i <= n; i++) ans += dis1[i] + dis2[i]; 
    
    pw.println(ans);
} 



private static int[] dijkstra1(int start, int n, int m) {
    // TODO Auto-generated method stub
    int[] dis = new int[n + 1];
    boolean[] vis = new boolean[n + 1];
    Arrays.fill(dis, INF);
    dis[start] = 0;
    
    PriorityQueue<Vertex> q = new PriorityQueue<>(new Comparator<Vertex>() {
        @Override
        public int compare(Main.Vertex o1, Main.Vertex o2) {
            // TODO Auto-generated method stub
            return o1.dis - o2.dis;
        }
    });
    
    q.add(new Vertex(start, 0));
    
    while (!q.isEmpty()) {
        Vertex u = q.poll();
        
        if (vis[u.ver]) continue ;
        vis[u.ver] = true;
        
        for (int i = head1[u.ver]; i != -1; i = edge1[i].next) {
            Edge e = edge1[i];
            if (dis[e.to] > dis[u.ver] + e.dis) {
                dis[e.to] = dis[u.ver] + e.dis;
                q.add(new Vertex(e.to, dis[e.to]));
            }
        }
    }
    
    
    return dis;
}

private static int[] dijkstra2(int start, int n, int m) {
    int[] dis = new int[n + 1];
    boolean[] vis = new boolean[n + 1];
    Arrays.fill(dis, INF);
    dis[start] = 0;
    
    PriorityQueue<Vertex> q = new PriorityQueue<>(new Comparator<Vertex>() {
        @Override
        public int compare(Main.Vertex o1, Main.Vertex o2) {
            return o1.dis - o2.dis;
        }
    });
    
    q.add(new Vertex(start, 0));
    
    while (!q.isEmpty()) {
        Vertex u = q.poll();
        
        if (vis[u.ver]) continue ;
        vis[u.ver] = true;
        
        for (int i = head2[u.ver]; i != -1; i = edge2[i].next) {
            Edge e = edge2[i];
            if (dis[e.to] > dis[u.ver] + e.dis) {
                dis[e.to] = dis[u.ver] + e.dis;
                q.add(new Vertex(e.to, dis[e.to]));
            }
        }
    }
    
    
    return dis;
}


private static void addEdge1(Edge e) {
    edge1[idx1] = e;
    e.next = head1[e.from];
    head1[e.from] = idx1++;
}
private static void addEdge2(Edge e) {
    edge2[idx2] = e;
    e.next = head2[e.from];
    head2[e.from] = idx2++;
}

private static void init(int n, int m) {
    head1 = new int[n + 1];
    head2 = new int[n + 1];
    edge1 = new Edge[m];
    edge2 = new Edge[m];
    Arrays.fill(head1, -1);
    Arrays.fill(head2, -1);
    idx1 = 0;
    idx2 = 0;
}

/******************************************************************************************/

// Fast I/O

static class Edge {
    int from;
    int to;
    int dis;
    int next;
    
    public Edge(int from, int to, int dis) {
        this.from = from;
        this.to = to;
        this.dis = dis;
    }
}

static class Vertex {
    int ver;
    int dis;
    
    public Vertex(int ver, int dis) {
        this.ver = ver;
        this.dis = dis;
    }
}

这篇关于洛谷 Problem P1629 - 邮递员送信的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!