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的更多相关文章

  1. 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 ...

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

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

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

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

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

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

  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 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 ...

  8. 【50.00%】【codeforces 602C】The Two Routes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. Codeforces Gym 100015C City Driving 离线LCA

    City Driving 题目连接: http://codeforces.com/gym/100015/attachments Description You recently started fre ...

随机推荐

  1. AJPFX总结List的三个子类的特点

    ArrayList:                        底层数据结构是数组,查询快,增删慢.                        线程不安全,效率高.              ...

  2. re正则表达式讲解—初步认识

    # f = open(r"C:\Users\LENOVO\Desktop\模特.txt",'r') # 1.常规提取文档内容方法 # contacts = [] # for i i ...

  3. Android开发中SharedPreferences的使用

    在Android开发中,在储存少量的数据时,个人感觉SharedPreferences是最好的选择,SharedPreferences是以键值对的方式进行储存,支持boolean,int,float, ...

  4. ubuntu下安装php-curl扩展

    查找包 apt-cache是linux下的一个apt软件包管理工具,它可查询apt的二进制软件包缓存文件.APT包管理的大多数信息查询功能都可以由apt-cache命令实现,通过apt-cache命令 ...

  5. Selenium私房菜系列2 -- XPath的使用【ZZ】

    在编写Selenium案例时,少不免是要用到XPath的,现在外面关于XPath使用的参考资料很多,下面我直接转一篇关于XPath使用的文档.如果对XPath不熟悉请参考下文,你不需要去百度/Goog ...

  6. toast插件的简单封装(样式适用pc后台管理系统的场景)

    直接分三个步骤吧: 1.手写一个toast.vue组件 <template> <transition name="toast-fade"> <div ...

  7. html文本溢出显示省略字符的两种常用方法

    方法一:使用CSS溢出省略的方式解决 解决效果如下: css代码: display: -webkit-box; display: -moz-box; white-space: pre-wrap; wo ...

  8. SQLite-Like语句

    SQLite – LIKE子句 使用SQLite LIKE运算符 用于匹配文本.如果搜索表达式可以匹配模式表达式,如操作符将返回true,这是1.有两个通配符与Like操作符一起使用: The per ...

  9. Kubernetes 架构(上)【转】

    Kubernetes Cluster 由 Master 和 Node 组成,节点上运行着若干 Kubernetes 服务. Master 节点 Master 是 Kubernetes Cluster ...

  10. python+selenium自动化登录dnf11周年活动界面领取奖励登录部分采坑总结[1]

    背景: Dnf的周年庆活动之一,游戏在6月22日 06:00~6月23日 06:00之间登陆过游戏后可以于6月25日 16:00~7月04日 06:00领取奖励 目标:连续四天自动运行脚本,自动领取所 ...