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 ...
随机推荐
- 【前端】html5获取经纬度,百度api获取街区名,并使用JS保存进cookie
引用js<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak= ...
- poj2385 Apple Catching
思路: 简单dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...
- jQuery选择器之表单元素选择器
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- Metinfo 5.3.19管理员密码重置漏洞复现
Metinfo 5.3.19管理员密码重置漏洞 操作系统:Windows 10专业版 kali linux 网站环境:UPUPW 5.3 使用工具:burpsuite 1.7 beta 漏洞分 ...
- magic_quotes_runtime 和 magic_quotes_sybase 的作用
如果启用了 magic_quotes_runtime,大多数返回任何形式外部数据的函数,包括数据库和文本段将会用反斜线转义引号. 如果启用了magic_quotes_sybase,单引号会被单引号转义 ...
- Vsphere中ESXi主机ssh开启的三种方法
ESXi 5.5是直接安装在物理主机上的一个虚拟机系统,本质上是一个Linux系统. 平时可以通过VMware Client端或者VMware vCenter进行管理,但对于一些特殊的VMware命令 ...
- input_shape { dim: 1 dim: 3 dim: 224 dim: 224 }
http://blog.csdn.net/u010417185/article/details/52619593
- Qt _六天的学习路线
六天的学习路线:第一天: 1.Qt的介绍 2.Qt的框架 3.项目文件(.pro) 4.第一个Qt程序(hello Qt) 5.父窗口和子窗口的区别(控件,部件,构件) ...
- 使用plsql导入dmp文件缺少imp*.exe
在C:\app\Administrator\product\11.2.0\client_2\BIN 找到imp.exe 导入
- OpenCV2:第二章 创建图像并显示
一.简介 相当于在PS中,新建一个画布 二.CvMat类/LPLImage和CvMat结构体 参考: OpenCV2:第一章 图像表示 三.create() Mat m(2,2,CV_8UC3); m ...