#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct edge{
int to;
int next;
int len;
}qwq[];
queue<int>pq;
int edge_cnt=,n,m,head[],in[],stk[],dist[];
bool spfa()
{
memset(in,,sizeof(in));
memset(stk,,sizeof(stk));
memset(dist,-,sizeof(dist));
dist[]=;
while(!pq.empty())
{
pq.pop();
}
pq.push();
in[]++;
stk[]=;
while(!pq.empty())
{
int qaq=pq.front();pq.pop();
stk[qaq]=;
for(int i = head[qaq];i!=-;i=qwq[i].next)
{
int v=qwq[i].to;
if(dist[v]<dist[qaq]+qwq[i].len)
{
dist[v]=dist[qaq]+qwq[i].len;
if(!stk[v])
{
pq.push(v);
in[v]++;
stk[v]=;
if(in[v]>n+){
return false;
}
}
}
}
}
return true;
}
void add(int x,int y,int z)
{
qwq[edge_cnt].to=y;
qwq[edge_cnt].next=head[x];
qwq[edge_cnt].len=z;
head[x]=edge_cnt++;
}
int main()
{
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
edge_cnt=;
for(int i = ; i < m ;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
if(!spfa())printf("-1\n");
return ;
}

spfa【模板】的更多相关文章

  1. SPFA模板 Bellmanford优化版

    SPFA模板: queue<int>Q; ]; ],sumv[]; *],__next[*],e,w[*],first[],cnts[]; void AddEdge(int U,int V ...

  2. floyed dij spfa 模板

    /* SPFA模板 */ const int inf=0x3f3f3f3f; inline int SPFA(int s){ memset(dis,inf,sizeof(dis)); queue< ...

  3. spfa模板

    通过stl的queue实现的spfa(vector实现邻接表存图) 本模板没有考虑存在两点不连通的情况 如果需要判断则需要用到并查集或者遍历整个邻接表 #include<iostream> ...

  4. spfa(模板)

    spfa作为图论中的常用算法,深受各类出题人和各位OIer的喜爱: so,为了给大众创造福利,宝宝在此奉上spfa大发的思路和模板:以感谢社会, 感谢CCF,感谢CCTV, 感谢我的老师,感谢同学们, ...

  5. 最短路算法 -- SPFA模板

    一.算法步骤 建立一个队列,初始时队列里只有起始点,再建立一个数组记录起始点到所有点的最短路径(该数组的初始值要赋为极大值,该点到它本身的路径赋为0,下面的模板中该数组为dist[]).然后执行松弛操 ...

  6. hdu-2544-最短路(SPFA模板)

    题目链接 题意很清晰,入门级题目,适合各种模板,可用dijkstra, floyd, Bellman-ford, spfa Dijkstra链接 Floyd链接 Bellman-Ford链接 SPFA ...

  7. spfa模板+讲解

    zz http://blog.sina.com.cn/s/blog_6ad20aef0100mc1a.html Spfa算法 (模板源代码) 这是Bellman Ford的改进算法.    算法介绍: ...

  8. 图论--最短路--SPFA模板(能过题,真没错的模板)

    [ACM常用模板合集] #include<iostream> #include<queue> #include<algorithm> #include<set ...

  9. UVA 558 判定负环,spfa模板题

    1.UVA 558 Wormholes 2.总结:第一个spfa,好气的是用next[]数组判定Compilation error,改成nexte[]就过了..难道next还是特殊词吗 题意:科学家, ...

  10. Dijkstra堆优化与SPFA模板

    Dijkstra+优先队列 #include<cstdio> #include<cctype> #include<queue> #include<cstrin ...

随机推荐

  1. HDU 6090 Rikka with Graph

    Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...

  2. 算法笔记--STL中的各种遍历及查找(待增)

    算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...

  3. Android之RecyclerView实现时光轴

    做项目的过程中有个需求需要时光轴,于是网上找了部分资料 ,写了个案例,现在分享给大家. 如图: activity_main.xml <?xml version="1.0" e ...

  4. Confluence 6 连接一个目录

    你可以添加下面类型的目录服务器和目录管理器: Confluence 的内部目录(Configuring the Internal Directory). Microsoft Active Direct ...

  5. hdu4847 kmp

    Chen, Adrian (November 7, 2013). “Doge Is An Ac- tually Good Internet Meme. Wow.”. Gawker. Retrieved ...

  6. python-day8-列表的内置方法

    # l=[1,2,3] #l=list([1,2,3])# print(type(l)) #pat1===>优先掌握部分# 索引## 切片l=['a','b','c','d','e','f'] ...

  7. python-day4笔记

    1.文件后缀名对python运行没关系 2.Python解释器执行python程序的过程:python3 C:\test.py 1)启动python解释器(内存中) 2)将C:\test.py内容从硬 ...

  8. Leetcode 86

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  9. oracle传入一个可能为空的参数进行查询

    在我们数据库的表中的某些字段可能为空,且传入的查询参数也可能为空. 例如 ,,); 其查询结果集如下 MAPPING_ID PARTY_ID VENDOR_ID SUPPLIER_REG_ID 332 ...

  10. 10. Regular Expression Matching *HARD*

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...