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 ...
随机推荐
- poj1857 To Europe! To Europe!
思路: 一维dp. 实现: #include <cstdio> #include <iostream> using namespace std; const int INF = ...
- AngularJS 表单验证手机号(非必填)
代码: <form ng-app="myApp" ng-controller="validateCtrl" name="myForm" ...
- WARN警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended
使用Apache HttpClient发送请求,有大量WARN警告:Going to buffer response body of large or unknown size. Using getR ...
- vue 组件相互传值
Part.1 传值几种方式 在写项目时,遇到组件传值问题,特此记录一波~~~ (1) 父传子 (2) 子传父 (2) 兄弟组件传值 Part.2 父传子 顾名思义,就是父组件传值给子组件 子 ...
- Must set property 'expression' before attempting to match
因为这个问题没有直接指向问题的地点, 所以找起来不是很容易. 但是如果找不到, 这个错就会一直都存在. 原因分析: 在使用切面编程的时候, 没有把切入点配置全面 解决方法: 在before, afte ...
- RestTemplate-postForObject源码
参数: 请求路径, 请求参数, 返回类型, 扩展模板变量
- Codeforces Round #555 (Div. 3) 解题报告
A.Reachable Numbers 题意: 给定操作f(x):将x加1,删去得到的数的所有末尾0,如f(10099)=10099+1=10100→1010→101.现在给定一个数n,对n进行无限次 ...
- iOS开发基础知识
1:App跳转至系统Settings 跳转在IOS8以上跟以下是有区别的,如果是IOS8以上可以如下设置: NSURL *url = [NSURL URLWithString:UIApplicatio ...
- IOS学习笔记37——ViewController生命周期详解
在我之前的学习笔记中讨论过ViewController,过了这么久,对它也有了新的认识和体会,ViewController是我们在开发过程中碰到最多的朋友,今天就来好好认识一下它.ViewContro ...
- 12scrapy_redis
一.简介 1.redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zse ...