• 最短路默写


    Dij

            bool visit[MAXN];
            priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
            pair<int, int> cnt;
            memset(visit, false, sizeof(visit));
            visit[st] = true;
            que.push(make_pair(0, st));
            while (!que.empty())
            {
                    cnt = que.top();
                    que.pop();
                    int ucost = cnt.first;
                    int u = cnt.second;
                    for (int i = Head[u]; i; i = nxt[i])
                    {
                            int v = to[i];
                            if (visit[v])
                            {
                                    continue;
                            }
                            visit[v] = true;
                            if (dis[v] > dis[u] + cost[i])
                            {
                                    dis[v] = dis[u] + cost[i];
                                    que.push(make_pair(dis[v], v));
                            }
                    }
            }

    SPFA

    dfs求负环版

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 2e5+ 5;
    struct lpl{ int to, dis; }lin;
    vector<lpl> point[maxn];
    int n, m, dis[maxn];
    bool Flag, vis[maxn];
    
    inline void connect(int a, int b, int w){ lin.to = b; lin.dis = w; point[a].push_back(lin); }
    
    inline void putit()
    {
        int a, b, w;
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; ++i) point[i].clear();
        for(int i = 1; i <= m; ++i)
        {
            scanf("%d%d%d", &a, &b, &w);
            connect(a, b, w);
            if(w > 0) connect(b, a, w);
        }
    }
    
    void SPFA(int t)
    {
        int now; vis[t] = true;
        for(int i = point[t].size() - 1; i >= 0; --i)
        {
            now = point[t][i].to;
            if(dis[now] > dis[t] + point[t][i].dis)
            {
                dis[now] = dis[t] + point[t][i].dis;
                if(vis[now] == true || Flag == true) { Flag = true; break; }
                SPFA(now);
            }
        }
        vis[t] = false;
    }
    
    int main()
    {
        int T; scanf("%d", &T);
        while(T--)
        {
            putit();
            memset(dis, 0, sizeof(dis)); memset(vis, false, sizeof(vis)); Flag = false;
            for(int i = 1; i <= n; ++i) { SPFA(i); if(Flag == true) break; }
            if(Flag == true) { printf("YE5
    "); continue;}
            printf("N0
    ");
        }
        return 0;
    }
    //spfa negetive circle
            bool visit[MAXN];
            queue<int> que;
            memset(visit, false, sizeof(visit));
            dis[st] = 0, visit[st] = true;
            que.push(st);
            while (!que.empty())
            {
                    int u = que.front();
                    que.pop();
                    visit[u] = false;
                    for (int i = Head[u]; i; i = nxt[i])
                    {
                            int v = to[i];
                            if (dis[v] > dis[u] + cost[i])
                            {
                                    dis[v] = dis[u] + cost[i];
                                    if (!visit[v])
                                    {
                                            visit[v] = true;
                                            que.push(v);
                                    }
                            }
                    }
            }
  • 相关阅读:
    javascript函数的定义和调用(包括方法)
    iterable(遍历)
    循环
    C#基础知识 简单说明泛型的优点
    C#基础知识 yield与foreach
    C#基础知识 结构与类的区别
    Asp.net MVC 生成zip并下载
    Asp.net MVC 填充word并下载
    Asp.net MVC 简单实现生成Excel并下载
    CTF中怎么看phpinfo
  • 原文地址:https://www.cnblogs.com/Aragaki/p/9320478.html
Copyright © 2020-2023  润新知