ACM学习历程—CodeForces 601A The Two Routes(最短路)
题目链接:http://codeforces.com/problemset/problem/601/A
题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇。
此外,有铁路的地方肯定没有陆路。
这种情况下就会有一个结论,就是至少有一种交通可以直接1到n。
这样只需要对另一种跑一个最短路就OK了。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; const int maxN = ;
int n, m;
bool dis[maxN][maxN];
int p[maxN];
bool flag;
struct node
{
int val;
int id;
bool operator<(node x) const
{
return val > x.val;
}
}; void input()
{
memset(dis, false, sizeof(dis));
int u, v;
for (int i = ; i < m; ++i)
{
scanf("%d%d", &u, &v);
dis[u][v] = dis[v][u] = true;
}
if (dis[][n]) flag = false;
else flag = true;
} void work()
{
memset(p, -, sizeof(p));
int ans = , u;
node k, v;
priority_queue<node> q;
p[] = ;
k.val = ;
k.id = ;
q.push(k);
while (!q.empty())
{
k = q.top();
q.pop();
u = k.id;
for (int i = ; i <= n; ++i)
{
if (i == u) continue;
if (dis[i][u] != flag) continue;
if (p[i] == - || p[i] > p[u]+)
{
p[i] = p[u]+;
k.id = i;
k.val = p[i];
q.push(k);
}
}
}
if (p[n] == -) ans = -;
else ans = p[n];
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d%d", &n, &m) != EOF)
{
input();
work();
}
return ;
}
ACM学习历程—CodeForces 601A The Two Routes(最短路)的更多相关文章
- ACM学习历程—Codeforces Round #354 (Div. 2)
http://codeforces.com/contest/676 在allzysyz学弟和hqwhqwhq的邀请下,打了我的第三场CF... 毕竟在半夜..所以本来想水到12点就去睡觉的...结果一 ...
- ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...
- ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)
Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...
- ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)
Description Let's consider one interesting word game. In this game you should transform one word int ...
- [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路
14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU5521 Meeting(图论)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...
- 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 ...
随机推荐
- byte[] 、Bitmap与Drawbale 三者直接的转换
经常遇到这种类似头疼的问题 byte[] .Bitmap与Drawbale 三者直接的转换 1.byte[] ->Bitmap Bitmap Bitmap = BitmapFactory.dec ...
- eclipse 给jar包关联javadoc
1.右键点击Referenced Libraries下的jar --> 选择 Build Path --> Configure Build Path. 2.选择jar的Javadoc lo ...
- EasyPlayer.js H5播放器帮助我这种不会前端的普通用户也能轻松实现直播接入
说到EasyPlayer.js,先得说一下EasyPlayer到底是啥, An elegant, simple, fast android RTSP/RTMP/HLS/HTTP Player.Easy ...
- 关于Future
1 为什么需要Callable和Future Runnable没有返回值,也不抛异常,这样主线程不能知道子线程的执行结果. 为了解决这个问题就有了Callable和Future.Callable提供的 ...
- spring属性注入方式
一.使用有参构造注入属性 配置文件 constructor-arg标签是需注入属性的名字 User类 生成了User的有参构造函数 测试类 结果 打印出了name属性的值 二.使用set方法注入属性 ...
- eclispse + tomcat 启动是不加载项目的解决办法
有一个java spring的项目一直好好的,突然一天不能启动了.eclipse的console没有报任何错误,相关的server配置也没有问题,经过一翻折腾顺便还把eclipse从indigo升级到 ...
- Notification状态栏显示信息
Notification即通知,用于在通知栏显示提示信息. 在API Level > 11,Notification类中的一些方法被Android声明deprecated(弃用),而在API L ...
- C#BackgroundWorker组件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- windows下的mysql闪退问题
早上来启动MySQL发现输入密码就闪退,连续试了好几次,最后到网上查到了解决方案. 与Linux系统下MySQL密码丢失的操作步骤基本一样. 首先要跳过密码启动MySQL服务. 启动服务必须使用全路径 ...
- IOS NSDate 调整当前时间戳为明天
这个可以根据需要调整 在day month hour minute second 等都行 以下是以当前时间戳为基础,调整时间为明天的零点零时零分零秒 可以根据需要 写成毫秒的 +(NSStri ...