Problem 2 慢跑问题
jogging.out
jogging.out
分析:
算法一
算法二
时间复杂度:O(N+M) 空间复杂度:O(N+M) 期望得分:100 分
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100005,maxm=2000005,INF=2000000000;
int dis[maxn],head[maxn],n,ml,md,cnt=0;
struct eadge
{int next,to,dist;
} e[maxm];
void add(int from,int to,int dist)
{e[++cnt].next=head[from];e[cnt].to=to;e[cnt].dist=dist;head[from]=cnt;
}
int main()
{freopen("jogging.in","r",stdin);freopen("jogging.out","w",stdout);scanf("%d%d%d",&n,&ml,&md);int x,y,z;for(int i=1;i<=ml;i++){scanf("%d%d%d",&x,&y,&z);if(x<y) add(x,y,z);else add(y,x,z);}for(int i=1;i<=md;i++){scanf("%d%d%d",&x,&y,&z);if(x<y) add(x,y,-z);else add(y,x,-z);}for(int i=1;i<=n;i++)dis[i]=INF;dis[1]=0;for(int i=1;i<=n;i++){if(dis[i]!=INF){for(int j=head[i];j;j=e[j].next){int to=e[j].to;if(dis[to]>dis[i]+e[j].dist)dis[to]=dis[i]+e[j].dist;}}}printf("%d\n",dis[n]);if(dis[n]<0) printf("YES\n");else printf("NO\n");fclose(stdin);fclose(stdout);return 0;
}
当然用直接用spfa也是不会错的,但要比上一种算法要慢一点。
关于慢跑问题的说明:
Problem 2 慢跑问题相关推荐
- linux下yum错误:[Errno 14] problem making ssl connection Trying other mirror.
所有的base 都要取消注释 mirrorlist 加上注释 另外所有的enable都要设为零 目录 今天是要yum命令安装EPEL仓库后 yum install epel-release 突然发现y ...
- A + B Problem
1001: A + B Problem Description 计算 A + B. Input 多组测试数据,每组测试数据占一行,包括2个整数. Output 在一行中输出结果. Sample Inp ...
- Error:(49, 1) A problem occurred evaluating project ':guideview'. Could not read script 'https://r
出现问题如下: Error:(49, 1) A problem occurred evaluating project ':guideview'. > Could not read script ...
- #418 Div2 Problem B An express train to reveries (构造 || 全排列序列特性)
题目链接:http://codeforces.com/contest/814/problem/B 题意 : 有一个给出两个含有 n 个数的序列 a 和 b, 这两个序列和(1~n)的其中一个全排列序列 ...
- ADPRL - 近似动态规划和强化学习 - Note 3 - Stochastic Infinite Horizon Problem
Stochastic Infinite Horizon Problem 3.Stochastic Infinite Horizon Problem 定义3.1 无限范围的马尔可夫决策过程 (Marko ...
- ADPRL - 近似动态规划和强化学习 - Note 2 - Stochastic Finite Horizon Problem
2. Stochastic Finite Horizon Problem 在这一节中主要介绍了随机DP算法来解决不确定性下的有限地范围问题,如Denition 1.4所述,它被表述为一个组合优化问题. ...
- There was a problem confirming the ssl certificate ……
在安装一个Python库onetimepass时发生下面的问题: pip install onetimepass Could not fetch URL https://pypi.python.org ...
- HDU 1757 A Simple Math Problem
Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x & ...
- The C10K problem原文翻译
原文地址:http://www.cnblogs.com/fll/archive/2008/05/17/1201540.html The C10K problem 如今的web服务器需要同时处理一万个以 ...
- This is probably not a problem with npm. There is likely additional logging output above
微信小程序开发交流qq群 173683895 承接微信小程序开发.扫码加微信. E:\weii_objct\invoice-manage-web-view>npm start > ...
最新文章
- Linux 进程管理工具
- MATLAB时间序列的排序函数
- 【数据结构总结】第六章 图(非线性结构)
- JS运行机制(浏览器内核)
- python中的装饰器-(重复阅读)
- arcgis报错常用解决方法
- 数据结构链表之单链表的快慢指针——3
- THUSCH 2017 大魔法师(矩阵乘法+线段树)
- jQuery常用知识点总结以及平时封装常用函数
- SpringBoot项目在Gitee的控管步骤
- 详尽解读中美科技差距究竟多么巨大
- 烟台市计算机二级培训机构,烟台市2020年3月计算机二级报名时间|网上报名入口【12月20日9:00开通】...
- P6039A高压衰减棒使用方法及注意事项
- eclipse插件下载及使用
- 手摸手。完成一个H5 抽奖功能
- 计算机系统时间无法更改,电脑时间改不了,小编教你电脑无法修改系统时间如何解决...
- html图片轮播15个自动,15个超强的jQuery/HTML5图片轮播插件
- web网页设计期末课程大作业 基于HTML+CSS+JavaScript响应式环保科技公司网站模板(环保主题网站设计)
- hao.360.cn不停跳....
- 加密币Terra创始人权道亨已从新加坡飞往杜拜转往第三国
热门文章
- 【模型检测学习笔记】8:无限字上ω正则LT性质的验证
- 微信公众号访问 ssm框架根目录下MP_verify_xxxxxx.txt的解决方法
- windows7旗舰版序列号[经测试,第一枚即可完成升级!]
- 百度谷歌搜索引擎研究,如何做SEO优化?网站优化实操(程序员必看)
- 如何提高阅读源代码能力
- linux鼠标怎么取出来,浅析linux中鼠标数据读取
- 文件共享服务器(CIFS协议)
- Photoshop制作3D水晶按钮
- Python4班平均成绩统计_空中交通管理学院2017级学生 20182019学年第一学期成绩分析报告...
- 实验一 网络侦查与网络扫描