CodeForces - 601A The Two Routes
http://codeforces.com/problemset/problem/601/A
这道题没想过来, 有点脑筋急转弯的感觉了
本质上就是找最短路径 但是卡在不能重复走同一个点 ---->>> 这是来坑人的
因为这是一个完全图(不是被road 连接 就是被rail连接 ) 所以一定有一条直接连1 和 n的路径
那么只用找没有连 1 和 n 的路径的 那个图的最短路即可
然后这个dijkstra写的是O(V^2)的写法
以后尽量用优先队列的写法O(ElogV)
#include <iostream>
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
using namespace std; int rail[][];
int road[][];
int n, m; int dijkstra(int town[][])
{
int dist[];
bool use[];
memset(use, , sizeof(use));
fill(dist, dist+n+, INF);
dist[] = ;
while (true)
{
int v = -;
for (int i = ; i <= n; i++)
{
if (!use[i] && (v == - || dist[i] < dist[v])) v = i;
}
if (v == -) break;
use[v] = true;
for (int i = ; i <= n; i++)
{
dist[i] = min(dist[i], dist[v] + town[v][i]);
}
}
return dist[n]; }
int main()
{
freopen("in.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
rail[i][j] = INF;
road[i][j] = ;
}
}
for (int i = ; i < m; i++)
{
int x, y;
scanf("%d%d", &x, &y);
rail[x][y] = ;
rail[y][x] = ;
road[x][y] = INF;
road[y][x] = INF;
}
int ans = ;
if (rail[][n] == INF || rail[n][] == INF) ans = dijkstra(rail);
else ans = dijkstra(road);
if (ans == INF) printf("-1\n");
else printf("%d\n",ans);
}
CodeForces - 601A The Two Routes的更多相关文章
- 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 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没 ...
- [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路
14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...
- Codeforces 601A:The Two Routes 宽搜最短路径
A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 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 Round #333 (Div. 2) C. The Two Routes flyod
C. The Two Routes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/pro ...
- 【50.00%】【codeforces 602C】The Two Routes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Gym 100015C City Driving 离线LCA
City Driving 题目连接: http://codeforces.com/gym/100015/attachments Description You recently started fre ...
随机推荐
- 【学习笔记】深入理解js原型和闭包(7)——原型的灵活性
在Java和C#中,你可以简单的理解class是一个模子,对象就是被这个模子压出来的一批一批月饼(中秋节刚过完).压个啥样,就得是个啥样,不能随便动,动一动就坏了. 而在javascript中,就没有 ...
- Android MVVM小结
一.概念 关于MVC.MVP与MVVM的概念就不介绍了,总之一句话,MVVM概念出现比MVP早,MVP比MVC早,作为程序员就应该去学习最新的技术不是?详细的概念介绍移步这里吧,https://www ...
- 51nod 1272 最大距离
题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 给出一个长度为N的整数数组A,对于每一个数组元素,如果他后面存在大于等 ...
- 自定义Jquery分页插件
/** * 功能说明:jPager 分页插件 * 参数说明:pages:[] 分页的控件个数 @id:显示分页的div ID,@showSelectPage: 是否显示当前分页的条目过滤下拉框 * @ ...
- github 下载全部项目
从github下载资料过程中,有些项目含有子模块,有时通过git clone 或者下载zip方式项目可能会缺少文件,因此需要执行 git submodule update --init --recur ...
- Unity3D windows平台视频录制录屏插件 UnityRecorder
例子:从官方例子简单改了 using UnityEditor;using UnityEditor.Recorder;using UnityEditor.Recorder.Input;using Sys ...
- Spring AOP源码解析——专治你不会看源码的坏毛病!
昨天有个大牛说我啰嗦,眼光比较细碎,看不到重点.太他爷爷的有道理了!要说看人品,还是女孩子强一些. 原来记得看到一个男孩子的抱怨,说怎么两人刚刚开始在一起,女孩子在心里就已经和他过完了一辈子.哥哥们, ...
- Linux之用户权限管理
chmod(更改目录或文件权限) 在linux中,文件的权限分为3中,拥有者,群组,其他人.而chmod则是对权限更改的命令. u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个组,o 表示其 ...
- 主席树-指针实现-找第k小数
主席树,其实就是N颗线段树 只是他们公用了一部分节点(๑•̀ㅂ•́)و✧ 我大部分的代码是从一位大佬的那里看到的 我这个垃圾程序连Poj2104上的数据都过不了TLE so希望神犇能给我看看, 顺便给 ...
- 安装docker和docker-compose
环境:centos7,参考官方文档:https://docs.docker.com/insta... 第一步:删除旧版本和相关依赖,运行命令: yum remove docker \ docker ...