高校网站建设建议,微信朋友圈投放广告怎么收费,上海市工程建设监理协会网站,企业模板图片目录
Floyd求最短路模板
4074. 铁路与公路 - floyd 脑筋急转弯 Floyd求最短路模板
活动 - AcWing 题目#xff1a; 给定一个 n 个点 m 条边的有向图#xff0c;图中可能存在重边和自环#xff0c;边权可能为负数。 再给定 k 个询问#xff0c;每个询问包含两个整数 x 和…目录
Floyd求最短路模板
4074. 铁路与公路 - floyd 脑筋急转弯 Floyd求最短路模板
活动 - AcWing 题目 给定一个 n 个点 m 条边的有向图图中可能存在重边和自环边权可能为负数。 再给定 k 个询问每个询问包含两个整数 x 和 y表示查询从点 x 到点 y 的最短距离如果路径不存在则输出 impossible 数据保证图中不存在负权回路 public static void floyd(){for(int k1;kn;k)for(int i1;in;i)for(int j1;jn;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 wtnew PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N210;static int n,m,k;static int[][] dnew int[N][N];public static void floyd(){for(int k1;kn;k)for(int i1;in;i)for(int j1;jn;j)d[i][j]Math.min(d[i][j],d[i][k]d[k][j]);}public static void main(String[] args) throws IOException{nrd.nextInt();mrd.nextInt();krd.nextInt();for(int i1;in;i)for(int j1;jn;j)if(ij) d[i][j]0; //如果是自环else d[i][j]0x3f3f3f3f;while(m--0){int ard.nextInt(),brd.nextInt(),wrd.nextInt();d[a][b]Math.min(d[a][b],w); //重边取最小}floyd();while(k--0){int xrd.nextInt(),yrd.nextInt();if(d[x][y]0x3f3f3f3f/2) wt.println(impossible);else wt.println(d[x][y]);}wt.flush();}static class rd{static BufferedReader bfnew BufferedReader(new InputStreamReader(System.in));static StringTokenizer tknew StringTokenizer();static String nextLine() throws IOException{return bf.readLine();}static String next() throws IOException{while(!tk.hasMoreTokens()) tknew 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 dnew BigInteger(rd.nextLine());return d;}}
}class PII
{int x,y;PII(int x,int y){this.xx;this.yy;}
} 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 wtnew PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N410;static int n,m;static int[][] trnew int[N][N];static int[][] carnew int[N][N];public static int floyd(int[][] d){for(int k1;kn;k)for(int i1;in;i)for(int j1;jn;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{nrd.nextInt();mrd.nextInt();for(int i1;in;i) Arrays.fill(tr[i],0x3f3f3f3f);for(int i1;in;i) Arrays.fill(car[i],0x3f3f3f3f);while(m--0){int ard.nextInt(),brd.nextInt();tr[a][b]tr[b][a]1;}for(int i1;in;i)for(int j1;jn;j)if(tr[i][j]!1i!j) car[i][j]car[j][i]1;int afloyd(tr);int bfloyd(car);int resMath.max(a,b);if(res0x3f3f3f3f) wt.print(-1);else wt.print(res);wt.flush();}static class rd{static BufferedReader bfnew BufferedReader(new InputStreamReader(System.in));static StringTokenizer tknew StringTokenizer();static String nextLine() throws IOException{return bf.readLine();}static String next() throws IOException{while(!tk.hasMoreTokens()) tknew 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 dnew BigInteger(rd.nextLine());return d;}}
}class PII
{int x,y;PII(int x,int y){this.xx;this.yy;}
} 文章转载自: http://www.morning.djlxz.cn.gov.cn.djlxz.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.tlrxp.cn.gov.cn.tlrxp.cn http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.shawls.com.cn.gov.cn.shawls.com.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.linzhigongmao.cn.gov.cn.linzhigongmao.cn http://www.morning.rmpfh.cn.gov.cn.rmpfh.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.fbfnk.cn.gov.cn.fbfnk.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.ksgjn.cn.gov.cn.ksgjn.cn http://www.morning.sqdjn.cn.gov.cn.sqdjn.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.rrms.cn.gov.cn.rrms.cn http://www.morning.khcpx.cn.gov.cn.khcpx.cn http://www.morning.zdxss.cn.gov.cn.zdxss.cn http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.lhytw.cn.gov.cn.lhytw.cn http://www.morning.lpzqd.cn.gov.cn.lpzqd.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.wqgr.cn.gov.cn.wqgr.cn http://www.morning.klltg.cn.gov.cn.klltg.cn http://www.morning.monstercide.com.gov.cn.monstercide.com http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.mkrqh.cn.gov.cn.mkrqh.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.kqwsy.cn.gov.cn.kqwsy.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.qmpbs.cn.gov.cn.qmpbs.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.gxklx.cn.gov.cn.gxklx.cn http://www.morning.dxqfh.cn.gov.cn.dxqfh.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.ryfq.cn.gov.cn.ryfq.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.sjqml.cn.gov.cn.sjqml.cn http://www.morning.cnhgc.cn.gov.cn.cnhgc.cn http://www.morning.ljllt.cn.gov.cn.ljllt.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.ngkng.cn.gov.cn.ngkng.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.czlzn.cn.gov.cn.czlzn.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.dlwzm.cn.gov.cn.dlwzm.cn http://www.morning.xkhhy.cn.gov.cn.xkhhy.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.rlwgn.cn.gov.cn.rlwgn.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.ghfrb.cn.gov.cn.ghfrb.cn