L - The Shortest Path Gym - 101498L (dfs式spfa判断负环)
题目链接: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判断负环)的更多相关文章
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- [BZOJ 1486][HNOI2009]最小圈(二分答案+dfs写的spfa判负环)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1486 分析:容易想到先二分答案x,然后把所有边的权值-x,那么如果图中存在权值和为0的 ...
- SPFA找负环(DFS) luogu3385
SPFA找负环的基本思路就是如果一个点被访问两次说明成环,如果第二次访问时所用路径比第一次短说明可以通过一直跑这个圈将权值减为负无穷,存在负环 有bfs和dfs两种写法,看了一些博客,在bfs和dfs ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- 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 ...
- 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 ...
- The Shortest Path in Nya Graph---hdu4725(spfa+扩点建图)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4725 有n个点,每个点都有一个层l[i],相邻层的边有一条无向带权边,权值为都为C,另外 ...
- 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)
职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...
- HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...
随机推荐
- [代码]--c#-实现局域网聊天
服务器端: using System; using System.Collections.Generic; using System.Linq; using System.Net; using Sys ...
- BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树
题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...
- sakila数据库及其他数据库实例文件
下载地址: https://dev.mysql.com/doc/index-other.html Other MySQL Documentation This page provides additi ...
- HDU4403-模拟、数学
一道很难的奥数题,给出一个数字串,插入加号和等号使之成立.求成立的算式数. 我的做法是,先分成两段,中间插入等号 ,再分别求出左右两边可能的值和个数,然后对比,把值相等的情况乘起来,加到最终结果上. ...
- bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...
- JPQL设置自增长、只读、文本类型等的注解
JAVA中使用JPQL 一种设置id自动生成,自增长的方法 private long id; @Id @GeneratedValue(generator="_native") @G ...
- day5 continue 和 break的区别
# continue num = 1 while num <=10: num += 1 if num == 3: continue print(num) # continue 表示跳出本次循环后 ...
- HNOI2017单旋
单旋 这道题做法贼多,LCT,splay,线段树什么的貌似都行. 像我这种渣渣只会线段树了(高级数据结构学了也不会用). 首先离线所有操作,因为不会有两个点值重复,所以直接离散. 一颗线段树来维护所有 ...
- 探测.yml
liveness.yml #探测apiVersion: v1kind: Podmetadata: labels: test: liveness name: livenessspec: restartP ...
- Python进制表示及转换
进制表示: 二进制:>>> abin = 0b1000>>> abin8 八进制:>>> aoct = 0o123 (数字0,字母o)>&g ...