图论--最长路--基于SPFA的调整模板
#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
#define Swap(a,b) a^=b^=a^=b
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define INF 0x3f3f3f3f
#define maxn 100010
#define Ege 100000000
#define Vertex 1005
#define esp 1e-9
#define mp(a,b) make_pair(a,b)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
struct Node
{
int to, lat, val; //边的右端点,边下一条边,边权
};
Node edge[1000005];
int head[1005],tot,dis[1005],N,M,vis[1005];
void add(int from, int to, int dis)
{
edge[++tot].lat = head[from];
edge[tot].to = to;
edge[tot].val = dis;
head[from] = tot;
}
void spfa(int s)
{
for(int i=0;i<=N;i++) dis[i]=-INF;
dis[0]=0;
memset(vis, 0, sizeof(vis));
vis[s] = 1;
dis[s] = 0;
queue<int>Q;
Q.push(s);
while (!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = 0;
for (int i = head[u]; i; i = edge[i].lat)
{
int to = edge[i].to;
int di = edge[i].val;
if (dis[to]<dis[u] + di)
{
dis[to] = dis[u] + di;
if (!vis[to])
{
vis[to] = 1;
Q.push(to);
}
}
}
}
}
int main()
{
int t, x;
memset(head, 0, sizeof(head));
cini(N),cini(M);
while (M--)
{
int a, b, dis;
scanf("%d %d %d", &a, &b, &dis);
add(a, b, dis);
}
spfa(1);
if(dis[N]==-INF) {return cout<<-1<<endl,0;}
cout<<dis[N]<<endl;
return 0;
}
图论--最长路--基于SPFA的调整模板的更多相关文章
- 图论--最长路--洛谷P1807 最长路_NOI导刊2010提高(07)
题目描述 设G为有n个顶点的有向无环图,G中各顶点的编号为1到n,且当为G中的一条边时有i < j.设w(i,j)为边的长度,请设计算法,计算图G中<1,n>间的最长路径. 输入格式 ...
- FJNUOJ the greed of Yehan(最长路 + 权值乘积转化)题解
Description During the trip, Yehan and Linlin pass a cave, and there is a board at the door, which s ...
- HDU 4109 Instrction Arrangement(DAG上的最长路)
把点编号改成1-N,加一点0,从0点到之前任意入度为0的点之间连一条边权为0的边,求0点到所有点的最长路. SPFA模板留底用 #include <cstdio> #include < ...
- hdu 6501 transaction transaction transaction 最长路/树形DP/网络流
最长路: 设置一个虚拟起点和虚拟终点,每个点与起点间一条负边,值为这个点书的价值的相反数(代表买书花钱),每个点与终点连一条正边,值为这个点的书的价格(代表卖书赚钱). 然后按照图中给的边建无向边,权 ...
- spfa求最长路
http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...
- [HDU 1317]XYZZY[SPFA变形][最长路]
题意: 一个图, 点权代表走到该点可获得的能量值. 可正可负. 一个人从1 号出发,带有100点能量. 问是否有一种方案可使人在能量值>0的时候走到n. 思路: 这个题首先要注意点权. 其实就是 ...
- Ural 1450 求最长路 SPFA
题意就是求S点到T点的有向无环图中的最长路. 用SPFA可以解决. 一开始一直RE的原因 QAQ 竟然是在开Edge 邻接表的时候开小了 改了一下4Y #include <stdio.h> ...
- XYZZY(spfa求最长路)
http://acm.hdu.edu.cn/showproblem.php?pid=1317 XYZZY Time Limit: 2000/1000 MS (Java/Others) Memor ...
- XYZZY spfa 最长路 判环
题意: 有n个点 m条边 每个边有权值 一开始有一百血 每次经过一条路都会加上其权值 判断是否能够到达n 显然 有正环的时候肯定能够到达 最短路好题!!!!!!! 显用folyed判断是否联通 ...
随机推荐
- Flask 入门(七)
flask操作数据库:建表: 承接上文: 修改main.py中的代码如下: #encoding:utf-8 from flask_sqlalchemy import SQLAlchemy from f ...
- javascript入门 之 ztree(七 结点的查询)
<!DOCTYPE html> <HTML> <HEAD> <meta http-equiv="content-type" content ...
- Python爬虫系列(二):requests基础
1.发送请求: import requests # 获取数据#r是一个 response 对象.包含请求返回的内容r = requests.get('https://github.com/timeli ...
- 【Android】EventReminder使用教程(日历事件导出封装库)
碎碎念 为啥要写这个库呢? 尝试自己写一个库调用,学习一下这个流程,为以后做准备 日历库在网上的资料太少了,而这个功能却又很实用 自己做的项目都会涉及到事件导出功能,不想重复写代码 使用方法 引入 在 ...
- 端口扫描工具nmap的常用参数讲解
转载请注明出处:https://www.cnblogs.com/wangyanzhong123/p/12576406.html nmap下载与安装 这个没什么好说的.很简单官网上下载就ok了,需要注意 ...
- Spring Cloud 系列之 Gateway 服务网关(一)
什么是 Spring Cloud Gateway Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由 ...
- I - Harmonic Number LightOJ - 1234 (分段打表+暴力)
题目给的时间限制是3s,所以可以直接暴力来做,注意n的取值范围是1e8,如果开一个1e8的数组会RE.分段打表,可以每100个数记录一次,然后对每次询问先找到它所在的区间,然后在暴力往后找.(学到了~ ...
- Spring5:控制反转
二.Spring IOC控制反转 1:IOC推导 >传统业务调用编程 定义一个userDao接口:UserDao package com.spring; public interface Use ...
- Intellij IDEA 基础设置,个性化设置,好用的设置→_→
Intellij IDEA 个性化设置 Appearance & Behavior 外观和行为 Keymap 快捷键 Editor 编辑器设置 Plugins 插件 Version Contr ...
- css: scroll-table
.scroll-table { table tbody { display: block; max-height: 120px; overflow-y: scroll; } table thead,t ...