题意:

给你N个点表示N个站,有汽车和火车,汽车只能走公路,火车只能走铁路。

然后给你M条双向路,代表这两个点之间有铁路连接。

然后告诉你如果两个点之间没有铁路,那么就是公路连接。

问你汽车和火车都到达目的地所要的最小时间是多少(两种交通工具不能同时到达同一个城市除了目的地)。

如果有一种交通工具不能到达就输出-1;

思路:

如果两两点之间都是铁路,那么就没有公路,那么汽车不能到达,这时-1;

如果不是这种情况那么要么有铁路连接起点终点,要么有公路,所以两者间必有一个为1,

所以求另一个的最短路就行(肯定不会相撞)。(求两次最短路也行)。

复杂度N2;

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<vector>
6 #include<queue>
7 #include<cstdio>
8 #include<string.h>
9 void dj(int n);
10 const int V=99999999;
11 typedef struct pp
12 {
13 int x;
14 int y;
15 } ss;
16 using namespace std;
17 int flag[500];
18 int jj[500][500];
19 int d[500];
20 int main(void)
21 {
22 int n,i,j,k,p,q,N,M;
23 while(scanf("%d %d",&N,&M)!=EOF)
24 {
25 memset(flag,0,sizeof(flag));
26 for(i=0; i<450; i++)
27 {
28 for(j=0; j<450; j++)
29 {
30 jj[i][j]=V;
31 }
32 }
33 for(i=0; i<M; i++)
34 {
35 scanf("%d %d",&p,&q);
36 jj[p][q]=1;
37 jj[q][p]=1;
38 }
39 dj(N);
40 int ss=d[N];
41 for(i=0; i<450; i++)//求汽车的路径
42 {
43 for(j=0; j<450; j++)
44 {
45 if(jj[i][j]==V)
46 {
47 jj[i][j]=1;
48 }
49 else jj[i][j]=V;
50 }
51 }
52 dj(N);
53 int vv=d[N];
54 int uu=max(vv,ss);
55 if(uu>=V)
56 {
57 printf("-1\n");
58 }
59 else printf("%d\n",uu);
60
61 }
62 return 0;
63 }
64
65 void dj(int n)//最短路N2算法
66 {
67 fill(d,d+n+1,V);
68 fill(flag,flag+n+1,0);
69 d[1]=0;
70 int i,j,k,p,q;
71 while(true)
72 {
73 int l=-1;
74 for(i=1; i<=n; i++)
75 {
76 if(flag[i]==0&&(l==-1||d[i]<d[l]))
77 {
78 l=i;
79 }
80 }
81 if(l==-1)
82 {
83 break;
84 }
85 flag[l]=1;
86 for(i=1; i<=n; i++)
87 {
88 d[i]=min(d[i],d[l]+jj[l][i]);
89 }
90 }
91
92 }

codeforce-601A. The Two Routes(最短路)的更多相关文章

  1. [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路

    14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...

  2. codeforces 601A The Two Routes(最短路 flody)

    A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. ACM学习历程—CodeForces 601A The Two Routes(最短路)

    题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没 ...

  4. CodeForces - 601A The Two Routes

    http://codeforces.com/problemset/problem/601/A 这道题没想过来, 有点脑筋急转弯的感觉了 本质上就是找最短路径 但是卡在不能重复走同一个点 ----> ...

  5. The Two Routes CodeForces - 601A(水最短路)

    一个完全图 1和n肯定有一条路  不是公路就是铁路  另= 另一个跑遍最短路即可 #include <bits/stdc++.h> #define mem(a, b) memset(a, ...

  6. CodeForces 602C The Two Routes(最短路)

    Description In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. T ...

  7. Codeforces 601A:The Two Routes 宽搜最短路径

    A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. codeforce Gym 100570B ShortestPath Query (最短路SPFA)

    题意:询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同,无重边且边权为正. 题解:因为路径的合法性和边的颜色有关, 所以在做spfa的时候,把边丢到队列中去,松弛的时候注意判断一下颜 ...

  9. [USACO14OPEN] Dueling GPS's[最短路建模]

    题目描述 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked t ...

随机推荐

  1. 24-Longest Palindromic Substring-Leetcode

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. 第一个基础框架 — mybatis框架 — 更新完毕

    1.Mybatis是什么? 百度百科一手 提取一下重点: MyBatis 本是apache的一个开源项目iBatis.即:mybatis的原名为:ibatis 2010年迁移到google code, ...

  3. SpringCloud微服务实战——搭建企业级开发框架(三十):整合EasyExcel实现数据表格导入导出功能

      批量上传数据导入.数据统计分析导出,已经基本是系统必不可缺的一项功能,这里从性能和易用性方面考虑,集成EasyExcel.EasyExcel是一个基于Java的简单.省内存的读写Excel的开源项 ...

  4. A Child's History of England.51

    CHAPTER 14 ENGLAND UNDER KING JOHN, CALLED LACKLAND At two-and-thirty years of age, John became King ...

  5. Scala(六)【模式匹配】

    目录 一.基本语法 二.匹配固定值 三.守卫 四.匹配类型 五.匹配集合 1.Array 2.List 3.元祖 4.对象和样例类 六.偏函数 七.赋值匹配 八.for循环匹配 一.基本语法 在匹配某 ...

  6. CentOS7 搭建maven私服Nexus

    下载解压 官网https://www.sonatype.com/download-oss-sonatype 下载页面 https://help.sonatype.com/repomanager2/do ...

  7. 删除button中除label之外的View

    因为button中的UIButtonLabel判断class类型时,会被认为是view,所以想删除view类型的子控件时,会将label也删掉,从而无法显示title,此时,可以给指定的View添加t ...

  8. 【面试】【Linux】【Web】基础概念

    1. HTTP https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol 2. TCP handshake https://en.wikipe ...

  9. my41_主从延迟大排查

    半同步复制 主库执行 INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; SET GLOBAL rpl_semi_sync ...

  10. Vector Bin Packing 华为讲座笔记

    Vector bin packing:first fit / best fit / grasp 成本:性价比 (先验) 设计评价函数: evaluation function:cosine simil ...