传送门

貌似可以最短路时同时判定负环啊。

但我不想这样做。

于是写了一个dfs版的判环,bfs版的求最短路。

代码:

#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<queue>
#define N 1005
#define M 1000005
#define inf 0x3f3f3f3f
using namespace std;
inline int read(){
    int ans=0,w=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans*w;
}
int n,m,first[N],cnt=0,d[N];
bool in[N];
struct edge{int v,next,w;}e[M<<1];
inline void add(int u,int v,int w){e[++cnt].v=v,e[cnt].w=w,e[cnt].next=first[u],first[u]=cnt;}
inline bool spfa(int p){
    in[p]=true;
    for(int i=first[p];i;i=e[i].next){
        int v=e[i].v;
        if(d[v]>d[p]+e[i].w){
            d[v]=d[p]+e[i].w;
            if(in[v]||spfa(v))return in[p]=false,true;
        }
    }
    return in[p]=false;
}
inline bool check(){
    for(int i=1;i<=n;++i)if(spfa(i))return true;
    return false;
}
inline void Spfa(int s){
    queue<int>q;
    for(int i=1;i<=n;++i)d[i]=inf,in[i]=false;
    q.push(s),d[s]=0,in[s]=true;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        in[x]=false;
        for(int i=first[x];i;i=e[i].next){
            int v=e[i].v;
            if(d[v]>d[x]+e[i].w){
                d[v]=d[x]+e[i].w;
                if(!in[v])in[v]=true,q.push(v);
            }
        }
    }
}
int main(){
    int s;
    n=read(),m=read(),s=read();
    for(int i=1;i<=m;++i){
        int u=read(),v=read(),w=read();
        add(u,v,w);
    }
    if(check()){printf("-1");return 0;}
    Spfa(s);
    for(int i=1;i<=n;++i){
        if(d[i]==inf)puts("NoPath");
        else printf("%d\n",d[i]);
    }
    return 0;
}

2018.09.15 vijos1053Easy sssp(最短路)的更多相关文章

  1. Lean Data Innovation Sharing Salon(2018.09.15)

    时间:2018.09.15地点:北京国华投资大厦

  2. 2018.09.15点名器(简单dp)

    描述 Ssoier在紧张的学习中,杜老师每天给他们传授精妙的知识. 杜老师为了活跃气氛,设计了一个点名器,这个点名器包含一个长度为M的数组(下标1开始),每个元素是一个oier的名字,每次点名的时候, ...

  3. 2018.09.15 poj1734Sightseeing trip(floyd求最小环)

    跟hdu1599差不多.. 只是需要输出方案. 这个可以递归求解. 代码: #include<iostream> #include<cstdio> #include<cs ...

  4. 2018.09.15 hdu1599find the mincost route(floyd求最小环)

    传送门 floyd求最小环的板子题目. 就是枚举两个相邻的点求最小环就行了. 代码: #include<bits/stdc++.h> #define inf 0x3f3f3f3f3f3f ...

  5. 2018.09.15 bzoj1977:次小生成树 Tree(次小生成树+树剖)

    传送门 一道比较综合的好题. 由于是求严格的次小生成树. 我们需要维护一条路径上的最小值和次小值. 其中最小值和次小值不能相同. 由于不喜欢倍增我选择了用树链剖分维护. 代码: #include< ...

  6. 2018.09.15 秘密的牛奶管道SECRET(次小生成树)

    描述 约翰叔叔希望能够廉价连接他的供水系统,但是他不希望他的竞争对手知道他选择的路线.一般这样的问题需要选择最便宜的方式,所以他决定避免这种情况而采用第二便宜的方式. 现在有W(3 <= W & ...

  7. 2018.09.15 hdu3018Ant Trip(欧拉路)

    传送门 显然答案等于各个连通分量的笔画数之和. 因此我们dfs每个连通分量计算对答案的贡献. 对于一个连通分量,如果本来就有欧拉回路那么只需要一笔. 否则需要寄点数/2那么多笔才能画完. 知道这个结论 ...

  8. 2018.09.15 poj2117Electricity(割点)

    传送门 其实求一个图删除一个点之后,联通块最多有多少. 直接tarjan求割点更新答案就行了. 但注意原图不一定连通. 代码: #include<iostream> #include< ...

  9. 2018.09.15[POI2008]BLO-Blockade(割点)

    描述 There are exactly nn towns in Byteotia. Some towns are connected by bidirectional roads. There ar ...

随机推荐

  1. location 对象属性

    Location 对象属性 hash 返回一个URL的锚部分 host 返回一个URL的主机名和端口 hostname 返回URL的主机名 href 返回完整的URL pathname 返回的URL路 ...

  2. IdUDPServer 收到4次重复的数据

    IdUDPServer1->Send(RemoteIP, LabeledEdit2->Text.ToInt(), InText, IndyTextEncoding_UTF8()); 我发给 ...

  3. PHP判断客户端是PCweb端还是移动手机端方法

    /** * * 根据php的$_SERVER['HTTP_USER_AGENT'] 中各种浏览器访问时所包含各个浏览器特定的字符串来判断是属于PC还是移动端 * @author discuz3x * ...

  4. DC组策略相关

    恢复DC组策略默认配置 DcGPOFix [/ignoreschema] [/Target: Domain | DC | BOTH] dcgpofix /? gpupdate刷新 gpedit.msc ...

  5. as2 针对加载进来的swf操作

    如果加载的子swf,里面的东西会随时发生变化,那么as2获取的子swf宽高也会不停在变动. 解决方法,就是在子swf里面控制设置方法,当as2需要准确获取子swf宽高时,迫使子宽高恢复到原本样子,这样 ...

  6. Inspector视图中的get/set使用

    using UnityEngine; using System.Collections; public class Test : MonoBehaviour { public int width { ...

  7. DataType 数据类型

    基本类型:四类八种:数值 : 整数:byte,short,int,long.默认是 int 小数:float,double                  默认是 double 布尔:boolean ...

  8. Solving SharePoint Server 2010 - 503. The service is unavailable, After installation

    Installed: SharePoint Server 2010 for Internet Enterprise Beta (x64) On: Windows Server 2008 Standar ...

  9. array numpy 模块

    高级用法:http://www.jb51.net/article/87987.htm from array import * 调用 array 与 import numpy as np  调用 np. ...

  10. DEXSeq

    1)Introduction DEXSeq是一种在多个比较RNA-seq实验中,检验差异外显子使用情况的方法. 通过差异外显子使用(DEU),我们指的是由实验条件引起的外显子相对使用的变化. 外显子的 ...