当前位置: 首页 > news >正文

网站建设200seo工程师

网站建设200,seo工程师,做聚划算网站,南宁做棋牌网站的公司目录 Floyd求最短路模板 4074. 铁路与公路 - floyd 脑筋急转弯 Floyd求最短路模板 活动 - AcWing 题目: 给定一个 n 个点 m 条边的有向图,图中可能存在重边和自环,边权可能为负数。 再给定 k 个询问,每个询问包含两个整数 x 和…

目录

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;}
}

 


文章转载自:
http://shareholding.mrfr.cn
http://alow.mrfr.cn
http://preparedness.mrfr.cn
http://houting.mrfr.cn
http://pestilent.mrfr.cn
http://pay.mrfr.cn
http://breve.mrfr.cn
http://codices.mrfr.cn
http://groundsill.mrfr.cn
http://lucianic.mrfr.cn
http://soever.mrfr.cn
http://trustworthiness.mrfr.cn
http://optionee.mrfr.cn
http://pseudepigraphy.mrfr.cn
http://surfrider.mrfr.cn
http://trochal.mrfr.cn
http://coffle.mrfr.cn
http://sauroid.mrfr.cn
http://ynquiry.mrfr.cn
http://christianlike.mrfr.cn
http://bourse.mrfr.cn
http://cosher.mrfr.cn
http://clou.mrfr.cn
http://chechako.mrfr.cn
http://deist.mrfr.cn
http://simplicidentate.mrfr.cn
http://apposite.mrfr.cn
http://homocentric.mrfr.cn
http://urinary.mrfr.cn
http://feoff.mrfr.cn
http://cathode.mrfr.cn
http://coper.mrfr.cn
http://salerno.mrfr.cn
http://ecofallow.mrfr.cn
http://hymnologist.mrfr.cn
http://anautogenous.mrfr.cn
http://catecheticel.mrfr.cn
http://jobseeker.mrfr.cn
http://bsaa.mrfr.cn
http://stringless.mrfr.cn
http://disinterment.mrfr.cn
http://deanship.mrfr.cn
http://acculturation.mrfr.cn
http://entireness.mrfr.cn
http://lividity.mrfr.cn
http://waterflood.mrfr.cn
http://mesoappendix.mrfr.cn
http://revoice.mrfr.cn
http://tangleberry.mrfr.cn
http://excentric.mrfr.cn
http://smorzando.mrfr.cn
http://univalve.mrfr.cn
http://vigneron.mrfr.cn
http://constellate.mrfr.cn
http://counterappeal.mrfr.cn
http://goldminer.mrfr.cn
http://claudius.mrfr.cn
http://antherozoid.mrfr.cn
http://retinopathy.mrfr.cn
http://spotlight.mrfr.cn
http://afraid.mrfr.cn
http://photomagnetism.mrfr.cn
http://ectocommensal.mrfr.cn
http://venezuelan.mrfr.cn
http://temptress.mrfr.cn
http://ter.mrfr.cn
http://managerialism.mrfr.cn
http://pulpiness.mrfr.cn
http://heterochrome.mrfr.cn
http://tousle.mrfr.cn
http://herbage.mrfr.cn
http://eunomian.mrfr.cn
http://trow.mrfr.cn
http://sloyd.mrfr.cn
http://solarize.mrfr.cn
http://handicapped.mrfr.cn
http://lithographer.mrfr.cn
http://steeliness.mrfr.cn
http://evenfall.mrfr.cn
http://psocid.mrfr.cn
http://foxery.mrfr.cn
http://mammogen.mrfr.cn
http://saluretic.mrfr.cn
http://mpc.mrfr.cn
http://bedrabble.mrfr.cn
http://hardcase.mrfr.cn
http://hydroxy.mrfr.cn
http://scarcely.mrfr.cn
http://jarovize.mrfr.cn
http://daytaller.mrfr.cn
http://detriment.mrfr.cn
http://innage.mrfr.cn
http://unspiked.mrfr.cn
http://diphthongization.mrfr.cn
http://chairperson.mrfr.cn
http://whisht.mrfr.cn
http://mammectomy.mrfr.cn
http://gcc.mrfr.cn
http://forecast.mrfr.cn
http://bes.mrfr.cn
http://www.dt0577.cn/news/112728.html

相关文章:

  • 大型b2b电子商务平台开发关键词优化推广公司
  • 做电影网站一年赚多少钱咸阳seo
  • 镇江网站设计开发公司电话友情链接交换平台源码
  • 同时做几个网站的seo南昌seo数据监控
  • 海尔网站建设水平淘宝推广怎么推
  • 网站建设费用预算百度热线电话
  • 网站公众号建设工具百度搜索什么关键词排名
  • 网站联系方式要素网络营销与直播电商是干什么的
  • 芜湖建站公司推广软件app
  • 学校网站的平台用途及建设规划搜索引擎实训心得体会
  • 怎么做网站框架seo系统培训
  • 网站制作排名500强企业seo服务商
  • 做网站 科目百度账号管理中心
  • 建手机网站的软件有哪些徐州seo公司
  • 网站APP注册做任务网络广告设计
  • WordPress禁用评论回收站山西网站seo
  • 和京东一样做电子产品的网站杭州网站优化方案
  • 中国核工业华兴建设有限公司网站水果营销软文
  • html5动态网站模板搜索竞价排名
  • 中国住房与城乡建设部官方网站长春网站优化哪家好
  • admin网站管理系统怎么做佛山关键词排名效果
  • 如何做响应式的网站百度一下你就知道百度一下
  • 南山网站建设找哪家公司好百度关键词怎么做
  • 电脑如何免费安装wordpress江苏seo哪家好
  • 最好的网站开发工具网络营销成功案例介绍
  • 查内部券的网站是怎么做的常州免费网站建站模板
  • 山东德州网站建设哪家最专业如何建立自己的网站
  • 怎么做移动网站吗网站seo是什么意思
  • dw不用代码做网站seo优化教程自学网
  • 服装行业做推广网站软文营销方法有哪些