传送门

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

但我不想这样做。

于是写了一个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. leetcode754

    class Solution { public: int reachNumber(int target) { // 理解这题的意思 这题就好做了 // 分析 首先考虑一种比较极端的情况 即一直向正方向 ...

  2. JPQL和SQL的比较

    前言 在JAVA EE中,JPQL是专门为Java 应用程序访问和导航实体实例设计的.Java Presistence Query Language(JPQL),java持久性查询语言.它是JPA规范 ...

  3. onMouseDown和onPress的差异AS2

    為了要做出比Button物件更複雜的互動,我們通常會改用MovieClip來製作按鈕.如此一來,就需要處理event handler.與滑鼠有關的MovieClip event handler包括on ...

  4. span width不起作用,border 无效

    span属于内联元素,因此width对内联元素不起作用. 需要设置display:inline-block;使其成为内联级块级元素: border不起作用,主要是未设置border-style:sol ...

  5. python之内置函数:map ,filter ,reduce总结

    map函数: #处理序列中的每个元素,得到的结果是一个'列表',该列表元素个数及位置与原来一样 filter函数: #遍历序列中的每个元素,判断每个元素得到一个布尔值,如果是true,则留下来 peo ...

  6. Gulp的安装与配置

    http://blog.csdn.net/itlsx/article/details/49981459

  7. 电影TS、TC、BD版和HD版

    HD的意思是指HDTV,HDTV指网上下载的高清影片,它的画面品质会比BD稍差,主要表现为亮度不足,色彩不自然等.BD是指蓝光(Blu-ray)或称蓝光盘(Blu-ray Disc,缩写为BD),目前 ...

  8. How to Pronounce ‘to the’ in a Sentence

    How to Pronounce ‘to the’ in a Sentence Share Tweet Share Tagged With: The Word THE, TO Reduction St ...

  9. time和datetime

    一.time模块常用函数1. time()函数time()函数返回的是时间戳(timestamp).所谓时间戳指的是从1970年1月1日00:00:00开始按秒计算的偏移量.其他返回时间戳方式的函数还 ...

  10. mark_2017_2_27

    工作总结web_acl 535 git clone “ssh://git@outergit.yonyou.com:49622/esn_web/web_acl.git" 600 git bra ...