2020暑期训练6

图论

SPFA

Shortest Path Faster Algorithm,即队列优化的Bellman-Ford算法,最坏情况下时间复杂度 O ( n m ) O(nm) O(nm),好像只能在负权图里用,不然会被卡死。
例题:P3371 【模板】单源最短路径(弱化版)
就是BFS的原理

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 0x7fffffff;
const int maxn = 1e5 + 9;
inline int read()
{
    int data = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        data = (data << 3) + (data << 1) + ch - '0';
        ch = getchar();
    }
    return f * data;
}
int cnt_e;
const int maxm = 5e5 + 9;
struct edge
{
    int next, to, dis;
} e[maxm];
int dis[maxn], vis[maxn], head[maxm];
void add(int from, int to, int dis)
{
    e[++cnt_e].next = head[from];
    e[cnt_e].to = to;
    e[cnt_e].dis = dis;
    head[from] = cnt_e;
}
int n, m, s;
void spfa()
{
    queue<int> q;
    for (int i = 1; i <= n; ++i)
    {
        dis[i] = INF;
        vis[i] = 0;
    }
    q.push(s);
    dis[s] = 0, vis[s] = 1;
    while (!q.empty())
    {
        int u = q.front();
        q.pop(), vis[u] = 0;
        for (int i = head[u]; i; i = e[i].next)
        {
            int v = e[i].to;
            if (dis[v] > dis[u] + e[i].dis)
            {
                dis[v] = dis[u] + e[i].dis;
                if (!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("data.txt","w",stdout);
    //std::ios::sync_with_stdio(false);
    //std::cin.tie(0);
    n = read(), m = read(), s = read();
    while (m--)
    {
        int a = read(), b = read(), c = read();
        add(a, b, c);
    }
    spfa();
    for (int i = 1; i <= n; ++i)
        cout << dis[i] << ' ';
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

链式前向星存的图,原理就是头插法的静态链表。

负环

SPFA判负环,如果存在负环就会一直松弛下去,所以当到某个点的最短路经过了不少于 n n n 个点,那说明一定出现了负环。
例题:P3385 【模板】负环

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 0x3f3f3f3f;
inline int read()
{
    int data = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        data = (data << 3) + (data << 1) + ch - '0';
        ch = getchar();
    }
    return f * data;
}
const int maxn = 2e3 + 10;
const int maxm = 6e3 + 10;
int n, m, head[maxn], tot, dis[maxn], vis[maxn], cnt[maxn];
struct edge
{
    int to, w, next;
} e[maxm];
queue<int> q;
void reset()
{
    for (int i = 0; i < maxm; i++)
        e[i].next = 0;
    for (int i = 0; i < maxn; i++)
        head[i] = 0;
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    memset(cnt, 0, sizeof(cnt));
    while (!q.empty())
        q.pop();
    tot = 0;
}
void add(int u, int v, int w)
{
    e[++tot].to = v;
    e[tot].w = w;
    e[tot].next = head[u];
    head[u] = tot;
}
bool spfa()
{
    dis[1] = 0, vis[1] = true;
    q.push(1);
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        vis[x] = false;
        for (int i = head[x]; i; i = e[i].next)
        {
            int y = e[i].to, z = e[i].w;
            if (dis[y] > dis[x] + z)
            {
                dis[y] = dis[x] + z;
                cnt[y] = cnt[x] + 1;
                if (cnt[y] >= n)
                    return true;
                if (!vis[y])
                {
                    q.push(y);
                    vis[y] = true;
                }
            }
        }
    }
    return false;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("data.txt","w",stdout);
    //std::ios::sync_with_stdio(false);
    //std::cin.tie(0);
    int T = read();
    while (T--)
    {
        reset();
        n = read(), m = read();
        while (m--)
        {
            int u = read(), v = read(), w = read();
            add(u, v, w);
            if (w >= 0)
                add(v, u, w);
        }
        puts(spfa() ? "YES" : "NO");
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

图论要学的新知识实在是太多了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值