洛谷 P1807 最长路_NOI导刊2010提高(07)
2024-10-15 23:40:06
最长路
#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;
}
最新文章
- select2插件不兼容ie7,ie7下样子显示错位问题
- vs2015 现用插件
- IOS第四天(6:答题区按钮点击和乱序)
- win7 下面使用任务计划程序执行php脚步
- 【转】android 欢迎界面翻页成效,仿微信第一次登陆介绍翻页界面
- jQuery跳房子插件hopscotch
- Magnum Kuernetes源码分析(二)
- java-进程
- Spring中ApplicationContextAware的用法
- VS2010 EXCEL2010 表格操作的编程实现
- SpringMVC源码分析--容器初始化(五)DispatcherServlet
- spring中配置quartz调用两次及项目日志log4j不能每天生成日志解决方法
- revit二次开发wpf里button按钮无法实现事务
- 玩转C线性表和单向链表之Linux双向链表优化
- android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app
- 《Linux内核分析》-- 扒开系统调用的三层皮(下)之system_call中断处理过程 20135311傅冬菁
- C语言qsort用法
- require的压缩命令
- Struts2拦截器的配置
- 深入理解Scala的隐式转换
热门文章
- Thrift简析
- rman理论(一)
- windows下VisualStudio和QtCreator搭建Qt开发环境
- openstack 创建镜像生成虚拟机不知道密码如何解决
- <;正则吃饺子>; :关于redis集群的测试demo
- 一步步实现 Prism + MEF(一)--- 搭建框架
- eclipse中jquery.js文件有错误提示…
- codeblocks 汉字乱码
- UVaLive 3530 Martian Mining (简单DP)
- [UE4]C++实现动态加载的问题:LoadClass()和LoadObject()