zoukankan      html  css  js  c++  java
  • 【luogu P1807 最长路_NOI导刊2010提高(07)】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1807
    求最大路?就是把权值取相反数跑最短路。

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    const int maxn = 100000;
    const int inf = 0x7fffffff;
    bool vis[maxn];
    struct edge{
        int from, next, len, to;
    }e[maxn];
    int head[maxn], cnt, dis[maxn], ans, m, n, u, v, w;
    queue<int> q;
    void add(int u, int v, int w)
    {
        e[++cnt].from = u;
        e[cnt].to = v;
        e[cnt].len = w;
        head[u] = cnt;
    }
    int SPFA()
    {
        while(!q.empty())
        {
            int now = q.front();
            q.pop();
            vis[now] = 0;
            for(int i = head[now]; i; i = e[i].next)
            {
                if(dis[e[i].to] > dis[now] + e[i].len)
                {
                    dis[e[i].to] = dis[now] + e[i].len;
                    if(!vis[e[i].to])
                    {
                        vis[e[i].to] = 1;
                        q.push(e[i].to);
                    }   
                }
            }
        }
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            w = w*-1;
            add(u,v,w);
        }
        int s = 1;
        dis[s] = 0;
        q.push(s);
        vis[s] = 1;
        SPFA();
        if(dis[n] != 0)
        printf("%d",abs(dis[n]));
        else
        printf("-1");
        return 0;
    }
    

    隐约雷鸣,阴霾天空,但盼风雨来,能留你在此。

    隐约雷鸣,阴霾天空,即使天无雨,我亦留此地。

  • 相关阅读:
    服务管理命令
    软件管理
    Qt软件打包与发布(windeployqt工具)
    03
    第一章 BP神经网络
    代理模式 与 Spring AOP
    java 回调机制
    HashTable 实现
    实现Singleton模式
    BST 汇总
  • 原文地址:https://www.cnblogs.com/MisakaAzusa/p/9205692.html
Copyright © 2011-2022 走看看