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. CF963A Alternating Sum

    思路:利用周期性转化为等比数列求和. 注意当a != b的时候 bk * inv(ak) % (109 + 9)依然有可能等于1,不知道为什么. 实现: #include <bits/stdc+ ...

  2. 02全志r58平台Android4.4.4下关闭内核中的CPU的开启关闭提示

    02全志r58平台Android4.4.4下关闭内核中的CPU的开启关闭提示 2017/8/18 13:53 版本:V1.0 开发板:SC5806(全志R58平台) SDK:android4.4.4 ...

  3. String 截取字符串#中间的文本

    通过正则实现: String regex = "#([^#]+)#"; @Test public void test() { String text = "#中俄建交七十 ...

  4. Android模板制作

    本文详细介绍模板相关的知识和如何制作Android模版及使用,便于较少不必要的重复性工作.比如我在工作中如果要创建一个新的模块,就不要需要创建MVP相关的几个类:Model.View.Presente ...

  5. Kotlin学习的一些心得

    1.工程manifest中的application节点有冲突时,添加 xmlns:tools="http://schemas.android.com/tools" tools:re ...

  6. IOS代码收集

    http://mobile.51cto.com/hot-410417.htm 退回输入键盘: - (BOOL) textFieldShouldReturn:(id)textField{ [textFi ...

  7. js文件下载代码 import downloadjs from 'downloadjs'

    function logDownload(contribution_id) { setTimeout(function () { $.ajax({ url: "/ajax/Wallpaper ...

  8. Chrome安装助手踩坑

    [前言] 最近用之前的方法配置hosts,想浏览下载国外网站的数据和插件,突然发现几乎所有的方法都无效了...... 本文介绍下下载谷歌助手,通过助手访问国外网站 [主体] (1)搜索谷歌助手,点击下 ...

  9. Vickers Vane Pump - How To Choose Vane Pump Parameter Specifications?

    1 rated pressure selection. The rated pressure of the vane pump products is 7MPa, 1OMPa, 16MPa, 2lMP ...

  10. github下拉刷新与上拉加载地址

    https://github.com/chrisbanes/Android-PullToRefresh