节点编号是字母的最短路

44 篇文章 1 订阅
33 篇文章 0 订阅
文章提供了两个Java程序,分别使用Dijkstra算法和Bellman-Ford算法计算字符间的最短路径。Dijkstra算法通过字符映射到数字并构建图,初始化矩阵后找到最短路;Bellman-Ford算法则直接在字符间添加边,通过迭代更新距离。两个程序均以A为起点,计算到S的最短距离。
摘要由CSDN通过智能技术生成

AC代码1:(Dijkstra,字符映射成数字,跑一遍最短路,简简单单)

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)200;
    static Map<Character,Integer> map = new HashMap<>();
    static int g[][] = new int[N][N];
    static int dist[] = new int [N];
    static boolean visited[] = new boolean[N];

    // A~Z映射成1~26
    static void reflect()
    {
        String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char str[] = s.toCharArray();

        for(int i = 0 ; i < str.length ; i ++)  map.put(str[i],i + 1);
    }

    // 矩阵的初始化
    static void init()
    {
        g[map.get('A')][map.get('B')] = 2;
        g[map.get('A')][map.get('C')] = 1;
        g[map.get('A')][map.get('D')] = 1;
        g[map.get('A')][map.get('E')] = 1;

        g[map.get('B')][map.get('A')] = 2;
        g[map.get('B')][map.get('G')] = 1;
        g[map.get('B')][map.get('J')] = 2;

        g[map.get('C')][map.get('A')] = 1;
        g[map.get('C')][map.get('D')] = 3;
        g[map.get('C')][map.get('G')] = 3;
        g[map.get('C')][map.get('F')] = 3;

        g[map.get('D')][map.get('C')] = 3;
        g[map.get('D')][map.get('A')] = 1;
        g[map.get('D')][map.get('E')] = 1;
        g[map.get('D')][map.get('I')] = 2;
        g[map.get('D')][map.get('H')] = 1;
        g[map.get('D')][map.get('G')] = 2;

        g[map.get('E')][map.get('A')] = 1;
        g[map.get('E')][map.get('D')] = 1;
        g[map.get('E')][map.get('H')] = 1;
        g[map.get('E')][map.get('I')] = 3;

        g[map.get('F')][map.get('C')] = 3;
        g[map.get('F')][map.get('J')] = 1;
        g[map.get('F')][map.get('G')] = 1;

        g[map.get('G')][map.get('F')] = 1;
        g[map.get('G')][map.get('B')] = 1;
        g[map.get('G')][map.get('C')] = 3;
        g[map.get('G')][map.get('D')] = 2;
        g[map.get('G')][map.get('I')] = 3;
        g[map.get('G')][map.get('K')] = 2;

        g[map.get('H')][map.get('D')] = 1;
        g[map.get('H')][map.get('E')] = 1;
        g[map.get('H')][map.get('I')] = 1;
        g[map.get('H')][map.get('L')] = 2;

        g[map.get('I')][map.get('M')] = 3;
        g[map.get('I')][map.get('G')] = 3;
        g[map.get('I')][map.get('H')] = 1;
        g[map.get('I')][map.get('D')] = 2;
        g[map.get('I')][map.get('E')] = 3;

        g[map.get('J')][map.get('B')] = 2;
        g[map.get('J')][map.get('F')] = 1;
        g[map.get('J')][map.get('S')] = 2;

        g[map.get('K')][map.get('G')] = 2;
        g[map.get('K')][map.get('L')] = 3;
        g[map.get('K')][map.get('P')] = 2;
        g[map.get('K')][map.get('N')] = 1;

        g[map.get('L')][map.get('H')] = 2;
        g[map.get('L')][map.get('M')] = 1;
        g[map.get('L')][map.get('R')] = 1;
        g[map.get('L')][map.get('K')] = 3;

        g[map.get('M')][map.get('S')] = 1;
        g[map.get('M')][map.get('Q')] = 1;
        g[map.get('M')][map.get('N')] = 2;
        g[map.get('M')][map.get('L')] = 1;
        g[map.get('M')][map.get('I')] = 3;

        g[map.get('N')][map.get('K')] = 1;
        g[map.get('N')][map.get('M')] = 2;
        g[map.get('N')][map.get('P')] = 1;

        g[map.get('O')][map.get('Q')] = 1;
        g[map.get('O')][map.get('R')] = 3;
        g[map.get('O')][map.get('P')] = 1;

        g[map.get('P')][map.get('N')] = 1;
        g[map.get('P')][map.get('K')] = 2;
        g[map.get('P')][map.get('O')] = 1;

        g[map.get('Q')][map.get('O')] = 1;
        g[map.get('Q')][map.get('M')] = 1;

        g[map.get('R')][map.get('O')] = 3;
        g[map.get('R')][map.get('L')] = 1;
        g[map.get('R')][map.get('S')] = 1;

        g[map.get('S')][map.get('R')] = 1;
        g[map.get('S')][map.get('J')] = 2;
        g[map.get('S')][map.get('M')] = 1;
    }

    static int dijstra()
    {
        Arrays.fill(dist,Integer.MAX_VALUE >> 1);

        dist[map.get('A')] = 0;

        for(int i = 1 ; i <= 19 ; i ++)
        {
            int t = -1;
            for(int j = 1 ; j <= 19 ; j ++)
            {
                if(!visited[j] && (t == -1 || dist[j] < dist[t]) )  t = j;
            }

            visited[t] = true;

            for(int j = 1 ; j <= 19 ; j ++)  dist[j] = Math.min(dist[j],dist[t] + g[t][j]);
        }

        if(dist[19] == Integer.MAX_VALUE >> 1)  return -1;
        return dist[19];
    }

    public static void main(String[] args ) throws IOException
    {
        for(int i = 1 ; i < N ; i ++)  Arrays.fill(g[i],Integer.MAX_VALUE >> 1);
        reflect();
        init();

        pw.println(dijstra());
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException  { return reader.readLine(); }

    static String next() throws IOException
    {
        while (!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException  { return Integer.parseInt(next()); }

    static double nextDouble() throws IOException { return Double.parseDouble(next()); }

    static long nextLong() throws IOException  { return Long.parseLong(next());}

    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
}

AC代码2:(Bellman_ford,直接粗暴加边,不需要映射)

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)1e3 + 10;
    static int INF = 0x3f3f3f3f;
    static math math_me = new math();
    static List<int []> edges = new ArrayList<>();
    static int dist[] = new int[N];

    // a到b加一条边,权重为w
    static void add(char a, char b, int w)
    {
        edges.add(new int[] {a, b, w});
        edges.add(new int[] {b, a, w});
    }

    public static void main(String[] args ) throws IOException
    {
        add('A', 'C', 1);
        add('A', 'D', 1);
        add('A', 'E', 1);
        add('D', 'E', 1);
        add('E', 'I', 1);
        add('D', 'H', 1);
        add('H', 'I', 1);
        add('B', 'G', 1);
        add('F', 'G', 1);
        add('F', 'J', 1);
        add('K', 'N', 1);
        add('L', 'M', 1);
        add('N', 'P', 1);
        add('P', 'O', 1);
        add('O', 'Q', 1);
        add('Q', 'M', 1);
        add('L', 'R', 1);
        add('S', 'R', 1);
        add('M', 'S', 1);

        add('A', 'B', 2);
        add('B', 'J', 2);
        add('D', 'I', 2);
        add('D', 'G', 2);
        add('G', 'K', 2);
        add('K', 'P', 2);
        add('J', 'S', 2);
        add('M', 'N', 2);
        add('H', 'L', 2);

        add('E', 'I', 3);
        add('I', 'M', 3);
        add('G', 'I', 3);
        add('C', 'D', 3);
        add('C', 'G', 3);
        add('C', 'F', 3);
        add('O', 'R', 3);
        add('K', 'L', 3);

        int n = edges.size(); // 边的总数

        Arrays.fill(dist,Integer.MAX_VALUE >> 1); // 除以2是为了防止溢出

        dist['A'] = 0; // 谁是起点,就把谁的dist(距起点的距离)设置为0,然后利用它来更新邻边,然后邻边可以再更新新的邻边,完美,阿琛yyds

        for(int i = 1 ; i <= n - 1 ; i ++) // 迭代(边数-1)次
        {
            for(int[] edge: edges)  // 遍历边的集合中每一条边
            {
                int a = edge[0],b = edge[1],w = edge[2];
                dist[b] = Math.min(dist[b],dist[a] + w); // 更新可以更新的边
            }
        }
        pw.println(dist['S']);
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class math
{
    int gcd(int a,int b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }

    int lcm(int a,int b)
    {
        return a * b / gcd(a, b);
    }

    // 求n的所有约数
    List get_factor(int n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }

        // 相同因子去重,这个方法,完美
        a = a.stream().distinct().collect(Collectors.toList());

        // 对因子排序(升序)
        Collections.sort(a);

        return a;
    }

    // 判断是否是质数
    boolean check_isPrime(int n)
    {
        if(n < 2) return false;
        for(int i = 2 ; i <= n / i; i ++)  if (n % i == 0) return false;
        return true;
    }
}

class PII implements Comparable<PII>
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.x-a.x != 0)
            return this.x-a.x;  //按x升序排序
        else return this.y-a.y;  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

class Line implements Comparable<Line>
{
    double k; // 斜率
    double b; // 截距

    public Line(double k, double b)
    {
        this.k = k;
        this.b = b;
    }

    @Override
    public int compareTo(Line o)
    {
        if (this.k > o.k) return 1;
        if (this.k == o.k)
        {
            if (this.b > o.b) return 1;
            return -1;
        }
        return -1;
    }
}

class mqm
{
    int fa[] = new int[1005];
    void init()
    {
        for(int i = 1 ; i <= 1000 ; i ++)  fa[i] = i;
    }
    void merge(int x, int y) { fa[find(x)] = find(y); }
    int find(int x)
    {
        if(x != fa[x])  fa[x] = find(fa[x]);
        return fa[x];
    }
    boolean query(int x, int y) { return find(x) == find(y); }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

21RGHLY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值