【算法】图论-最短路

最短路算法

alt

dijkstral

import java.util.*;

public class Main{
    static int N = 510;
    static int[] dist = new int[N];
    static int[][] g = new int[N][N];
    static boolean[] st = new boolean[N];
    static int n,m;
    
    public static int dijkstra(){
        Arrays.fill(dist,0x3f3f3f);
        dist[1] = 0;
        for(int i = 0; i < n; i ++){
            int t = -1;
            for(int j = 1; j <= n; j ++ ){
                if(!st[j] && (t == -1 || dist[t] > dist[j])){
                    t = j;
                }
            }
            st[t] = true;
            for(int j = 1; j <= n; j ++){
                dist[j] = Math.min(dist[j],g[t][j] + dist[t]);
            }
        }
        if(dist[n] == 0x3f3f3f)return -1;
        else return dist[n];
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        for(int[] x : g)Arrays.fill(x,0x3f3f3f);
        while(m -- > 0){
            int a = sc.nextInt(),b = sc.nextInt(),c = sc.nextInt();
            g[a][b] = Math.min(g[a][b],c);
        }
        System.out.println(dijkstra());
    }
}

dijkstral(堆优化)

public static int dijkstral(int s){
        Arrays.fill(st,false);
        Arrays.fill(d,0x3f3f3f);
        d[s] = 0;
        PriorityQueue<PII> heap = new PriorityQueue<>((p1,p2) -> p1.x - p2.x);
        heap.add(new PII(0,s));

        while(heap.size() > 0){
            PII cur = heap.poll();
            int dis = cur.x, v = cur.y;
            if(st[v])continue;
            st[v] = true;

            for(int i = h[v];i != -1;i = ne[i]){
                int j = e[i];
                if(d[j] > dis + w[i]){
                    d[j] = dis + w[i];
                    heap.add(new PII(d[j],j));
                }
            }
        }
        ...
    }
class PII{
    int x;
    int y;
    public PII(int x,int y){
        this.x = x;
        this.y = y;
    }

Bellman-Ford

int bellman_ford()
{
    Arrays.fill(dist, 0x3f3f3f);
    dist[1] = 0;
    for (int i = 0; i < n; i ++ ){
        int[] backup = Arrays.copyOf(dist,n);//备份操作
        for (int j = 0; j < m; j ++ ){
            int a = edges[j].a, b = edges[j].b, w = edges[j].w;
            backup[b] = Math.min(backup[b],d[a] + w);
        }
    	 d = backup;
    }
    if (dist[n] > 0x3f3f3f3f / 2) return -1;
    return dist[n];
}
class Edge{
    int a;
    int b;
    int w;
}

spfa

public static int spfa(){
        Arrays.fill(d,0x3f3f3f);
        Queue<Integer> q = new LinkedList<>();
        q.add(1);
        d[1] = 0;
        st[1] = true;
        while(q.size() > 0){
            Integer cur = q.poll();
            st[cur] = false;

            for(int i = h[cur];i != -1;i = ne[i]){
                int j = e[i];

                if(d[j] > d[cur] + w[i]){
                    d[j] = d[cur] + w[i];
                    if(!st[j]){
                        st[j] = true;
                        q.add(j);
                    }
                }
            }
        }
        if(d[n] == 0x3f3f3f)return Integer.MIN_VALUE;
        return d[n];
    }

floyd

	public static void floyd(){
        for(int k = 1;k <= n;k ++){
            for(int i = 1;i <= n;i ++){
                for(int j = 1;j <= n;j ++){
                    d[i][j] = Math.min(d[i][j],d[i][k]+d[k][j]);
                }
            }
        }
    }
全部评论

相关推荐

点赞 评论 收藏
分享
hanliu:1. 排版与格式问题字体与对齐问题:标题和内容的字体大小差异不够明显,无法迅速吸引目光。某些文字看起来有些拥挤(比如校园经历中的“班委成员”部分)。2. 内容逻辑性模块顺序问题:实习经历放在较靠后的位置,实际上这部分内容对应聘来说更重要,建议提前突出。细节表述不够突出:比如教育背景部分的专业课程仅仅列出名字,没有说明自己在这些课程中表现如何或者掌握了什么技能,缺乏量化描述。多余内容:例如“班委成员”和“宣传委员”这类校园经历,叙述过于普通,缺乏和岗位相关的实质性贡献。,建议简写。3. 措辞专业性表达不够精准:例如“协助班长与团支书更好地为同学服务”显得较为笼统,没有实际成果的体现。用词重复:如“学习了焊接”“学习了光检”等重复词语较多,缺乏丰富的动词来展示个人能力(如“负责”“优化”“改进”等)。技能展示不足:虽然列出了UG和CAD证书,但没有明确提到这些技能如何在实际工作中发挥作用。4. 技能匹配度技能深度不足:虽然列出了掌握的软件和技术,但没有描述技能水平(如“熟练掌握”“精通”),也没有具体案例支持这些技能。缺乏岗位导向性:比如针对机械设计与制造方向,实习经历提到了“E6尾灯项目”,但没有详细说明自己在其中的技术贡献,可能会显得经验描述泛泛而谈。5. 自我评价问题表达空泛:如“具有良好的沟通协调能力”“责任心强”之类的描述太常见,没有让人眼前一亮的特点。缺乏成果支持:自我评价中的能力没有用具体项目、经历或成就来验证,可信度较弱。 兄弟加油
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务