codeforce-601A. The Two Routes(最短路)
题意:
给你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(最短路)的更多相关文章
- [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路
14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...
- 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 ...
- ACM学习历程—CodeForces 601A The Two Routes(最短路)
题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没 ...
- CodeForces - 601A The Two Routes
http://codeforces.com/problemset/problem/601/A 这道题没想过来, 有点脑筋急转弯的感觉了 本质上就是找最短路径 但是卡在不能重复走同一个点 ----> ...
- The Two Routes CodeForces - 601A(水最短路)
一个完全图 1和n肯定有一条路 不是公路就是铁路 另= 另一个跑遍最短路即可 #include <bits/stdc++.h> #define mem(a, b) memset(a, ...
- CodeForces 602C The Two Routes(最短路)
Description In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. T ...
- Codeforces 601A:The Two Routes 宽搜最短路径
A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforce Gym 100570B ShortestPath Query (最短路SPFA)
题意:询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同,无重边且边权为正. 题解:因为路径的合法性和边的颜色有关, 所以在做spfa的时候,把边丢到队列中去,松弛的时候注意判断一下颜 ...
- [USACO14OPEN] Dueling GPS's[最短路建模]
题目描述 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked t ...
随机推荐
- 网络爬虫-python-爬取天涯求职贴
使用urllib请求页面,使用BeautifulSoup解析页面,使用xlwt3写入Excel import urllib.request from bs4 import BeautifulSoup ...
- Demo03找素数
package Deom1;import java.awt.*;import java.util.Scanner;public class lx {//输入任意两个正整数,求出这两个正整数之间素数的个 ...
- 日常Java 2021/9/21
将Java数组中的元素前后反转.题目要求:已知一个数组arr = {11,12,13,14,15}用程序实现把该数组中的元素值交换,交换后的数组arr = { 15,14,13,12,11},并输出交 ...
- Spark基础:(七)Spark Streaming入门
介绍 1.是spark core的扩展,针对实时数据流处理,具有可扩展.高吞吐量.容错. 数据可以是来自于kafka,flume,tcpsocket,使用高级函数(map reduce filter ...
- 容器之分类与各种测试(三)——forward_list的用法
forward_list是C++11规定的新标准单项链表,slist是g++以前的规定的单项链表 例程 #include<stdexcept> #include<string> ...
- C语言time函数获取当前时间
以前放了个链接,但是原作者把博文删了,这里放一个获取时间的代码,已经比较详细所以不做注释 #include<stdio.h> #include<time.h> #include ...
- spring注解事务管理
使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...
- 3.0 go mod之远程仓库搭建-代码示例
注意事项 所谓的远程仓库指的是github,个人首次使用go mod在其他云仓库上尝试,并未成功,这浪费了我近2小时的时间: 如果你是初次尝试,那么除了github的地址换一下之外,其他的都按照示例操 ...
- “==” 和 equals()的区别
※ "==" 和 equals()的区别 ※ == :比较. 基本数据类型比较的是值:. 引用类型比较的是地址值. ※ equals(Object o):1)不能比较基本数据类型, ...
- 关于python中的随机种子——random_state
random_state是一个随机种子,是在任意带有随机性的类或函数里作为参数来控制随机模式.当random_state取某一个值时,也就确定了一种规则. random_state可以用于很多函数,我 ...