最长路

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
//Mystery_Sky
//
#define maxn 1000010
#define maxm 5000050
#define INF 0x3f3f3f3f
queue <int> q;
int ind[maxn], dis[maxn];
struct Edge{
int to, next, w;
}edge[maxn];
int head[maxn], cnt, ans;
int n, m;
inline void add_edge(int u, int v, int w)
{
edge[++cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt;
}
inline void topo()
{
for(int i = 1; i <= n; i++) {
if(!ind[i]) {
q.push(i);
}
}
dis[1] = INF;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; i ; i = edge[i].next) {
int v = edge[i].to;
// printf("%d %d %d\n", u, v, edge[i].w);
ind[v]--;
dis[v] = max(dis[v], dis[u] + edge[i].w);
if(!ind[v]) q.push(v);
}
} } int u, v, w;
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
ind[v]++;
}
topo();
if(dis[n]) printf("%d\n", dis[n]-INF);
else printf("-1\n");
return 0;
}

最新文章

  1. select2插件不兼容ie7,ie7下样子显示错位问题
  2. vs2015 现用插件
  3. IOS第四天(6:答题区按钮点击和乱序)
  4. win7 下面使用任务计划程序执行php脚步
  5. 【转】android 欢迎界面翻页成效,仿微信第一次登陆介绍翻页界面
  6. jQuery跳房子插件hopscotch
  7. Magnum Kuernetes源码分析(二)
  8. java-进程
  9. Spring中ApplicationContextAware的用法
  10. VS2010 EXCEL2010 表格操作的编程实现
  11. SpringMVC源码分析--容器初始化(五)DispatcherServlet
  12. spring中配置quartz调用两次及项目日志log4j不能每天生成日志解决方法
  13. revit二次开发wpf里button按钮无法实现事务
  14. 玩转C线性表和单向链表之Linux双向链表优化
  15. android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app
  16. 《Linux内核分析》-- 扒开系统调用的三层皮(下)之system_call中断处理过程 20135311傅冬菁
  17. C语言qsort用法
  18. require的压缩命令
  19. Struts2拦截器的配置
  20. 深入理解Scala的隐式转换

热门文章

  1. Thrift简析
  2. rman理论(一)
  3. windows下VisualStudio和QtCreator搭建Qt开发环境
  4. openstack 创建镜像生成虚拟机不知道密码如何解决
  5. &lt;正则吃饺子&gt; :关于redis集群的测试demo
  6. 一步步实现 Prism + MEF(一)--- 搭建框架
  7. eclipse中jquery.js文件有错误提示…
  8. codeblocks 汉字乱码
  9. UVaLive 3530 Martian Mining (简单DP)
  10. [UE4]C++实现动态加载的问题:LoadClass()和LoadObject()