题目链接:https://cn.vjudge.net/contest/283066#problem/L

题目大意:T组测试样例,n个点,m条边,每一条边的信息是起点,终点,边权。问你是不是存在负环,如果存在的话输出-inf,否则的话,输出最小路径(两个点之间)。

具体思路:一般的spfa只能判断一个点到其他点的最短距离,但是这个题目让我们求的是任意两个点之间的,所以我们可以通过超级源点的建立,从超级源点到每一个点都引一条边权为0的边,然后通过超级源点作为spfa的起点,就可以判断出最短距离了。

ps:当所有的边权都大于0的时候,这种情况需要特殊讨论,因为spfa的0点的初始边权是0,如果按照spfa的话,根本不会进入循环,因为只要一进入循环,dis就会增大。

AC代码:

 #include<iostream>
#include<stack>
#include<cmath>
#include<cstring>
#include<stdio.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int mod = 1e9+;
const int maxn = 2e4+;
const int maxedge= 5e4+;
int vis[maxn],head[maxn];
int dis[maxn];
int num,flag;
struct node
{
int fr;
int to;
int cost;
int nex;
} edge[maxedge];
void spfa(int u)
{
if(flag)return ;
vis[u]=;
for(int i=head[u]; i!=-; i=edge[i].nex)
{
int v=edge[i].to;
if(dis[u]+edge[i].cost<dis[v])
{
dis[v]=dis[u]+edge[i].cost;
if(vis[v])
{
flag=;
}
else
spfa(v);
}
}
vis[u]=;
}
void init()
{
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
memset(dis,inf,sizeof(dis));
num=;
flag=;
}
void addedge(int fr,int to,int cost)
{
edge[num].to=to;
edge[num].cost=cost;
edge[num].nex=head[fr];
head[fr]=num++;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
int n,m;
scanf("%d %d",&n,&m);
int t1,t2,t3;
int minn=inf;
for(int i=; i<=m; i++)
{
scanf("%d %d %d",&t1,&t2,&t3);
addedge(t1,t2,t3);
minn=min(minn,t3);
}
// if(minn>=0){
// printf("%d\n",minn);
// continue;
// }
for(int i=; i<=n; i++)
{
addedge(,i,);
}
dis[]=;
spfa();
if(flag)
printf("-inf\n");
else
{
int minn=inf;
for(int i=; i<=n; i++)
{
minn=min(minn,dis[i]);
}
printf("%d\n",minn);
}
}
return ;
}

L - The Shortest Path Gym - 101498L (dfs式spfa判断负环)的更多相关文章

  1. BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环

    Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...

  2. [BZOJ 1486][HNOI2009]最小圈(二分答案+dfs写的spfa判负环)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1486 分析:容易想到先二分答案x,然后把所有边的权值-x,那么如果图中存在权值和为0的 ...

  3. SPFA找负环(DFS) luogu3385

    SPFA找负环的基本思路就是如果一个点被访问两次说明成环,如果第二次访问时所用路径比第一次短说明可以通过一直跑这个圈将权值减为负无穷,存在负环 有bfs和dfs两种写法,看了一些博客,在bfs和dfs ...

  4. Shortest Path(思维,dfs)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  5. ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  6. AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和单源最短路径)

    题目链接: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B   Single Source Shortest Path ...

  7. The Shortest Path in Nya Graph---hdu4725(spfa+扩点建图)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4725  有n个点,每个点都有一个层l[i],相邻层的边有一条无向带权边,权值为都为C,另外 ...

  8. 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)

    职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...

  9. HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...

随机推荐

  1. Python连接字符串用join还是+

    我们先来看一下用join和+连接字符串的例子 str1 = " ".join(["hello", "world"]) str2 = &quo ...

  2. c++ 可变参数模板

    可变参数模板,自己尝试了个例子,如下: // variadicTemplates.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #incl ...

  3. Ubuntu 16.04安装Maven

    此篇为http://www.cnblogs.com/EasonJim/p/7139275.html的分支页. 前提:必须正确安装JDK. 一.通过二进制包(tar.gz)安装 下载: 进入下载列表:h ...

  4. mybatis 缓存(cache)的使用

    许多应用程序,为了提高性能而增加缓存, 特别是从数据库中获取的数据. 在默认情况下,mybatis 的一级缓存是默认开启的.类似于hibernate, 所谓一级缓存,也就是基于同一个sqlsessio ...

  5. Java 8 forEach简单例子(转载)

    forEach and Map 1.1 通常这样遍历一个Map Map<String, Integer> items = new HashMap<>(); items.put( ...

  6. MT【13】三角函数求范围

    解答:AB显然正确,C中$a$取0时,解为三个,C 错误.我们主要看一下D 评:这里提供了一个处理$sin^2xcosx$的常见方法:平方,单变量后用算术几何不等式.

  7. 【Luogu5108】仰望半月的夜空(后缀数组)

    [Luogu5108]仰望半月的夜空(后缀数组) 题面 洛谷 题解 实名举报这题在比赛之前还不是这个样子的,还被我用SAM给水过去了 很明显求出\(SA\)之后就是按照\(SA\)的顺序从前往后考虑每 ...

  8. HGOI 20180224 题解

    /* The Most Important Things: ljc chat with fyh on QQTa说期末考Ta数学74分感觉不好但是我觉得fyh是地表最强的鸭~~(of course en ...

  9. poj2386(简单的dfs/bfs)

    题目链接:http://poj.org/problem?id=2386 Description Due to recent rains, water has pooled in various pla ...

  10. (转)小谈keepalived vip漂移原理与VRRP协议

    背景:之前搭建过keepalived双机热备的集群,但对其中的原理不甚理解,看完就忘了,所有有必要深入的学习下. 简介 什么是keepalived呢?keepalived是实现高可用的一种轻量级的技术 ...