zoukankan      html  css  js  c++  java
  • vijos 1053 Easy sssp

    描述

    输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 
    要求你写一个程序, 判断这个有向图中是否存在负权回路. 如果从一个点沿着某条路径出发, 又回到了自己, 而且所经过的边上的权和小于0, 就说这条路是一个负权回路.
    如果存在负权回路, 只输出一行-1;
    如果不存在负权回路, 再求出一个点S(1 <= S <= N)到每个点的最短路的长度. 约定: S到S的距离为0, 如果S与这个点不连通, 则输出NoPath.

    格式

    输入格式

    第一行: 点数N(2 <= N <= 1,000), 边数M(M <= 100,000), 源点S(1 <= S <= N);
    以下M行, 每行三个整数a, b, c表示点a, b(1 <= a, b <= N)之间连有一条边, 权值为c(-1,000,000 <= c <= 1,000,000)

    输出格式

    如果存在负权环, 只输出一行-1, 否则按以下格式输出
    共N行, 第i行描述S点到点i的最短路: 
    如果S与i不连通, 输出NoPath;
    如果i = S, 输出0;
    其他情况输出S到i的最短路的长度.

    样例1

    样例输入1

    6 8 1
    1 3 4
    1 2 6
    3 4 -7
    6 4 2
    2 4 5
    3 6 3
    4 5 1
    3 5 4
    

    样例输出1

    0
    6
    4
    -3
    -2
    7
    

    限制

    Test5 5秒
    其余 1秒

    提示

    做这道题时, 你不必为超时担心, 不必为不会算法担心, 但是如此“简单”的题目, 你究竟能ac么?

    通过率超低一道题,

    但学过bellman-ford 应该很容易。

    本蒟蒻用的是spfa

    屠龙宝刀点击就送

    #include <ctype.h>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    #define N 10005
    #define M 100005
    
    using namespace std;
    queue<int>q;
    
    void read(int &x)
    {
        x=0;
        bool f=0;
        char ch=getchar();
        while(!isdigit(ch)) {if(ch=='-') f=1;ch=getchar();}
        while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
        x=f?(~x)+1:x;
    }
    struct node
    {
        int u,v,w;
        node (int u=0,int v=0,int w=0) :u(u),v(v),w(w){}
    }edge[M<<1];
    bool vis[N],flag=false;;
    int di,cnt,head[N],fw,out[N],n,m,s,dis[N];
    void add(int u,int v,int w)
    {
        edge[++cnt]=node(head[u],v,w);
        head[u]=cnt;
    }
    void spfa1(int pre)
    {
        if(flag) return ;
        vis[pre]=1;
        for(int i=head[pre];i;i=edge[i].u)
        {
            int to=edge[i].v;
            if(dis[to]>dis[pre]+edge[i].w)
            {
                if(vis[to]||flag)
                {
                    flag=1;
                    break ;
                }
                dis[to]=dis[pre]+edge[i].w;
                spfa1(to);
            }
        }
        vis[pre]=0;
     }
    bool pd()
    {
        for(int i=1;i<=n;i++)
        {
            spfa1(i);
            if(flag) return true;
        }
        return false;
    }
    void spfa(int s)
    {
        for(int i=1;i<=n;i++) dis[i]=0x7fffffff;
        dis[s]=0;
        q.push(s);
        while(!q.empty())
        {
            int Top=q.front();
            q.pop();
            vis[Top]=0;
            for(int i=head[Top];i;i=edge[i].u)
            {
                int v=edge[i].v;
                if(dis[v]>dis[Top]+edge[i].w)
                {
                    dis[v]=dis[Top]+edge[i].w;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        q.push(v); 
                    }
                }
            }
        }
    }
    int main()
    {
        read(n);
        read(m);
        read(s);
        for(int a,b,c;m--;)
        {
            read(a);
            read(b);
            read(c);
            add(a,b,c);
        }
        if(pd())
            printf("-1");
        else 
        {
            spfa(s);
            for(int i=1;i<=n;i++)
            {
                if(dis[i]==0x7fffffff) printf("NoPath
    ");
                else printf("%d
    ",dis[i]);
            }
        }
        return 0;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    B.Icebound and Sequence
    Educational Codeforces Round 65 (Rated for Div. 2) D. Bicolored RBS
    Educational Codeforces Round 65 (Rated for Div. 2) C. News Distribution
    Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers
    Educational Codeforces Round 65 (Rated for Div. 2) A. Telephone Number
    Codeforces Round #561 (Div. 2) C. A Tale of Two Lands
    Codeforces Round #561 (Div. 2) B. All the Vowels Please
    Codeforces Round #561 (Div. 2) A. Silent Classroom
    HDU-2119-Matrix(最大匹配)
    读书的感想!
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7197219.html
Copyright © 2011-2022 走看看