【蓝桥杯集训16】多源汇求最短路——Floyd算法(2 / 2)

文章提供了两个使用Floyd算法求解最短路径的问题实例,一个是标准的最短路径查询,处理负权边和询问;另一个是在特定交通网络中,为火车和汽车规划路径以求最小时间。代码以Java实现,展示了如何避免火车和汽车在同一城市相遇并找到最小总时间。
摘要由CSDN通过智能技术生成

目录

Floyd求最短路模板

4074. 铁路与公路 - floyd + 脑筋急转弯


Floyd求最短路模板

活动 - AcWing

题目:

给定一个 n 个点 m 条边的有向图,图中可能存在重边和自环,边权可能为负数。

再给定 k 个询问,每个询问包含两个整数 x 和 y,表示查询从点 x 到点 y 的最短距离,如果路径不存在,则输出 impossible

数据保证图中不存在负权回路

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]);
    }
/*
    *道阻且长,行则将至*
    author:Roye_ack
*/
import java.util.*;
import java.io.*;
import java.math.*;
 
class Main
{
    static PrintWriter wt=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N=210;
    static int n,m,k;
    static int[][] d=new int[N][N];
    
    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]);
    }
    
    public static void main(String[] args) throws IOException
    {
        n=rd.nextInt();
        m=rd.nextInt();
        k=rd.nextInt();
        
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i==j) d[i][j]=0; //如果是自环
                else d[i][j]=0x3f3f3f3f;
        
        while(m-->0)
        {
            int a=rd.nextInt(),b=rd.nextInt(),w=rd.nextInt();
            d[a][b]=Math.min(d[a][b],w); //重边取最小
        }
        
        floyd();
        
        while(k-->0)
        {
            int x=rd.nextInt(),y=rd.nextInt();
            if(d[x][y]>0x3f3f3f3f/2) wt.println("impossible");
            else wt.println(d[x][y]);
        }
        
        wt.flush();
    }
    
    static class rd
    {
        static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        static StringTokenizer tk=new StringTokenizer("");
        
        static String nextLine() throws IOException
        {
            return bf.readLine();
        }
        
        static String next() throws IOException
        {
            while(!tk.hasMoreTokens()) tk=new StringTokenizer(bf.readLine());
            return tk.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 nextBig() throws IOException
        {
            BigInteger d=new BigInteger(rd.nextLine());
            return d;
        }
    }
}
 
class PII
{
    int x,y;
    PII(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
}

4074. 铁路与公路 - floyd + 脑筋急转弯

4074. 铁路与公路 - AcWing题库

题目:

  • 某国家有 n 个城市(编号 1∼n)和 m 条双向铁路
  • 对于每对不同的城市 x,y,当且仅当它们之间没有铁路时,它们之间会存在一条双向公路。
  • 经过每条铁路或公路都需要花费 1 小时的时间。
  • 现在有一列火车和一辆汽车同时离开城市 1,它们的目的地都是城市 n。
  • 它们不会在途中停靠(但是可以在城市 n 停靠)。
  • 火车只能沿铁路行驶,汽车只能沿公路行驶。
  • 请你为它们规划行进路线,每条路线中可重复经过同一条铁路或公路,但是为了避免发生事故,火车和汽车不得同时到达同一个城市(城市 n 除外)。
  • 请问,在这些条件的约束下,两辆车全部到达城市 n 所需的最少小时数,即求更慢到达城市 n 的那辆车所需的时间的最小值。

思路:

由题目可知,公路和铁路不会重合

那么必然有一辆车最短时间为1,因为1到n之间必然有一条路(公路或铁路)

一辆车从1直接到n,另一辆走其他路,则两车并不会在中途城市相遇

所以我们直接求两者的最短路即可

由于数据范围较小,我们用floyd算法(O(n^3))

/*
    *道阻且长,行则将至*
    author:Roye_ack
*/
import java.util.*;
import java.io.*;
import java.math.*;
 
class Main
{
    static PrintWriter wt=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N=410;
    static int n,m;
    static int[][] tr=new int[N][N];
    static int[][] car=new int[N][N];
    
    public static int floyd(int[][] d)
    {
        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]);
        return d[1][n];
    }
    
    public static void main(String[] args) throws IOException
    {
        n=rd.nextInt();
        m=rd.nextInt();
        
        for(int i=1;i<=n;i++) Arrays.fill(tr[i],0x3f3f3f3f);
        for(int i=1;i<=n;i++) Arrays.fill(car[i],0x3f3f3f3f);
        
        while(m-->0)
        {
            int a=rd.nextInt(),b=rd.nextInt();
            tr[a][b]=tr[b][a]=1;
        }
        
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(tr[i][j]!=1&&i!=j) car[i][j]=car[j][i]=1;
        
        int a=floyd(tr);
        int b=floyd(car);
        
        int res=Math.max(a,b);
        if(res==0x3f3f3f3f) wt.print(-1);
        else wt.print(res);
        
        wt.flush();
    }
    
    static class rd
    {
        static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        static StringTokenizer tk=new StringTokenizer("");
        
        static String nextLine() throws IOException
        {
            return bf.readLine();
        }
        
        static String next() throws IOException
        {
            while(!tk.hasMoreTokens()) tk=new StringTokenizer(bf.readLine());
            return tk.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 nextBig() throws IOException
        {
            BigInteger d=new BigInteger(rd.nextLine());
            return d;
        }
    }
}
 
class PII
{
    int x,y;
    PII(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值