6.23 <409>总结

· · 个人记录

前言

。 。 。

没什么好说的,直接开始吧。 。 。

P4779

题意

给定一个n个点,m条边的有向图,请你计算从s出发到每个点的距离。

思路

这就是个dijkstra的板子吧。

错因

额……写成了SPFA。 。 。

AC Code

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e5 + 5;
int n, m;
int dp[N];
bool vis[N];
struct node
{
    int y, w;
    bool operator < (const node &tmp) const //重载
    {
        return w > tmp.w;
    }
};
vector<node> g[N];
void dijkstra(int s)
{
    priority_queue<node> q;
    memset(dp, 0x3f, sizeof dp); //初始化
    memset(vis, 0, sizeof vis);
    dp[s] = 0;
    q.push({s, dp[s]});
    while (q.empty() == 0)
    {
        int x = q.top().y;
        q.pop();
        if (vis[x] == 1) //如果已经上色了
            continue;
        vis[x] = 1; //上色
        for (auto v : g[x])
            if (dp[v.y] > dp[x] + v.w && vis[v.y] == 0)
            {
                dp[v.y] = dp[x] + v.w; //更新
                q.push({v.y, dp[v.y]}); //进队列
            }
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int s;
    cin >> n >> m >> s;
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back({v, w});
    }
    dijkstra(s);
    for (int i = 1; i <= n; i++)
        cout << dp[i] << " ";
    return 0;
}

P3385

题意

给定一个n个点的有向图,请求出图中是否存在从顶点 1 出发能到达的负环。

思路

其实就是SPFA改一下。

不难想到,一个最短路最多只经过n-1条边。既然负环会一直走下去,我们不就可以看看经过的边数大不大于这个不就行了?

错因

忘记把标记取消了。 。 。

AC Code

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2e3 + 5;
int n, m;
int dp[N], cnt[N];
bool vis[N];
struct node
{
    int y, w;
};
vector<node> g[N];
bool spfa(int s)
{
    queue<node> q;
    memset(dp, 0x3f, sizeof dp); //初始化
    memset(vis, 0, sizeof vis);
    memset(cnt, 0, sizeof cnt);
    dp[s] = cnt[s] = 0;
    q.push({s, dp[s]});
    while (q.empty() == 0)
    {
        int x = q.front().y;
        q.pop();
        vis[x] = 0; //把标记取消
        for (auto v : g[x])
            if (dp[v.y] > dp[x] + v.w)
            {
                dp[v.y] = dp[x] + v.w; //更新
                cnt[v.y] = cnt[x] + 1;
                if (cnt[v.y] >= n) //有负环
                    return 1;
                if (vis[v.y] == 0)
                {
                    vis[v.y] = 1;
                    q.push({v.y, dp[v.y]});
                }
            }
    }
    return 0; //没有负环
}
void work()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++) //记得清空
        g[i].clear();
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back({v, w});
        if (w >= 0)
            g[v].push_back({u, w});
    }
    if (spfa(1) == 1)
        cout << "YES\n";
    else cout << "NO\n";
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        work();
    }
    return 0;
}

P2564

题意

有一条彩带,上面有n颗彩珠,有k种不同的颜色,每一颗彩珠都有一个坐标。要求剪下一段连续的区间,使得这个区间里包含了所有种类的彩珠。求这个区间的最大长度。

思路

这肯定要排序吧,不然怎么叫连续的?

你想它要我们求区间最大长度,而且要包含所有种类的彩珠,怎么和逛画展如此之像?

错因

写单调队列去了。

AC Code

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e6 + 5;
int n, k;
int ans = LLONG_MAX, len, cnt;
int b[N];
struct node
{
    int id, x;
} c[N];
bool cmp(node x, node y)
{
    return x.x < y.x;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> k;
    for (int i = 1; i <= k; i++)
    {
        int t;
        cin >> t;
        for (int j = 1; j <= t; j++)
        {
            int x;
            cin >> x;
            c[++len].id = i;
            c[len].x = x;
        }
    }
    sort(c + 1, c + 1 + len, cmp); //排序
    b[c[1].id] = 1;
    cnt++;
    int lt = 1, rt = 1;
    while (lt <= n && rt <= n) //双指针
    {
        if (cnt == k) //种类达到了k
        {
            ans = min(ans, c[rt].x - c[lt].x); //更新答案
            b[c[lt].id]--;
            if (b[c[lt].id] == 0) //没有这种的了
                cnt--;
            lt++; //移动慢指针
        }
        else
        {
            rt++; //移动快指针
            if (rt > n) //越界
                break;
            b[c[rt].id]++;
            if (b[c[rt].id] == 1) //有这种种类了
                cnt++;
        }
    }
    cout << ans;
    return 0;
}